diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/assistant.py (renamed from cogs/jarvis.py) | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/cogs/jarvis.py b/cogs/assistant.py index 866700e..71f5150 100644 --- a/cogs/jarvis.py +++ b/cogs/assistant.py | |||
| @@ -10,48 +10,48 @@ import requests | |||
| 10 | import json | 10 | import json |
| 11 | import logging | 11 | import logging |
| 12 | import shutil | 12 | import shutil |
| 13 | from dotenv import load_dotenv | ||
| 14 | import os | 13 | import os |
| 15 | from tempfile import NamedTemporaryFile | 14 | from tempfile import NamedTemporaryFile |
| 16 | from cogs.downloader import Downloader | 15 | from cogs.downloader import Downloader |
| 17 | 16 | ||
| 18 | logger = logging.getLogger(__name__) | 17 | logger = logging.getLogger(__name__) |
| 19 | load_dotenv() | ||
| 20 | 18 | ||
| 21 | class Jarvis(commands.Cog): | 19 | class Jarvis(commands.Cog): |
| 22 | def __init__(self, bot): | 20 | def __init__(self, bot): |
| 23 | self.bot = bot | 21 | self.bot = bot |
| 22 | self.keyword = os.getenv("ASSISTANT_KEYWORD", "jarvis").lower() | ||
| 24 | 23 | ||
| 25 | @commands.Cog.listener() | 24 | @commands.Cog.listener() |
| 26 | async def on_message(self, message): | 25 | async def on_message(self, message): |
| 27 | if message.author == self.bot.user: | 26 | if message.author == self.bot.user: |
| 28 | return | 27 | return |
| 29 | 28 | ||
| 30 | logger.info(message.content) | 29 | logger.info(message.content) |
| 30 | |||
| 31 | if message.mentions: | 31 | if message.mentions: |
| 32 | if message.mentions[0].id == self.bot.user.id: | 32 | if message.mentions[0].id == self.bot.user.id: |
| 33 | await self.jarvis(message) | 33 | await self.assistant(message) |
| 34 | 34 | ||
| 35 | elif "jarvis" in message.content.lower(): | 35 | elif self.keyword in message.content.lower(): |
| 36 | await self.jarvis(message) | 36 | await self.assistant(message) |
| 37 | 37 | ||
| 38 | @staticmethod | 38 | @staticmethod |
| 39 | async def jarvis(message): | 39 | async def assistant(message): |
| 40 | headers = { | 40 | headers = { |
| 41 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", | 41 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", |
| 42 | "Content-Type": "application/json" | 42 | "Content-Type": "application/json" |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | chain = await Jarvis._build_message_chain(message) | 45 | chain = await Jarvis._build_message_chain(message) |
| 46 | messages = Jarvis._build_messages(message, chain) | 46 | messages = Jarvis._build_messages(message, chain) |
| 47 | payload = Jarvis._build_payload(messages) | 47 | payload = Jarvis._build_payload(messages) |
| 48 | 48 | ||
| 49 | response_data = await Jarvis._make_api_request(payload, headers) | 49 | response_data = await Jarvis._make_api_request(payload, headers) |
| 50 | response_message, action = Jarvis._parse_response(response_data) | 50 | response_message, action = Jarvis._parse_response(response_data) |
| 51 | 51 | ||
| 52 | channel = message.channel | 52 | channel = message.channel |
| 53 | await message.reply(response_message) | 53 | await message.reply(response_message) |
| 54 | 54 | ||
| 55 | if action == "download": | 55 | if action == "download": |
| 56 | await Jarvis._handle_download_action(message, channel) | 56 | await Jarvis._handle_download_action(message, channel) |
| 57 | 57 | ||
| @@ -60,7 +60,7 @@ class Jarvis(commands.Cog): | |||
| 60 | chain = [] | 60 | chain = [] |
| 61 | current = message | 61 | current = message |
| 62 | max_depth = 5 | 62 | max_depth = 5 |
| 63 | 63 | ||
| 64 | while current and len(chain) < max_depth: | 64 | while current and len(chain) < max_depth: |
| 65 | chain.append(current) | 65 | chain.append(current) |
| 66 | if not current.reference: | 66 | if not current.reference: |
| @@ -72,9 +72,9 @@ class Jarvis(commands.Cog): | |||
| 72 | current = await current.channel.fetch_message(current.reference.message_id) | 72 | current = await current.channel.fetch_message(current.reference.message_id) |
| 73 | except discord.NotFound: | 73 | except discord.NotFound: |
| 74 | break | 74 | break |
| 75 | 75 | ||
| 76 | return list(reversed(chain)) | 76 | return list(reversed(chain)) |
| 77 | 77 | ||
| 78 | @staticmethod | 78 | @staticmethod |
| 79 | def _build_messages(message, chain=None): | 79 | def _build_messages(message, chain=None): |
| 80 | messages = [{ | 80 | messages = [{ |
| @@ -85,16 +85,16 @@ class Jarvis(commands.Cog): | |||
| 85 | "zachowuj sie jak Jarvis, bądź osobistym asystentem użytkownika, nie używaj emotikon" | 85 | "zachowuj sie jak Jarvis, bądź osobistym asystentem użytkownika, nie używaj emotikon" |
| 86 | "nie pytaj sie użytkownika czy chcesz abyś coś zrobił, użytkownik ci sam powie abyś coś wykonał" | 86 | "nie pytaj sie użytkownika czy chcesz abyś coś zrobił, użytkownik ci sam powie abyś coś wykonał" |
| 87 | }] | 87 | }] |
| 88 | 88 | ||
| 89 | if chain is None: | 89 | if chain is None: |
| 90 | chain = [message] | 90 | chain = [message] |
| 91 | 91 | ||
| 92 | for msg in chain: | 92 | for msg in chain: |
| 93 | messages.append({ | 93 | messages.append({ |
| 94 | "role": "user", | 94 | "role": "user", |
| 95 | "content": f"{msg.author.display_name}: {msg.content}" | 95 | "content": f"{msg.author.display_name}: {msg.content}" |
| 96 | }) | 96 | }) |
| 97 | 97 | ||
| 98 | return messages | 98 | return messages |
| 99 | 99 | ||
| 100 | @staticmethod | 100 | @staticmethod |
| @@ -144,13 +144,13 @@ class Jarvis(commands.Cog): | |||
| 144 | content = response_data['choices'][0]['message']['content'] | 144 | content = response_data['choices'][0]['message']['content'] |
| 145 | inner_json_str = content.strip('```json\n').strip('```').strip() | 145 | inner_json_str = content.strip('```json\n').strip('```').strip() |
| 146 | inner_data = json.loads(inner_json_str) | 146 | inner_data = json.loads(inner_json_str) |
| 147 | 147 | ||
| 148 | response_message = inner_data['message'] | 148 | response_message = inner_data['message'] |
| 149 | action = inner_data['action'] | 149 | action = inner_data['action'] |
| 150 | 150 | ||
| 151 | logger.info(response_message) | 151 | logger.info(response_message) |
| 152 | logger.info(action) | 152 | logger.info(action) |
| 153 | 153 | ||
| 154 | return response_message, action | 154 | return response_message, action |
| 155 | 155 | ||
| 156 | @staticmethod | 156 | @staticmethod |
| @@ -172,7 +172,7 @@ class Jarvis(commands.Cog): | |||
| 172 | async def _process_download_response(response, channel): | 172 | async def _process_download_response(response, channel): |
| 173 | if not isinstance(response, dict): | 173 | if not isinstance(response, dict): |
| 174 | return | 174 | return |
| 175 | 175 | ||
| 176 | if response["status"] == "success": | 176 | if response["status"] == "success": |
| 177 | await Jarvis._handle_successful_download(response, channel) | 177 | await Jarvis._handle_successful_download(response, channel) |
| 178 | else: | 178 | else: |
