forked from beat-b/CProjectG6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview_bot.py
More file actions
151 lines (118 loc) · 4.55 KB
/
review_bot.py
File metadata and controls
151 lines (118 loc) · 4.55 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
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
# [i] #
# [i] Imports #
# [i] #
from openai import OpenAI
from util import local_settings
import pickle
# [i] #
# [i] OpenAI API #
# [i] #
class GPT_Helper:
def __init__(self, OPENAI_API_KEY: str, system_behavior: str = "", model: str = "gpt-3.5-turbo"):
"""
Initialize the GPT_Helper class.
Parameters:
OPENAI_API_KEY (str): API key for OpenAI.
system_behavior (str): System behavior message to be included.
model (str): GPT model to use (default is "gpt-3.5-turbo").
"""
self.client = OpenAI(api_key=OPENAI_API_KEY)
self.messages = []
self.model = model
if system_behavior:
self.messages.append({
"role": "system",
"content": system_behavior
})
def get_completion(self, prompt: str, temperature: int = 0) -> str:
"""
Get completion from the GPT model.
Parameters:
prompt (str): User prompt for the model.
temperature (int): Temperature parameter for randomness (default is 0).
Returns:
str: Completed message from the model.
"""
self.messages.append({"role": "user", "content": prompt})
completion = self.client.chat.completions.create(
model=self.model,
messages=self.messages,
temperature=temperature,
)
self.messages.append(
{
"role": "assistant",
"content": completion.choices[0].message.content
}
)
return completion.choices[0].message.content
# [i] #
# [i] ReviewChatBot #
# [i] #
class ReviewChatBot:
"""
Generate a response using Language Models (LLMs).
"""
def __init__(self, system_behavior: str):
"""
Initialize the ReviewChatBot class.
Parameters:
system_behavior (str): System behavior message to be included.
"""
self._system_behavior = system_behavior
self._username = None
self.engine = GPT_Helper(
OPENAI_API_KEY=local_settings.OPENAI_API_KEY,
system_behavior=system_behavior
)
def set_username(self, username: str) -> None:
"""
Set the username for the chatbot.
Parameters:
username (str): User's username.
"""
self._username = username
def generate_response(self, message: str) -> str:
"""
Generate a response for the given message.
Parameters:
message (str): User's input message.
Returns:
str: Chatbot's response.
"""
user_message = f"{self._username}: {message}" if self._username else message
response = self.engine.get_completion(user_message)
return response
def __str__(self) -> str:
"""
Return a string representation of the ReviewChatBot instance.
Returns:
str: String representation.
"""
shift = " "
class_name = str(type(self)).split('.')[-1].replace("'>", "")
return f"🤖 {class_name}."
@property
def memory(self) -> list:
"""
Get the conversation history.
Returns:
list: List of messages in the conversation.
"""
return self.engine.messages
@property
def system_behavior(self) -> str:
"""
Get the system behavior.
Returns:
str: System behavior message.
"""
return self._system_behavior
@system_behavior.setter
def system_behavior(self, system_config: str) -> None:
"""
Set the system behavior.
Parameters:
system_config (str): New system behavior message.
"""
self._system_behavior = system_config