mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 01:06:19 +00:00
🔧 chore(requirements.txt): add 'simpleeval' package for math expression evaluation
🔧 chore(functionscalls.py): add 'evaluate_math' function to evaluate math expressions 🔧 chore(makeprompt.py): refactor 'chatgpt_process' to use 'prepare_messages' function for message preparation
This commit is contained in:
@@ -10,3 +10,4 @@ bs4
|
||||
discord-oauth2.py
|
||||
black
|
||||
orjson # for speed
|
||||
simpleeval
|
||||
@@ -1,8 +1,11 @@
|
||||
import discord
|
||||
import asyncio
|
||||
import orjson
|
||||
import aiohttp
|
||||
import random
|
||||
import time
|
||||
|
||||
from simpleeval import simple_eval
|
||||
from bs4 import BeautifulSoup
|
||||
from src.config import tenor_api_key
|
||||
|
||||
@@ -117,6 +120,20 @@ functions = [
|
||||
"required": ["query"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "evaluate_math",
|
||||
"description": "Get the result of a math expression. Only math expressions are supported, no variables, no functions.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"string": {
|
||||
"type": "string",
|
||||
"description": "The string to evaluate",
|
||||
}
|
||||
},
|
||||
"required": ["string"],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
server_normal_channel_functions = [
|
||||
@@ -253,7 +270,6 @@ async def send_ascii_art_text(
|
||||
asciiiar_url = (
|
||||
f"https://asciified.thelicato.io/api/v2/ascii?text={text}&font={font}"
|
||||
)
|
||||
print(asciiiar_url)
|
||||
ascii_art = await do_async_request(asciiiar_url, json=False)
|
||||
final_message = f"```\n{ascii_art}```\n{message}"
|
||||
if len(final_message) < 2000:
|
||||
@@ -299,15 +315,31 @@ async def send_ascii_art_image(
|
||||
await message_in_channel_in_wich_to_send.channel.send(message)
|
||||
|
||||
|
||||
async def evaluate_math(
|
||||
message_in_channel_in_wich_to_send: discord.Message, arguments: dict
|
||||
):
|
||||
evaluable = arguments.get("string", "")
|
||||
if evaluable == "":
|
||||
raise FuntionCallError("No string provided")
|
||||
try:
|
||||
result = simple_eval(evaluable)
|
||||
except Exception as e:
|
||||
result = f"Error: {e}"
|
||||
return f"Result to math eval of {evaluable}: ```\n{str(result)}```"
|
||||
|
||||
|
||||
async def call_function(message: discord.Message, function_call):
|
||||
name = function_call.get("name", "")
|
||||
if name == "":
|
||||
raise FuntionCallError("No name provided")
|
||||
arguments = function_call.get("arguments", {})
|
||||
# load the function call arguments json
|
||||
arguments = orjson.loads(arguments)
|
||||
if name not in functions_matching:
|
||||
raise FuntionCallError("Invalid function name")
|
||||
function = functions_matching[name]
|
||||
await function(message, arguments)
|
||||
returnable = await function(message, arguments)
|
||||
return returnable
|
||||
|
||||
|
||||
functions_matching = {
|
||||
@@ -318,4 +350,5 @@ functions_matching = {
|
||||
"send_ascii_art_text": send_ascii_art_text,
|
||||
"send_ascii_art_image": send_ascii_art_image,
|
||||
"create_a_thread": create_a_thread,
|
||||
"evaluate_math": evaluate_math,
|
||||
}
|
||||
|
||||
@@ -44,9 +44,7 @@ async def fetch_messages_history(channel: discord.TextChannel, limit, original_m
|
||||
return messages
|
||||
|
||||
|
||||
async def chatgpt_process(
|
||||
self, messages, message: discord.Message, api_key, prompt, model
|
||||
):
|
||||
async def prepare_messages(self, messages, message: discord.Message, api_key, prompt):
|
||||
async def error_call(error=""):
|
||||
try:
|
||||
if error != "":
|
||||
@@ -116,6 +114,20 @@ async def chatgpt_process(
|
||||
)
|
||||
await message.channel.trigger_typing()
|
||||
|
||||
return msgs
|
||||
|
||||
|
||||
async def chatgpt_process(self, msgs, message: discord.Message, api_key, prompt, model):
|
||||
async def error_call(error=""):
|
||||
try:
|
||||
if error != "":
|
||||
await message.channel.send(
|
||||
f"An error occured: {error}", delete_after=10
|
||||
)
|
||||
await message.channel.trigger_typing()
|
||||
except:
|
||||
pass
|
||||
|
||||
response = str()
|
||||
caller = openai_caller()
|
||||
called_functions = (
|
||||
@@ -134,7 +146,16 @@ async def chatgpt_process(
|
||||
response = response["choices"][0]["message"] # type: ignore
|
||||
if response.get("function_call"):
|
||||
function_call = response.get("function_call")
|
||||
await call_function(message, function_call)
|
||||
returned = await call_function(message, function_call)
|
||||
if returned != None:
|
||||
msgs.append(
|
||||
{
|
||||
"role": "function",
|
||||
"content": returned,
|
||||
"name": function_call.get("name"),
|
||||
}
|
||||
)
|
||||
await chatgpt_process(self, msgs, message, api_key, prompt, model)
|
||||
else:
|
||||
content = response.get("content", "")
|
||||
while len(content) != 0:
|
||||
@@ -283,4 +304,5 @@ async def chat_process(self, message):
|
||||
)
|
||||
.replace("[pretend-to-be]", pretend_to_be)
|
||||
)
|
||||
await chatgpt_process(self, messages, message, api_key, prompt, model)
|
||||
emesgs = await prepare_messages(self, messages, message, api_key, prompt)
|
||||
await chatgpt_process(self, emesgs, message, api_key, prompt, model)
|
||||
|
||||
Reference in New Issue
Block a user