40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import json
|
|
|
|
from typing import Type
|
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
|
from maubot import Plugin, MessageEvent
|
|
from maubot.handlers import command
|
|
|
|
class Config(BaseProxyConfig):
|
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
|
helper.copy("api_token")
|
|
helper.copy("openwebui_url")
|
|
|
|
class AIBot(Plugin):
|
|
|
|
async def start(self) -> None:
|
|
await super().start()
|
|
self.config.load_and_update()
|
|
|
|
@command.new("gpt")
|
|
@command.argument("pattern", pass_raw=True, required=True)
|
|
async def gpt(self, evt: MessageEvent, pattern: str) -> None:
|
|
|
|
token=self.config['api_token']
|
|
url = self.config['openwebui_url']
|
|
headers = {
|
|
'Authorization': 'Bearer '+token
|
|
}
|
|
self.log.debug(str(headers))
|
|
payload = {"model": "llama3.1:latest","messages": [{"role": "user","content": pattern}]}
|
|
|
|
async with self.http.post(url,headers=headers,json=payload) as resp:
|
|
response_json = await resp.json()
|
|
self.log.debug(str(response_json))
|
|
self.log.debug(str(response_json['choices'][0]['message']['content']))
|
|
await evt.respond(str(response_json['choices'][0]['message']['content']))
|
|
|
|
@classmethod
|
|
def get_config_class(cls) -> Type[BaseProxyConfig]:
|
|
return Config
|