22import requests
33import json
44
5+ """
6+ Ollama Bot for WeeChat
7+
8+ This script automatically responds to mentions in channels and private messages using an Ollama LLM running locally.
9+
10+ Features:
11+ - Responds to mentions in channels.
12+ - Can respond to private messages if enabled.
13+ - Allows manual queries using the /ollama command.
14+ - Configurable via WeeChat /set commands.
15+
16+ Usage:
17+ - To ask a question manually:
18+ /ollama What is Python?
19+
20+ - To enable or disable automatic responses in channels:
21+ /set plugins.var.python.ollama_bot.highlight_response on # Enable responses in channels
22+ /set plugins.var.python.ollama_bot.highlight_response off # Disable responses in channels
23+
24+ - To enable or disable automatic responses in private messages:
25+ /set plugins.var.python.ollama_bot.pm_response on # Enable PM responses
26+ /set plugins.var.python.ollama_bot.pm_response off # Disable PM responses
27+
28+ Dependencies:
29+ - Requires an Ollama server running locally at http://localhost:11434/api/generate
30+ """
31+
532# Script metadata
633SCRIPT_NAME = "ollama_bot"
734SCRIPT_AUTHOR = "teraflops"
8- SCRIPT_VERSION = "1.6 "
35+ SCRIPT_VERSION = "1.9 "
936SCRIPT_LICENSE = "MIT"
1037SCRIPT_DESC = "Automatically responds to mentions using Ollama and allows manual queries, including PMs"
1138OLLAMA_API_URL = "http://localhost:11434/api/generate"
@@ -24,40 +51,55 @@ def setup_config():
2451def ask_ollama (message ):
2552 """Send a query to Ollama and return the complete response."""
2653 try :
27- data = {"model" : "gemma" , "prompt" : message , "stream" : False }
28- response = requests .post (OLLAMA_API_URL , json = data )
54+ data = {"model" : "mistral" , "prompt" : message , "stream" : False }
55+ headers = {"Content-Type" : "application/json" }
56+
57+ response = requests .post (
58+ OLLAMA_API_URL ,
59+ json = data ,
60+ headers = headers ,
61+ verify = False # Change to True if you use a valid certificate
62+ )
63+
64+ if response .status_code != 200 :
65+ return f"Error { response .status_code } : { response .text } "
66+
2967 response_json = response .json ()
3068 return response_json .get ("response" , "No response received from Ollama." )
31- except Exception as e :
32- return f"Error connecting to Ollama: { str (e )} "
33-
34- def command_ollama (data , buffer , args ):
35- """Command /ollama to manually ask Ollama a question."""
36- if not args :
37- weechat .prnt (buffer , "[Ollama] Usage: /ollama <question>" )
38- return weechat .WEECHAT_RC_OK
3969
40- response = ask_ollama (args )
41- weechat .prnt (buffer , f"[Ollama] { response } " )
42- return weechat .WEECHAT_RC_OK
70+ except requests .exceptions .RequestException as e :
71+ return f"Error connecting to Ollama: { str (e )} "
4372
4473def message_callback (data , buffer , date , tags , displayed , highlight , prefix , message ):
4574 """Detect mentions in channels or private messages and respond automatically with Ollama."""
46- if weechat .config_get_plugin ("highlight_response" ) == "off" :
47- return weechat .WEECHAT_RC_OK # Do not respond if auto-response is disabled
4875
76+ if weechat .config_get_plugin ("highlight_response" ) == "off" :
77+ return weechat .WEECHAT_RC_OK
78+
4979 buffer_type = weechat .buffer_get_string (buffer , "localvar_type" )
5080 is_private = buffer_type == "private"
51- username = weechat .info_get ("irc_nick" , "" ) # Get current IRC username
81+ username = weechat .info_get ("irc_nick" , "" ) # Get the current IRC username
5282 is_mentioned = username .lower () in message .lower ()
53-
54- # Check if PM responses are disabled
83+
84+ # Ignore private messages if pm_response is off
5585 if is_private and weechat .config_get_plugin ("pm_response" ) == "off" :
5686 return weechat .WEECHAT_RC_OK
87+
88+ # Avoid responding to every PM automatically
89+ if is_private and not message .strip ().endswith ("?" ):
90+ return weechat .WEECHAT_RC_OK
91+
92+ # Only respond in channels if mentioned
93+ if not is_private and not is_mentioned and not int (highlight ):
94+ return weechat .WEECHAT_RC_OK
95+
96+ response = ask_ollama (message )
5797
58- if int (highlight ) or is_mentioned or is_private :
59- response = ask_ollama (message )
60- weechat .command (buffer , f"/msg { prefix } { response } " )
98+ if is_private :
99+ weechat .command (buffer , f"/msg { prefix } { response } " ) # Reply to private message
100+ else :
101+ weechat .command (buffer , f"/say { response } " ) # Reply in the channel
102+
61103 return weechat .WEECHAT_RC_OK
62104
63105def config_callback (data , option , value ):
@@ -74,6 +116,5 @@ def config_callback(data, option, value):
74116# Register commands and hooks
75117weechat .hook_command ("ollama" , "Ask something to Ollama" , "<question>" , "Example: /ollama What is Python?" , "" , "command_ollama" , "" )
76118weechat .hook_print ("" , "notify_highlight" , "" , 1 , "message_callback" , "" )
77- weechat .hook_print ("" , "notify_message" , "" , 1 , "message_callback" , "" ) # Capture normal messages
119+ weechat .hook_print ("" , "notify_message" , "" , 1 , "message_callback" , "" )
78120weechat .hook_print ("" , "notify_private" , "" , 1 , "message_callback" , "" )
79-
0 commit comments