1
1
from init import *
2
- import os ,traceback ,pathlib ,logging ,datetime ,sys ,time ,aiofiles ,os ,aiohttp ,urllib .parse ,ipaddress ,aiohttp .web ,markdown
2
+ import os ,traceback ,pathlib ,logging ,datetime ,sys ,time ,aiofiles ,os ,aiohttp ,urllib .parse ,ipaddress ,aiohttp .web ,markdown , base64
3
3
import nio .crypto ,ai .llm
4
4
logger = logging .getLogger (os .path .splitext (os .path .basename (__file__ ))[0 ])
5
5
loop = None
@@ -51,9 +51,17 @@ async def handle_message_openai(room,server,message,match):
51
51
break
52
52
if len (history )> 0 :
53
53
history .pop ()
54
- words = match .args ()
55
- if words == [] or words [0 ] != match .command ():
56
- words = [match .command ()]+ words
54
+ images = []
55
+ if match :
56
+ words = match .args ()
57
+ if words == [] or words [0 ] != match .command ():
58
+ words = [match .command ()]+ words
59
+ elif hasattr (message ,'url' ):
60
+ words = ['what' ,'is' ,'this' ,'image' ,'showing' ,'?' ]
61
+ target_folder = configpath / 'files' / room .room_id [1 :room .room_id .find (':' )- 2 ] / message .event_id
62
+ with open (target_folder / message .body , "rb" ) as image_file :
63
+ encoded_string = base64 .b64encode (image_file .read ())
64
+ images .append (encoded_string )
57
65
#ask model
58
66
if hasattr (server ,'keep_alive' ):
59
67
server ._model .kwargs ['keep_alive' ] = server .keep_alive
@@ -68,7 +76,7 @@ async def handle_message_openai(room,server,message,match):
68
76
except: logging.warning('failed to set parameter:'+param)
69
77
"""
70
78
res = await bot .api .async_client .room_typing (room .room_id ,True ,timeout = 300000 )
71
- message_p = await server ._model .query (' ' .join (words ),history )
79
+ message_p = await server ._model .query (' ' .join (words ),history , images = images )
72
80
if not thread_rel :
73
81
thread_rel = message .event_id
74
82
if message_p :
@@ -168,10 +176,33 @@ async def check_status():
168
176
logger .error (str (e )+ '\n ' + str (response_json ), exc_info = True )
169
177
await bot .api .send_text_message (room .room_id ,str (e ))
170
178
await bot .api .async_client .room_typing (room .room_id ,False ,0 )
179
+ async def handle_image (room ,image ):
180
+ try :
181
+ if not hasattr (server ,'_model' ):
182
+ wol = None
183
+ if hasattr (server ,'wol' ):
184
+ wol = server .wol
185
+ apikey = None
186
+ if hasattr (server ,'apikey' ):
187
+ apikey = server .apikey
188
+ server ._model = ai .llm .model (server .model ,api = server .url ,wol = wol ,apikey = apikey )
189
+ server ._model .system = server .system
190
+ #ensure variables
191
+ if not hasattr (server ,'history_count' ):
192
+ server .history_count = 15
193
+ try : int (server .history_count )
194
+ except : server .history_count = 0
195
+ try : server .threading = server .threading .lower () == 'true' or server .threading == 'on'
196
+ except : server .threading = True
197
+
198
+ except BaseException as e :
199
+ logger .error (str (e ), exc_info = True )
200
+ await bot .api .send_text_message (room .room_id ,str (e ))
201
+
171
202
@bot .listener .on_custom_event (nio .RoomEncryptedMedia )
172
203
async def enc_file (room ,event ):
173
204
try :
174
- target_folder = configpath / 'files' / room .room_id [1 :room .room_id .find (':' )- 2 ]
205
+ target_folder = configpath / 'files' / room .room_id [1 :room .room_id .find (':' )- 2 ] / event . event_id
175
206
response = await bot .async_client .download (mxc = event .url )
176
207
pathlib .Path (target_folder ).mkdir (parents = True ,exist_ok = True )
177
208
async with aiofiles .open (str (target_folder / event .body ), "wb" ) as f :
@@ -183,16 +214,32 @@ async def enc_file(room,event):
183
214
event .source ["content" ]["file" ]["iv" ],
184
215
)
185
216
)
217
+ for server in servers :
218
+ if server .room == room .room_id :
219
+ loop = asyncio .get_running_loop ()
220
+ api = 'openai'
221
+ if hasattr (server ,'api' ):
222
+ api = getattr (server ,'api' )
223
+ if api == 'openai' :
224
+ loop .create_task (handle_message_openai (room ,server ,event ,None ))
186
225
except BaseException as e :
187
226
logger .error (str (e ), exc_info = True )
188
227
@bot .listener .on_custom_event (nio .RoomMessageMedia )
189
228
async def file (room ,event ):
190
229
try :
191
- target_folder = configpath / 'files' / room .room_id [1 :room .room_id .find (':' )- 2 ]
230
+ target_folder = configpath / 'files' / room .room_id [1 :room .room_id .find (':' )- 2 ] / event . event_id
192
231
response = await bot .async_client .download (mxc = event .url )
193
232
pathlib .Path (target_folder ).mkdir (parents = True ,exist_ok = True )
194
233
async with aiofiles .open (str (target_folder / event .body ), "wb" ) as f :
195
234
await f .write (response .transport_response ._body )
235
+ for server in servers :
236
+ if server .room == room .room_id :
237
+ loop = asyncio .get_running_loop ()
238
+ api = 'openai'
239
+ if hasattr (server ,'api' ):
240
+ api = getattr (server ,'api' )
241
+ if api == 'openai' :
242
+ loop .create_task (handle_message_openai (room ,server ,event ,None ))
196
243
except BaseException as e :
197
244
logger .error (str (e ), exc_info = True )
198
245
@bot .listener .on_message_event
0 commit comments