From 5c66e4ca00a0a7fe32232a90f4b546459c44c774 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 3 Jun 2026 13:58:55 +0200 Subject: feat: add local API support for OpenAI compatible APIs Signed-off-by: salmonik --- cogs/assistant.py | 53 +++++++++-------------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) (limited to 'cogs/assistant.py') 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 @@ import discord from discord.ext import commands -import httpx import logging import os +from cogs.utils.llm import llm_completion logger = logging.getLogger(__name__) @@ -32,24 +32,20 @@ class Jarvis(commands.Cog): @staticmethod async def assistant(message): - headers = { - "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", - "Content-Type": "application/json" - } - chain = await Jarvis._build_message_chain(message) messages = Jarvis._build_messages(message, chain) - payload = Jarvis._build_payload(messages) - response_data = await Jarvis._safe_api_call(payload, headers) - if response_data is None: + extra = None + api_url = os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1/chat/completions") + if "openrouter" in api_url.lower(): + extra = {"plugins": [{"id": "response-healing"}]} + + response_text = await llm_completion(messages, extra=extra) + if response_text is None: await message.reply("Przepraszam, nie mogę teraz odpowiedzieć. Spróbuj ponownie później.") return - response_message = Jarvis._parse_response(response_data) - - channel = message.channel - await message.reply(response_message) + await message.reply(response_text) @staticmethod async def _build_message_chain(message): @@ -93,38 +89,7 @@ class Jarvis(commands.Cog): return messages - @staticmethod - def _build_payload(messages): - return { - "model": os.getenv("OPENROUTER_MODEL"), - "messages": messages, - "plugins": [{"id": "response-healing"}] - } - - @staticmethod - async def _make_api_request(payload, headers): - async with httpx.AsyncClient(timeout=30.0) as client: - url = "https://openrouter.ai/api/v1/chat/completions" - response = await client.post(url=url, headers=headers, json=payload) - response_data = response.json() - logger.info(response_data) - return response_data - - @staticmethod - async def _safe_api_call(payload, headers): - try: - return await Jarvis._make_api_request(payload, headers) - except httpx.HTTPError as e: - logger.error(f"API request failed: {e}") - return None - - @staticmethod - def _parse_response(response_data): - content = response_data['choices'][0]['message']['content'] - - logger.info(content) - return content def setup(bot): bot.add_cog(Jarvis(bot)) -- cgit v1.2.3