-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendChat.py
More file actions
51 lines (44 loc) · 1.52 KB
/
FriendChat.py
File metadata and controls
51 lines (44 loc) · 1.52 KB
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
# Ted Lawson 2/20/23
import openai
class FriendChat:
def __init__(self, human_name="You", ai_name="Friend"):
self.convo: str = ""
self.human_name: str = human_name
self.ai_name: str = ai_name
# integrate 'grammar correction' api for user input
def start_chat(self):
print("\nYou are now in a chat with your AI friend! Talk about anything.\n")
while True:
self.add_human_response()
self.add_ai_response()
print(f"\nBEGIN CONVO"
f"{self.convo}\n"
f"END CONVO\n")
def get_human_response(self) -> str:
response = input(f"{self.human_name}: ")
if response != "quit":
return response
quit()
def get_ai_response(self):
# convo can not be longer then 4097 token
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"{self.convo}\n",
temperature=.5,
max_tokens=60,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.0,
stop=["You:"]
)
return response
def add_ai_response(self):
response = self.get_ai_response()
text = response["choices"][0]["text"]
print(f"{self.ai_name}: {text}")
self.convo += text
def add_human_response(self):
response = self.get_human_response()
text = f"\n{self.human_name}: {response} " \
f"\n{self.ai_name}: "
self.convo += text