make its own http session and reusable header
This commit is contained in:
parent
35558cf155
commit
10ac529323
1 changed files with 15 additions and 29 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
#sparkytron 3000 plugin
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import asyncio
|
import asyncio
|
||||||
import aiofiles
|
import aiofiles
|
||||||
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
|
|
||||||
|
|
@ -18,7 +18,15 @@ class ChatGPT(commands.Cog):
|
||||||
self.premium_role = 1200943915579228170
|
self.premium_role = 1200943915579228170
|
||||||
self.folder_setup()
|
self.folder_setup()
|
||||||
self.remind_me_loop.start()
|
self.remind_me_loop.start()
|
||||||
|
self.http_session = self.create_aiohttp_session()
|
||||||
|
|
||||||
|
def create_aiohttp_session(self):
|
||||||
|
return aiohttp.ClientSession(
|
||||||
|
headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def folder_setup(self):
|
def folder_setup(self):
|
||||||
try:
|
try:
|
||||||
|
|
@ -82,11 +90,6 @@ class ChatGPT(commands.Cog):
|
||||||
return await user.send(reminder_dict["response"])
|
return await user.send(reminder_dict["response"])
|
||||||
|
|
||||||
async def answer_question(self, topic, model="gpt-3.5-turbo"):
|
async def answer_question(self, topic, model="gpt-3.5-turbo"):
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
|
||||||
}
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"model": model,
|
"model": model,
|
||||||
"messages": [{"role": "user", "content": topic}]
|
"messages": [{"role": "user", "content": topic}]
|
||||||
|
|
@ -95,7 +98,7 @@ class ChatGPT(commands.Cog):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with self.http_session.post(url, json=data) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
response = response_data['choices'][0]['message']['content']
|
response = response_data['choices'][0]['message']['content']
|
||||||
return response
|
return response
|
||||||
|
|
@ -195,10 +198,6 @@ class ChatGPT(commands.Cog):
|
||||||
await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
|
await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
|
||||||
|
|
||||||
async def dalle_api_call(self, prompt, model="dall-e-2", quality="standard", size="1024x1024"):
|
async def dalle_api_call(self, prompt, model="dall-e-2", quality="standard", size="1024x1024"):
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
|
||||||
}
|
|
||||||
data = {
|
data = {
|
||||||
"model": model,
|
"model": model,
|
||||||
"prompt": prompt,
|
"prompt": prompt,
|
||||||
|
|
@ -209,7 +208,7 @@ class ChatGPT(commands.Cog):
|
||||||
url = "https://api.openai.com/v1/images/generations"
|
url = "https://api.openai.com/v1/images/generations"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with self.http_session.post(url, json=data) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
response = response_data['data'][0]['url']
|
response = response_data['data'][0]['url']
|
||||||
return response
|
return response
|
||||||
|
|
@ -221,7 +220,7 @@ class ChatGPT(commands.Cog):
|
||||||
async def download_image(self, url, destination):
|
async def download_image(self, url, destination):
|
||||||
if url == "Error occurred in dalle":
|
if url == "Error occurred in dalle":
|
||||||
return
|
return
|
||||||
async with self.bot.http_session.get(url) as resp:
|
async with self.http_session.get(url) as resp:
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
f = await aiofiles.open(destination, mode='wb')
|
f = await aiofiles.open(destination, mode='wb')
|
||||||
await f.write(await resp.read())
|
await f.write(await resp.read())
|
||||||
|
|
@ -282,11 +281,6 @@ class ChatGPT(commands.Cog):
|
||||||
async def looker(self, ctx):
|
async def looker(self, ctx):
|
||||||
image_link = ctx.message.content.split(" ", maxsplit=2)[1]
|
image_link = ctx.message.content.split(" ", maxsplit=2)[1]
|
||||||
question = ctx.message.content.split(" ", maxsplit=2)[2]
|
question = ctx.message.content.split(" ", maxsplit=2)[2]
|
||||||
|
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
|
||||||
}
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"model": "gpt-4-vision-preview",
|
"model": "gpt-4-vision-preview",
|
||||||
|
|
@ -297,7 +291,7 @@ class ChatGPT(commands.Cog):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with self.http_session.post(url, json=data) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
self.bot.logger.debug(response_data)
|
self.bot.logger.debug(response_data)
|
||||||
answer = response_data['choices'][0]['message']['content']
|
answer = response_data['choices'][0]['message']['content']
|
||||||
|
|
@ -395,10 +389,6 @@ class ChatGPT(commands.Cog):
|
||||||
#todo above line is do not react to self, make this work programatically
|
#todo above line is do not react to self, make this work programatically
|
||||||
system_msg = "Send only an emoji as a discord reaction to the following chat message"
|
system_msg = "Send only an emoji as a discord reaction to the following chat message"
|
||||||
message = ctx.content[0]
|
message = ctx.content[0]
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
|
||||||
}
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"model": "gpt-3.5-turbo",
|
"model": "gpt-3.5-turbo",
|
||||||
|
|
@ -408,7 +398,7 @@ class ChatGPT(commands.Cog):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with self.http_session.post(url, json=data) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
reaction = response_data['choices'][0]['message']['content'].strip()
|
reaction = response_data['choices'][0]['message']['content'].strip()
|
||||||
if is_emoji(reaction):
|
if is_emoji(reaction):
|
||||||
|
|
@ -422,10 +412,6 @@ class ChatGPT(commands.Cog):
|
||||||
async with ctx.channel.typing():
|
async with ctx.channel.typing():
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
prompt = f"You are a {channel_vars['personality']} chat bot named Sparkytron 3000 created by @phixxy.com. Your personality should be {channel_vars['personality']}. You are currently in a {channel_vars['channel_topic']} chatroom. The message history is: {chat_history_string}"
|
prompt = f"You are a {channel_vars['personality']} chat bot named Sparkytron 3000 created by @phixxy.com. Your personality should be {channel_vars['personality']}. You are currently in a {channel_vars['channel_topic']} chatroom. The message history is: {chat_history_string}"
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
|
||||||
}
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"model": "gpt-3.5-turbo",
|
"model": "gpt-3.5-turbo",
|
||||||
|
|
@ -435,7 +421,7 @@ class ChatGPT(commands.Cog):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with self.http_session.post(url, json=data) as resp:
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
response = response_data['choices'][0]['message']['content']
|
response = response_data['choices'][0]['message']['content']
|
||||||
if "Sparkytron 3000:" in response[0:17]:
|
if "Sparkytron 3000:" in response[0:17]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue