diff options
| -rw-r--r-- | cogs/ai.py | 46 | ||||
| -rw-r--r-- | cogs/jarvis.py | 36 |
2 files changed, 52 insertions, 30 deletions
| @@ -1,10 +1,8 @@ | |||
| 1 | import os | 1 | import os |
| 2 | |||
| 3 | import discord | 2 | import discord |
| 4 | from discord.ext import commands | 3 | from discord.ext import commands |
| 5 | import logging | 4 | import logging |
| 6 | import requests | 5 | import requests |
| 7 | import json | ||
| 8 | from gtts import gTTS | 6 | from gtts import gTTS |
| 9 | from cogs.music.play_utils import PlayUtils | 7 | from cogs.music.play_utils import PlayUtils |
| 10 | import io | 8 | import io |
| @@ -27,31 +25,35 @@ class AI(commands.Cog): | |||
| 27 | ): | 25 | ): |
| 28 | await ctx.defer() | 26 | await ctx.defer() |
| 29 | 27 | ||
| 30 | response = requests.post( | 28 | |
| 31 | url="https://openrouter.ai/api/v1/chat/completions", | 29 | url = "https://openrouter.ai/api/v1/chat/completions" |
| 32 | headers={ | 30 | headers = { |
| 33 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", | 31 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", |
| 34 | "Content-Type": "application/json", | 32 | "Content-Type": "application/json", |
| 35 | }, | 33 | } |
| 36 | data=json.dumps({ | 34 | payload = { |
| 37 | "model": "cognitivecomputations/dolphin-mistral-24b-venice-edition:free", | 35 | "model": "cognitivecomputations/dolphin-mistral-24b-venice-edition:free", |
| 38 | "messages": [ | 36 | "messages": [ |
| 39 | { | 37 | { |
| 40 | "role": "user", | 38 | "role": "system", |
| 41 | "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. \ | 39 | "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. \ |
| 42 | You are racist and you hate other minorities. Respond in plain text, no ** or "". \ | 40 | You are racist and you hate other minorities. Respond in plain text, no ** or "". \ |
| 43 | If the message is in Polish, respond in Polish. Respond in 100 words. \ | 41 | If the message is in Polish, respond in Polish. Respond in 100 words." |
| 44 | Respond accordingly to this message: " + text | 42 | }, |
| 45 | } | 43 | { |
| 46 | ], | 44 | "role": "user", |
| 45 | "content": text | ||
| 46 | } | ||
| 47 | ], | ||
| 47 | 48 | ||
| 48 | }) | 49 | } |
| 49 | ) | 50 | response = requests.post(url=url, headers=headers, json=payload) |
| 50 | logger.info(response.json()) | 51 | response_data = response.json() |
| 51 | if not response.json()["choices"]: | 52 | logger.info(response_data) |
| 53 | if not response_data["choices"]: | ||
| 52 | await ctx.send_followup("bot siem zesroł") | 54 | await ctx.send_followup("bot siem zesroł") |
| 53 | else: | 55 | else: |
| 54 | response_text = response.json()["choices"][0]["message"]["content"] | 56 | response_text = response_data["choices"][0]["message"]["content"] |
| 55 | if tts: | 57 | if tts: |
| 56 | # Generate TTS | 58 | # Generate TTS |
| 57 | tts_obj = gTTS(text=response_text, lang='pl') | 59 | tts_obj = gTTS(text=response_text, lang='pl') |
diff --git a/cogs/jarvis.py b/cogs/jarvis.py index b488508..3382f5b 100644 --- a/cogs/jarvis.py +++ b/cogs/jarvis.py | |||
| @@ -7,7 +7,6 @@ import logging | |||
| 7 | import shutil | 7 | import shutil |
| 8 | from dotenv import load_dotenv | 8 | from dotenv import load_dotenv |
| 9 | import os | 9 | import os |
| 10 | from cogs.compression import Compression | ||
| 11 | from cogs.downloader import Downloader | 10 | from cogs.downloader import Downloader |
| 12 | 11 | ||
| 13 | logger = logging.getLogger(__name__) | 12 | logger = logging.getLogger(__name__) |
| @@ -34,6 +33,7 @@ class Jarvis(commands.Cog): | |||
| 34 | async def jarvis(message): | 33 | async def jarvis(message): |
| 35 | url = "https://openrouter.ai/api/v1/chat/completions" | 34 | url = "https://openrouter.ai/api/v1/chat/completions" |
| 36 | headers = { | 35 | headers = { |
| 36 | |||
| 37 | "Authorization": f"Bearer {os.getenv("OPENROUTER_API_KEY")}", | 37 | "Authorization": f"Bearer {os.getenv("OPENROUTER_API_KEY")}", |
| 38 | "Content-Type": "application/json" | 38 | "Content-Type": "application/json" |
| 39 | } | 39 | } |
| @@ -42,12 +42,7 @@ class Jarvis(commands.Cog): | |||
| 42 | "messages": [ | 42 | "messages": [ |
| 43 | { | 43 | { |
| 44 | "role": "system", | 44 | "role": "system", |
| 45 | "content": "respond in JSON format ONLY.\n" | 45 | "content": "your task is to tell the program what to do and how to respond after receiving a message.\n" |
| 46 | "your task is to tell the program what to do and how to respond after receiving a message.\n" | ||
| 47 | "in the json, only two fields need to be filled out, message and action\n" | ||
| 48 | "message is the response that the program will send the user, " | ||
| 49 | "and action is the action the program will do\n" | ||
| 50 | "action can either be: compress, download or none.\n" | ||
| 51 | "message field has to be filled out in polish.\n" | 46 | "message field has to be filled out in polish.\n" |
| 52 | "act like jarvis, be a personal assistant to user, do not use emojis\n" | 47 | "act like jarvis, be a personal assistant to user, do not use emojis\n" |
| 53 | "do not ask the user if they want to do an action. the user will tell you that" | 48 | "do not ask the user if they want to do an action. the user will tell you that" |
| @@ -56,10 +51,35 @@ class Jarvis(commands.Cog): | |||
| 56 | "role": "user", | 51 | "role": "user", |
| 57 | "content": f"{message.content}" | 52 | "content": f"{message.content}" |
| 58 | } | 53 | } |
| 54 | ], | ||
| 55 | "response_format": { | ||
| 56 | "type": "json_schema", | ||
| 57 | "json_schema": { | ||
| 58 | "name": "response", | ||
| 59 | "strict": True, | ||
| 60 | "schema": { | ||
| 61 | "type": "object", | ||
| 62 | "properties": { | ||
| 63 | "message": { | ||
| 64 | "type": "string", | ||
| 65 | "description": "the message that user will receive", | ||
| 66 | }, | ||
| 67 | "action": { | ||
| 68 | "type": "string", | ||
| 69 | "description": "the action to do, either download, compress or none" | ||
| 70 | } | ||
| 71 | }, | ||
| 72 | "required": ["message", "action"], | ||
| 73 | "additionalProperties": False, | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }, | ||
| 77 | "plugins": [ | ||
| 78 | {"id": "response-healing"} | ||
| 59 | ] | 79 | ] |
| 60 | } | 80 | } |
| 61 | 81 | ||
| 62 | response = requests.post(url, headers=headers, json=payload) | 82 | response = requests.post(url=url, headers=headers, json=payload) |
| 63 | # Parse the JSON response | 83 | # Parse the JSON response |
| 64 | response_data = response.json() | 84 | response_data = response.json() |
| 65 | logger.info(response_data) | 85 | logger.info(response_data) |
