-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.py
50 lines (31 loc) · 1.51 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
45
46
47
48
49
50
import openai
from dataframe import CreateDataFrame
class ChatGPT:
openai.api_key = "API_KEY"
def __init__(self) -> None:
self.df_instance = CreateDataFrame()
self.list_str = self.get_books_list()
self.messages = [
{"role": "system", "content": "You are a knowledgeable assistant."},
{"role": "user", "content": f"Here is a list of books:\n{self.list_str}\nPlease tell me the topic of each book on this list."}
]
def get_books_list(self):
book_titles_list = self.df_instance.make_titles_list()
# create a string with the book titles, each on a new line
books_list_str = "\n".join(book_titles_list)
return books_list_str
def get_response(self):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages,
)
response = completion['choices'][0]['message']['content']
self.messages.append({"role": "assistant", "content": response})
return response
def assistant_response(self):
self.get_response()
self.messages.append({"role": "user", "content": 'Thank you. Can you summarize each topic into 2 to 6 words.'})
second_response = self.get_response()
self.messages.append({"role": "user", "content": 'Please reword each topic using less than 6 words each.'})
third_response = self.get_response()
return second_response, third_response