diff options
| author | salmonik <raisersoft@proton.me> | 2026-06-03 13:58:55 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-06-03 13:58:55 +0200 |
| commit | 5c66e4ca00a0a7fe32232a90f4b546459c44c774 (patch) | |
| tree | ef1a5bc7b29e9b32a777cbf826a495570fba9f28 /cogs/assistant.py | |
| parent | Fix voice reconnect race: reuse VoiceClient instead of destroying it (diff) | |
| download | senator-sperma-5c66e4ca00a0a7fe32232a90f4b546459c44c774.tar.gz senator-sperma-5c66e4ca00a0a7fe32232a90f4b546459c44c774.zip | |
feat: add local API support for OpenAI compatible APIs
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to '')
| -rw-r--r-- | cogs/assistant.py | 53 |
1 files changed, 9 insertions, 44 deletions
diff --git a/cogs/assistant.py b/cogs/assistant.py index fed7825..ffb664e 100644 --- a/cogs/assistant.py +++ b/cogs/assistant.py | |||
| @@ -5,9 +5,9 @@ | |||
| 5 | 5 | ||
| 6 | import discord | 6 | import discord |
| 7 | from discord.ext import commands | 7 | from discord.ext import commands |
| 8 | import httpx | ||
| 9 | import logging | 8 | import logging |
| 10 | import os | 9 | import os |
| 10 | from cogs.utils.llm import llm_completion | ||
| 11 | 11 | ||
| 12 | logger = logging.getLogger(__name__) | 12 | logger = logging.getLogger(__name__) |
| 13 | 13 | ||
| @@ -32,24 +32,20 @@ class Jarvis(commands.Cog): | |||
| 32 | 32 | ||
| 33 | @staticmethod | 33 | @staticmethod |
| 34 | async def assistant(message): | 34 | async def assistant(message): |
| 35 | headers = { | ||
| 36 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", | ||
| 37 | "Content-Type": "application/json" | ||
| 38 | } | ||
| 39 | |||
| 40 | chain = await Jarvis._build_message_chain(message) | 35 | chain = await Jarvis._build_message_chain(message) |
| 41 | messages = Jarvis._build_messages(message, chain) | 36 | messages = Jarvis._build_messages(message, chain) |
| 42 | payload = Jarvis._build_payload(messages) | ||
| 43 | 37 | ||
| 44 | response_data = await Jarvis._safe_api_call(payload, headers) | 38 | extra = None |
| 45 | if response_data is None: | 39 | api_url = os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1/chat/completions") |
| 40 | if "openrouter" in api_url.lower(): | ||
| 41 | extra = {"plugins": [{"id": "response-healing"}]} | ||
| 42 | |||
| 43 | response_text = await llm_completion(messages, extra=extra) | ||
| 44 | if response_text is None: | ||
| 46 | await message.reply("Przepraszam, nie mogę teraz odpowiedzieć. Spróbuj ponownie później.") | 45 | await message.reply("Przepraszam, nie mogę teraz odpowiedzieć. Spróbuj ponownie później.") |
| 47 | return | 46 | return |
| 48 | 47 | ||
| 49 | response_message = Jarvis._parse_response(response_data) | 48 | await message.reply(response_text) |
| 50 | |||
| 51 | channel = message.channel | ||
| 52 | await message.reply(response_message) | ||
| 53 | 49 | ||
| 54 | @staticmethod | 50 | @staticmethod |
| 55 | async def _build_message_chain(message): | 51 | async def _build_message_chain(message): |
| @@ -93,38 +89,7 @@ class Jarvis(commands.Cog): | |||
| 93 | 89 | ||
| 94 | return messages | 90 | return messages |
| 95 | 91 | ||
| 96 | @staticmethod | ||
| 97 | def _build_payload(messages): | ||
| 98 | return { | ||
| 99 | "model": os.getenv("OPENROUTER_MODEL"), | ||
| 100 | "messages": messages, | ||
| 101 | "plugins": [{"id": "response-healing"}] | ||
| 102 | } | ||
| 103 | |||
| 104 | @staticmethod | ||
| 105 | async def _make_api_request(payload, headers): | ||
| 106 | async with httpx.AsyncClient(timeout=30.0) as client: | ||
| 107 | url = "https://openrouter.ai/api/v1/chat/completions" | ||
| 108 | response = await client.post(url=url, headers=headers, json=payload) | ||
| 109 | response_data = response.json() | ||
| 110 | logger.info(response_data) | ||
| 111 | return response_data | ||
| 112 | |||
| 113 | @staticmethod | ||
| 114 | async def _safe_api_call(payload, headers): | ||
| 115 | try: | ||
| 116 | return await Jarvis._make_api_request(payload, headers) | ||
| 117 | except httpx.HTTPError as e: | ||
| 118 | logger.error(f"API request failed: {e}") | ||
| 119 | return None | ||
| 120 | |||
| 121 | @staticmethod | ||
| 122 | def _parse_response(response_data): | ||
| 123 | content = response_data['choices'][0]['message']['content'] | ||
| 124 | |||
| 125 | logger.info(content) | ||
| 126 | 92 | ||
| 127 | return content | ||
| 128 | 93 | ||
| 129 | def setup(bot): | 94 | def setup(bot): |
| 130 | bot.add_cog(Jarvis(bot)) | 95 | bot.add_cog(Jarvis(bot)) |
