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
This commit is contained in:
Paillat
2023-07-18 21:53:07 +02:00
parent 12f13ca6c4
commit e91fafdc51
3 changed files with 57 additions and 5 deletions

View File

@@ -3,25 +3,27 @@ import sqlite3
import json import json
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
import openai
# Loading environement variables
load_dotenv() load_dotenv()
perspective_api_key = os.getenv("PERSPECTIVE_API_KEY") perspective_api_key = os.getenv("PERSPECTIVE_API_KEY")
discord_token = os.getenv("DISCORD_TOKEN") discord_token = os.getenv("DISCORD_TOKEN")
webhook_url = os.getenv("WEBHOOK_URL") webhook_url = os.getenv("WEBHOOK_URL")
max_uses: int = 400 max_uses: int = 400
tenor_api_key = os.getenv("TENOR_API_KEY")
# Logging
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# Setting up the google vision api
os.environ[ os.environ[
"GOOGLE_APPLICATION_CREDENTIALS" "GOOGLE_APPLICATION_CREDENTIALS"
] = "./../database/google-vision/botator.json" ] = "./../database/google-vision/botator.json"
with open(os.path.abspath(os.path.join("src", "prompts", "functions.json"))) as f: # Defining a debug function
functions = json.load(f)
def debug(message): def debug(message):
# if the os is windows, we logging.info(message), if
if os.name == "nt": if os.name == "nt":
logging.info(message) logging.info(message)
else: else:

View File

@@ -1,5 +1,10 @@
import discord import discord
import aiohttp 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 = [ functions = [
{ {
@@ -46,6 +51,28 @@ functions = [
"required": ["query"], "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 = [ server_normal_channel_functions = [
@@ -75,6 +102,11 @@ async def get_final_url(url):
final_url = str(response.url) final_url = str(response.url)
return final_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( async def add_reaction_to_last_message(
message_to_react_to: discord.Message, emoji, 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) msg = await channel_in_which_to_create_the_thread.send(message)
await msg.create_thread(name=name) 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)

View File

@@ -12,6 +12,7 @@ from src.functionscalls import (
reply_to_last_message, reply_to_last_message,
send_a_stock_image, send_a_stock_image,
create_a_thread, create_a_thread,
send_a_gif,
functions, functions,
server_normal_channel_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`", "`A server normal text channel only function has been called in a non standard channel. Please retry`",
delete_after=10, 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 == "": if name == "":
await message.channel.send( await message.channel.send(
"The function call is empty. Please retry.", delete_after=10 "The function call is empty. Please retry.", delete_after=10