-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.py
44 lines (36 loc) · 1.06 KB
/
chatgpt.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
import logging
import openai
#
# Class to handle the ChatGPT API
#
class ChatGPT:
def __init__(self, api_key) -> None:
openai.api_key = api_key
def __str__(self) -> str:
return "ChatGPT"
# Generate the content from the prompt with ChatGPT
def generateContent(self, prompt):
# Set the model ID for GPT-3.5-Turbo
model_engine = "gpt-3.5-turbo"
# Send the request to the API
response = openai.ChatCompletion.create(
model=model_engine,
messages=[
{
"role": "user",
"content": prompt
}
]
)
logging.debug(response)
content = response.choices[0].message.content.strip("\"")
return content
# Generate an image from the prompt with ChatGPT
def generateImage(self, prompt):
# Send the request to the API
response = openai.Image.create(
prompt=prompt,
n=1,
size="1024x1024"
)
return response.data[0].url