Tenzin Gyalpo commited on
Commit
1f0db1a
1 Parent(s): e3f487d

Create chat.py

Browse files
Files changed (1) hide show
  1. chat.py +66 -0
chat.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Tuple
3
+
4
+ import openai
5
+
6
+
7
+ class ChatGpt:
8
+ def __init__(self, api_key, max_tokens=4096):
9
+ self.api_key = api_key
10
+ self.max_tokens = max_tokens
11
+ self.message_history = []
12
+ self.total_tokens = 0
13
+
14
+ # Set up the OpenAI API client
15
+ openai.api_key = self.api_key
16
+
17
+ def clear_history(self):
18
+ self.message_history = []
19
+ self.total_tokens = 0
20
+
21
+ def add_message(self, role: str, content: str):
22
+ self.message_history.append({"role": role, "content": content})
23
+ self._truncate_history()
24
+
25
+ def add_system_message(self, content: str):
26
+ self.add_message("system", content)
27
+
28
+ def generate_response(self, user_input: str) -> str:
29
+ self.add_message("user", user_input)
30
+ response = self._call_openai_api(self.message_history)
31
+ self.add_message("assistant", response)
32
+
33
+ return response
34
+
35
+ def _truncate_history(self):
36
+ while self.total_tokens > self.max_tokens:
37
+ if self.message_history[0]["role"] != "system":
38
+ self.message_history.pop(0)
39
+ else:
40
+ break
41
+
42
+ def _call_openai_api(self, messages) -> str:
43
+ response = openai.ChatCompletion.create(
44
+ model="gpt-3.5-turbo", messages=messages
45
+ )
46
+ self.total_tokens += response["usage"]["total_tokens"]
47
+ return response["choices"][0]["message"]["content"].strip()
48
+
49
+
50
+ if __name__ == "__main__":
51
+ chat = ChatGpt(os.getenv("OPENAI_API_KEY"))
52
+
53
+ chat.add_system_message("The assistant can answer questions and tell jokes.")
54
+ user_input = "Tell me a joke."
55
+ user_msg, bot_response = chat.generate_response(user_input)
56
+ assert user_msg == user_input
57
+ print("User:", user_msg)
58
+ print("Assistant:", bot_response)
59
+ print("Total Tokens:", chat.total_tokens)
60
+
61
+ user_input = "another one"
62
+ user_msg, bot_response = chat.generate_response(user_input)
63
+ assert user_msg == user_input
64
+ print("User:", user_msg)
65
+ print("Assistant:", bot_response)
66
+ print("Total Tokens:", chat.total_tokens)