aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/ai.py
diff options
context:
space:
mode:
authorsalmonik <salmonik@noreply.codeberg.org>2026-06-03 14:00:03 +0200
committersalmonik <salmonik@noreply.codeberg.org>2026-06-03 14:00:03 +0200
commitc027964952bd0d9700a9c83f72fb23bd5bffad3e (patch)
tree1a7f31b08539288eab7fb58bd0ce3b14b2bb0194 /cogs/ai.py
parentMerge pull request 'Voice related fixes, alongside simplifying assistant' (#5... (diff)
parentfeat: add local API support for OpenAI compatible APIs (diff)
downloadsenator-sperma-c027964952bd0d9700a9c83f72fb23bd5bffad3e.tar.gz
senator-sperma-c027964952bd0d9700a9c83f72fb23bd5bffad3e.zip
Merge pull request 'feat: add local API support for OpenAI compatible APIs' (#6) from dev into main
Reviewed-on: https://codeberg.org/salmonik/senator-sperma/pulls/6
Diffstat (limited to '')
-rw-r--r--cogs/ai.py75
1 files changed, 35 insertions, 40 deletions
diff --git a/cogs/ai.py b/cogs/ai.py
index ef2ba7c..dbdb02e 100644
--- a/cogs/ai.py
+++ b/cogs/ai.py
@@ -7,12 +7,14 @@ import os
7import discord 7import discord
8from discord.ext import commands 8from discord.ext import commands
9import logging 9import logging
10import httpx
11from gtts import gTTS 10from gtts import gTTS
12import io 11import io
12from cogs.utils.llm import llm_completion
13 13
14logger = logging.getLogger(__name__) 14logger = logging.getLogger(__name__)
15 15
16_DEFAULT_SENATOR_MODEL = "cognitivecomputations/dolphin-mistral-24b-venice-edition:free"
17
16class AI(commands.Cog): 18class AI(commands.Cog):
17 def __init__(self, bot): 19 def __init__(self, bot):
18 self.bot = bot 20 self.bot = bot
@@ -29,48 +31,41 @@ class AI(commands.Cog):
29 ): 31 ):
30 await ctx.defer() 32 await ctx.defer()
31 33
34 model = (
35 os.getenv("LLM_MODEL")
36 or os.getenv("OPENROUTER_MODEL")
37 or _DEFAULT_SENATOR_MODEL
38 )
39 messages = [
40 {
41 "role": "system",
42 "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. \
43 You are racist and you hate other minorities. Respond in plain text, no markdown tricks. \
44 If the message is in Polish, respond in Polish. Respond in 100 words."
45 },
46 {
47 "role": "user",
48 "content": text
49 }
50 ]
32 51
33 url = "https://openrouter.ai/api/v1/chat/completions" 52 response_text = await llm_completion(messages, model=model)
34 headers = {
35 "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
36 "Content-Type": "application/json",
37 }
38 payload = {
39 "model": "cognitivecomputations/dolphin-mistral-24b-venice-edition:free",
40 "messages": [
41 {
42 "role": "system",
43 "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. \
44 You are racist and you hate other minorities. Respond in plain text, no markdown tricks. \
45 If the message is in Polish, respond in Polish. Respond in 100 words."
46 },
47 {
48 "role": "user",
49 "content": text
50 }
51 ],
52 53
53 } 54 if response_text is None:
54 async with httpx.AsyncClient() as client: 55 await ctx.send_followup("bot siem zesroł")
55 response = await client.post(url=url, headers=headers, json=payload) 56 return
56 response_data = response.json()
57 logger.info(response_data)
58 if not response_data["choices"]:
59 await ctx.send_followup("bot siem zesroł")
60 else:
61 response_text = response_data["choices"][0]["message"]["content"]
62 if tts:
63 tts_obj = gTTS(text=response_text, lang='pl')
64 tts_buffer = io.BytesIO()
65 tts_obj.write_to_fp(tts_buffer)
66 tts_buffer.seek(0)
67 from cogs.voice import get_voice_manager
68 vm = get_voice_manager(self.bot)
69 channel = ctx.author.voice.channel
70 await vm.play_source(channel, discord.FFmpegPCMAudio(tts_buffer), interrupt=False)
71 57
72 else: 58 if tts:
73 await ctx.send_followup(response_text) 59 tts_obj = gTTS(text=response_text, lang='pl')
60 tts_buffer = io.BytesIO()
61 tts_obj.write_to_fp(tts_buffer)
62 tts_buffer.seek(0)
63 from cogs.voice import get_voice_manager
64 vm = get_voice_manager(self.bot)
65 channel = ctx.author.voice.channel
66 await vm.play_source(channel, discord.FFmpegPCMAudio(tts_buffer), interrupt=False)
67 else:
68 await ctx.send_followup(response_text)
74 69
75 70
76 71