aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/assistant.py
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-06-03 13:58:55 +0200
committersalmonik <raisersoft@proton.me>2026-06-03 13:58:55 +0200
commit5c66e4ca00a0a7fe32232a90f4b546459c44c774 (patch)
treeef1a5bc7b29e9b32a777cbf826a495570fba9f28 /cogs/assistant.py
parentFix voice reconnect race: reuse VoiceClient instead of destroying it (diff)
downloadsenator-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.py53
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
6import discord 6import discord
7from discord.ext import commands 7from discord.ext import commands
8import httpx
9import logging 8import logging
10import os 9import os
10from cogs.utils.llm import llm_completion
11 11
12logger = logging.getLogger(__name__) 12logger = 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
129def setup(bot): 94def setup(bot):
130 bot.add_cog(Jarvis(bot)) 95 bot.add_cog(Jarvis(bot))