From e91fafdc51cfa2c57b99ef8968273a2d7028a19d Mon Sep 17 00:00:00 2001 From: Paillat Date: Tue, 18 Jul 2023 21:53:07 +0200 Subject: [PATCH] fix(config.py): fix typo in max_uses variable declaration feat(config.py): add support for TENOR_API_KEY environment variable to be able to use Tenor API for sending gifs feat(functionscalls.py): add new function send_a_gif to send a gif in the channel fix(makeprompt.py): add support for calling send_a_gif function in chatgpt_process function --- src/config.py | 12 +++++++----- src/functionscalls.py | 43 +++++++++++++++++++++++++++++++++++++++++++ src/makeprompt.py | 7 +++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/config.py b/src/config.py index b42f2a2..0327d68 100644 --- a/src/config.py +++ b/src/config.py @@ -3,25 +3,27 @@ import sqlite3 import json from dotenv import load_dotenv import os -import openai +# Loading environement variables load_dotenv() + perspective_api_key = os.getenv("PERSPECTIVE_API_KEY") discord_token = os.getenv("DISCORD_TOKEN") webhook_url = os.getenv("WEBHOOK_URL") max_uses: int = 400 +tenor_api_key = os.getenv("TENOR_API_KEY") + +# Logging logging.basicConfig(level=logging.INFO) +# Setting up the google vision api os.environ[ "GOOGLE_APPLICATION_CREDENTIALS" ] = "./../database/google-vision/botator.json" -with open(os.path.abspath(os.path.join("src", "prompts", "functions.json"))) as f: - functions = json.load(f) - +# Defining a debug function def debug(message): - # if the os is windows, we logging.info(message), if if os.name == "nt": logging.info(message) else: diff --git a/src/functionscalls.py b/src/functionscalls.py index 422d264..3a02fe2 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -1,5 +1,10 @@ import discord import aiohttp +import random + +from src.config import tenor_api_key + +tenor_api_url = f"https://tenor.googleapis.com/v2/search?key={tenor_api_key}&q=" functions = [ { @@ -46,6 +51,28 @@ functions = [ "required": ["query"], }, }, + { + "name": "send_a_gif", + "description": "Send a gif in the channel.", + "parameters": { + "type": "object", + "properties": { + "query": { + "type": "string", + "description": "The query to search for, words separated by spaces", + }, + "message": { + "type": "string", + "description": "Your message to send with the gif", + }, + "limit": { + "type": "integer", + "description": "The number of gifs to search for, a random one will be chosen. If the gif is a really specific one, you might want to have a lower limit, to avoid sending a gif that doesn't match your query.", + }, + }, + "required": ["query"], + }, + }, ] server_normal_channel_functions = [ @@ -75,6 +102,11 @@ async def get_final_url(url): final_url = str(response.url) return final_url +async def do_async_request(url): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + response = await response.json() + return response async def add_reaction_to_last_message( message_to_react_to: discord.Message, emoji, message="" @@ -105,3 +137,14 @@ async def create_a_thread( ): msg = await channel_in_which_to_create_the_thread.send(message) await msg.create_thread(name=name) + +async def send_a_gif(message_in_channel_in_wich_to_send: discord.Message, query: str, message: str = "", limit: int = 15): + query = query.replace(" ", "+") + image_url = f"{tenor_api_url}{query}&limit={limit}" + print(image_url) + response = await do_async_request(image_url) + json = response + print(json) + gif_url = random.choice(json["results"])["itemurl"] + message = message + "\n" + gif_url + await message_in_channel_in_wich_to_send.channel.send(message) \ No newline at end of file diff --git a/src/makeprompt.py b/src/makeprompt.py index d5ff262..79f51fc 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -12,6 +12,7 @@ from src.functionscalls import ( reply_to_last_message, send_a_stock_image, create_a_thread, + send_a_gif, functions, server_normal_channel_functions, ) @@ -144,6 +145,12 @@ async def chatgpt_process( "`A server normal text channel only function has been called in a non standard channel. Please retry`", delete_after=10, ) + if name == "send_a_gif": + if arguments.get("query"): + query = arguments.get("query") + reply = arguments.get("message", "") + limit = arguments.get("limit", 15) + await send_a_gif(message, query, reply, limit) if name == "": await message.channel.send( "The function call is empty. Please retry.", delete_after=10