From 090a2e138dee4672309ec062c26c53d318ab70ef Mon Sep 17 00:00:00 2001 From: fejy Date: Sun, 12 Jan 2025 19:20:08 -0500 Subject: [PATCH] adding openwebui bot --- ollama/aibot.py | 39 +++++++++++++++++++++++++++++++++++++++ ollama/base-config.yaml | 2 ++ ollama/maubot.yaml | 9 +++++++++ 3 files changed, 50 insertions(+) create mode 100644 ollama/aibot.py create mode 100644 ollama/base-config.yaml create mode 100644 ollama/maubot.yaml diff --git a/ollama/aibot.py b/ollama/aibot.py new file mode 100644 index 0000000..98cc6bd --- /dev/null +++ b/ollama/aibot.py @@ -0,0 +1,39 @@ +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 diff --git a/ollama/base-config.yaml b/ollama/base-config.yaml new file mode 100644 index 0000000..a5e3538 --- /dev/null +++ b/ollama/base-config.yaml @@ -0,0 +1,2 @@ +api_token: somestring +openwebui_url: http://someurl diff --git a/ollama/maubot.yaml b/ollama/maubot.yaml new file mode 100644 index 0000000..0ef98eb --- /dev/null +++ b/ollama/maubot.yaml @@ -0,0 +1,9 @@ +maubot: 0.1.0 +id: nearfuture.maubot.ai +version: 0.1.0 +modules: + - aibot +main_class: AIBot +config: true +extra_files: + - base-config.yaml