-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.py
More file actions
38 lines (19 loc) · 811 Bytes
/
chatgpt.py
File metadata and controls
38 lines (19 loc) · 811 Bytes
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
from openai import OpenAI
from openai import APIError
api_Key = "Fill with your api key"
class Chatbot:
def __init__(self, api_key: str) -> None:
self.name = 'weather bot'
self.client = OpenAI(api_key=api_key)
def generate_text(self, msg: str, role:str="user") -> str:
try:
completion = self.client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": role, "content": "Give me suggestions based on the data below"},
{"role": role, "content": msg}
]
)
return completion.choices[0].message.content
except APIError as e:
print(f"Message:e.message")