-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
139 lines (124 loc) · 5.49 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import discord
import openai
import re
import requests
import io
from dotenv import load_dotenv
import os
# Load .env file
load_dotenv()
openai_key= os.environ.get('OPENAI')
channel_id = os.environ.get('CHANNELID')
secret_token = os.environ.get('TOKEN')
client = discord.Client(intents=discord.Intents.all())
openai.api_key = openai_key
@client.event
async def on_member_join(member):
channel = client.get_channel(channel_id)
await channel.send(
"Hi there! Welcome to NAME's server. Here are the available commands:\n"
"!image: ChatGPT to generate an image from your prompt.\n"
"!chat: ChatGPT will answer your prompts.\n"
"!code: ChatGPT will put the answer in a code box.\n"
"!creative: ChatGPT will answer your prompts with more variety and creativtiy (look up ChatGPT temperature settings, it is set to 1 with this command and 0.5 for the other).\n"
"!creativecode: ChatGPT will answer your prompt with both !creative and !code."
)
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!help"):
response = "Here are the available commands:\n"
response += "!image: Generate an image from your prompt.\n"
response += "!chat: Answers prompts.\n"
response += "!code: Answers prompts and puts the answer in a code box (for easy copy and pasting).\n"
response += "!creative: Answers prompts with more variety and creativity (look up ChatGPT temperature settings, it is set to 1 with this command and 0.5 for the other).\n"
response += "!creativecode: Answers prompts with both !creative and !code.\n"
await message.channel.send(response)
elif message.content.startswith("!image"):
prompt = message.content[7:]
loading_message = await message.channel.send("***Generating image, please wait...***")
api_url = "https://api.openai.com/v1/images/generations"
api_key = {"Authorization": f"Bearer {openai.api_key}"}
data = {"model": "image-alpha-001", "prompt": prompt, "num_images": 1, "size": "512x512", "response_format": "url"}
response = requests.post(api_url, headers=api_key, json=data).json()
if 'error' in response:
error_message = response['error']['message']
await message.channel.send(f"An error occurred: {error_message}")
else:
image_url = response['data'][0]['url']
response = requests.get(image_url)
image = response.content
await message.channel.send(file=discord.File(io.BytesIO(image), 'image.jpg'))
await loading_message.delete()
elif message.content.startswith("!code"):
prompt = message.content[6:]
loading_message = await message.channel.send("***Generating response, please wait...***")
response_lines = generate_response(prompt, 0.5)
if isinstance(response_lines, str):
await message.channel.send(response_lines)
else:
response = '\n'.join(response_lines)
await message.channel.send('```' + response + '```')
await loading_message.delete()
elif message.content.startswith("!creative"):
prompt = message.content[10:]
loading_message = await message.channel.send("***Generating response, please wait...***")
response_lines = generate_response(prompt, 1)
if isinstance(response_lines, str):
await message.channel.send(response_lines)
else:
for line in response_lines:
await message.channel.send(line)
await loading_message.delete()
elif message.content.startswith("!creativecode"):
prompt = message.content[4:]
loading_message = await message.channel.send("***Generating response, please wait...***")
response_lines = generate_response(prompt, 1)
if isinstance(response_lines, str):
await message.channel.send(response_lines)
else:
response = '\n'.join(response_lines)
await message.channel.send('```' + response + '```')
await loading_message.delete()
elif message.content.startswith("!chat"):
prompt = message.content[6:]
loading_message = await message.channel.send("***Generating response, please wait...***")
response_lines = generate_response(prompt, 0.5)
if isinstance(response_lines, str):
await message.channel.send(response_lines)
else:
for line in response_lines:
await message.channel.send(line)
await loading_message.delete()
def generate_response(prompt, temperature):
completions = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=2048, # 4000 is the maximum number of tokens for the davinci 003 GPT3 model
n=1,
stop=None,
temperature=temperature,
)
if 'error' in completions:
return completions['error']['message']
else:
message = completions.choices[0].text
if '!code' in prompt:
return message
elif '!creativecode' in prompt:
return message
elif '!image' in prompt:
return message
else:
words = re.split(r'\b', message)
lines = []
current_line = ''
for word in words:
if len(current_line + word) > 75:
lines.append(current_line)
current_line = ''
current_line += word
lines.append(current_line)
return lines
client.run(secret_token)