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/ai.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 'cogs/ai.py')
| -rw-r--r-- | cogs/ai.py | 75 |
1 files changed, 35 insertions, 40 deletions
| @@ -7,12 +7,14 @@ import os | |||
| 7 | import discord | 7 | import discord |
| 8 | from discord.ext import commands | 8 | from discord.ext import commands |
| 9 | import logging | 9 | import logging |
| 10 | import httpx | ||
| 11 | from gtts import gTTS | 10 | from gtts import gTTS |
| 12 | import io | 11 | import io |
| 12 | from cogs.utils.llm import llm_completion | ||
| 13 | 13 | ||
| 14 | logger = logging.getLogger(__name__) | 14 | logger = logging.getLogger(__name__) |
| 15 | 15 | ||
| 16 | _DEFAULT_SENATOR_MODEL = "cognitivecomputations/dolphin-mistral-24b-venice-edition:free" | ||
| 17 | |||
| 16 | class AI(commands.Cog): | 18 | class 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 | ||
