-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_bot_while_loop.py
152 lines (131 loc) · 4.9 KB
/
chat_bot_while_loop.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import openai
import tiktoken
import datetime
import numpy as np
import pickle
from openai import OpenAI
# openai.api_key = 'YOUR_OPENAI_API_KEY'
today = datetime.datetime.now()
day = datetime.datetime.now().strftime('%A')
tokens = 0
tokens = 0
import tiktoken
def num_tokens_from_string(string: str, encoding_name: str) -> int:
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
#''''''''''''''''''''''' without function '''''''''''''''''''''''''''
client = OpenAI(
# api_key defaults to os.environ.get("OPENAI_API_KEY")
api_key="YOUR_OPENAI_API_KEY",
)
def get_completion_from_messages(messages, model='gpt-3.5-turbo', temperature=0):
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature
)
return response.choices[0].message.content
class user:
user_dict = {}
id = ''
context = []
chatbot_name = ''
# constructor function
def __init__(self, id, chatbot_name):
self.id = id
self.chatbot_name = chatbot_name
self.context = [
{'role': 'system', 'content': 'Today is ' + str(today) + ' and the day is ' + day + '. You are chatting with a best friend. Enjoy the conversation.'},
{'role': 'assistant', 'content': f'My name is {self.chatbot_name}'},
{'role': 'user', 'content': 'Tell me about your day, buddy.'},
{'role': 'assistant', 'content': 'My day has been exciting. How did you survive your day without me? Hehehe.'}
]
self.user_dict = {}
def create_user(self):
user_ids = self.user_dict.values()
if self.id not in user_ids:
self.user_dict[self.id] = self.context
return print("User created sucessfuly!")
else:
return print("User ID already exits!")
def get_user_dict(self):
return self.user_dict
def get_user_context(self):
if self.id in self.user_dict.keys():
return self.user_dict[self.id]
else:
return print('User not found!')
def save_context(self, context):
path = './'
with open(path +self.id +'.pkl', 'wb') as fp:
pickle.dump(context, fp)
print('It was great talking to you! See ya later!')
return print(context, path)
def load_saved_dict(self):
if os.path.exists(self.id + '.pkl'):
with open(self.id +'.pkl', 'rb') as fp:
dict = pickle.load(fp)
self.user_dict = dict
return dict
else:
return print('User not found!')
def is_user(self):
if os.path.exists('./' + self.id + '.pkl'):
return self.load_saved_dict()
else:
return 0
def delete_previous_context(self):
context = self.context
return context
id = input('Enter user id: ')
name = input('chatbot name: ')
client = user(id, name)
is_user = client.is_user()
if is_user == 0:
print('New User')
client.create_user()
context = client.get_user_context()
prompt = 'hello'
prompt = input()
while prompt != 'exit':
context.append({'role':'user', 'content':f"{prompt}"})
for i in context:
for j in i.values():
count = num_tokens_from_string(j,'cl100k_base')
tokens = tokens + count
if tokens >= 15500:
context = client.delete_previous_context()
print('i\'m a little tired, how about we continue our conversation later!')
print(tokens)
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
print('Assistant:', response)
prompt = input()
if prompt == 'exit':
client.save_context(context)
client.load_saved_dict()
else:
print('Existing User')
context = client.load_saved_dict()
prompt = 'hello'
prompt = input()
while prompt != 'exit':
context.append({'role':'user', 'content':f"{prompt}"})
for i in context:
for j in i.values():
count = num_tokens_from_string(j,'cl100k_base')
tokens = tokens + count
if tokens >= 15500:
context = client.delete_previous_context()
print('i\'m a little tired, how about we continue our conversation later!')
break
print(tokens)
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
print('Assistant:', response)
prompt = input()
if prompt == 'exit':
client.save_context(context)
client.load_saved_dict()