2
2
import requests
3
3
import json
4
4
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
+
5
32
# Script metadata
6
33
SCRIPT_NAME = "ollama_bot"
7
34
SCRIPT_AUTHOR = "teraflops"
8
- SCRIPT_VERSION = "1.6 "
35
+ SCRIPT_VERSION = "1.9 "
9
36
SCRIPT_LICENSE = "MIT"
10
37
SCRIPT_DESC = "Automatically responds to mentions using Ollama and allows manual queries, including PMs"
11
38
OLLAMA_API_URL = "http://localhost:11434/api/generate"
@@ -24,40 +51,55 @@ def setup_config():
24
51
def ask_ollama (message ):
25
52
"""Send a query to Ollama and return the complete response."""
26
53
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
+
29
67
response_json = response .json ()
30
68
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
39
69
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 )} "
43
72
44
73
def message_callback (data , buffer , date , tags , displayed , highlight , prefix , message ):
45
74
"""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
48
75
76
+ if weechat .config_get_plugin ("highlight_response" ) == "off" :
77
+ return weechat .WEECHAT_RC_OK
78
+
49
79
buffer_type = weechat .buffer_get_string (buffer , "localvar_type" )
50
80
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
52
82
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
55
85
if is_private and weechat .config_get_plugin ("pm_response" ) == "off" :
56
86
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 )
57
97
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
+
61
103
return weechat .WEECHAT_RC_OK
62
104
63
105
def config_callback (data , option , value ):
@@ -74,6 +116,5 @@ def config_callback(data, option, value):
74
116
# Register commands and hooks
75
117
weechat .hook_command ("ollama" , "Ask something to Ollama" , "<question>" , "Example: /ollama What is Python?" , "" , "command_ollama" , "" )
76
118
weechat .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" , "" )
78
120
weechat .hook_print ("" , "notify_private" , "" , 1 , "message_callback" , "" )
79
-
0 commit comments