-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLMChat.py
222 lines (203 loc) · 8.68 KB
/
LLMChat.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#LLMChat.py
#
# This unit allows to run the application via GPT APIs or Claude APIs
#
from typing import List
from enum import Enum
# File key.ph contains the OpenAI key and is needed to execute the test.
# In case the import fails, shows instruction for creation of the file.
try:
import key
except():
raise ImportError(
"\n***KEY UNDEFINED!\n\n" +\
"Please create a file named key.py in the app directory " +\
"and put your LLM key(s) there.\n\n" +\
"According to the LLM you enabled, the application expects" +\
" to find:"
"\n\tGPTAIKey = \'<your key>\'" +\
"\nand/or" +\
"\n\tClaudeAIKey = \'<your key>\'" +\
"\nand/or" +\
"\n\tGeminiAIKey = \'<your key>\'" +\
"\nwhere <your key> is the text you got from the vendor site."
)
class Models(Enum):
GPT = 0
CLAUDE = 1
GEMINI = 2
class LLM():
"""
Purpose: This class initializes an instance of the Language Learning Model (LLM) for conversational AI tasks.
Usage:
Instantiate the LLM class with the desired model (Models.GPT, Models.CLAUDE, or Models.GEMINI).
Example: llm_instance = LLM(Models.GPT)
Optionally, you can also pass a string to define the LLM role. The default is an empty string (no role).
Call method DefineRole() to set it later.
Example: llm_instance = LLM(Models.CLAUDE,"You are a personal shopper")
Notes:
- Ensure the required libraries are installed: openai for GPT, anthropic for CLAUDE, and google.generativeai for GEMINI.
- If libraries are missing, the program will exit and prompt the user to install them.
- Ensure the necessary API keys are defined in a file named key.py in the application directory.
- For GPT, the key.py file should contain GPTAIKey.
- For CLAUDE, the key.py file should contain ClaudeAIKey.
- For GEMINI, the key.py file should contain GeminiAIKey.
"""
model: Models = None # LLM to use, no default
exact_model: str = ""
def __init__(self,_model: Models, _role: str = "", _exact_model: str = "") -> None:
"""
Initializes the LLM instance with the specified model.
Parameters:
- model (Models): The LLM model to use (GPT, CLAUDE, or GEMINI).
- exact_model: exact definition of the model to use, e.g. gpt-4o-mini.
If omitted, uses defaults:
gpt-4o-mini for GPT
claude-3-5-sonnet-20241022 for CLAUDE
gemini-1.0-pro for GEMINI
"""
super().__init__()
self.roleInitMessage = _role
# Load libraries and throw an error if they are not installed
try:
if (_model == Models.GPT):
from openai import OpenAI
elif (_model == Models.CLAUDE):
import anthropic
elif (_model == Models.GEMINI):
import google.generativeai
except Exception as ex:
raise ImportError(
"\n***MISSING LIBRARIES!\n\n" +\
"According to the LLM you use, you may need to install\n" +\
"openai and/or anthropic and/or google.generativeai\n"
"\nApplication will now exit."
)
# Dependencies are loaded, create the client
if (_model == Models.GPT):
if (_exact_model != ""):
self.exact_model = _exact_model
else:
self.exact_model = "gpt-4o-mini"
self.LLMClient = OpenAI(api_key = key.GPTAIKey())
elif (_model == Models.CLAUDE):
if (_exact_model != ""):
self.exact_model = _exact_model
else:
self.exact_model="claude-3-5-sonnet-20241022"
self.LLMClient = anthropic.Client(api_key= key.ClaudeAIKey)
elif (_model == Models.GEMINI):
if (_exact_model != ""):
self.exact_model = _exact_model
else:
self.exact_model="gemini-1.0-pro"
google.generativeai.configure(api_key=key.GeminiAIKey)
# Set up the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 0,
"max_output_tokens": 512,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
]
self.LLMClient = google.generativeai.GenerativeModel(
model_name=self.exact_model,
generation_config=generation_config,
safety_settings=safety_settings
)
self.model = _model
def DefineRole(self, role : str):
"""
Set the LLM "role". Note that Gemini currently does not offer this feature.
Parameters:
- role (str): defines the LLM role, like in:
DefineRole("you are a medieval poet")
"""
self.roleInitMessage = role
def LLMCompletion(self, userMessage: str,queue: List[str] = None) :
"""
Call LLM in completion mode.
Parameters:
- userMessage (str): Message to send to LLM.
- queue (List[str]): List containing the user/LLM conversation.
Passing the same queue at each iteration allows to continue the
conversation. The parameter is optional. If omitted, the conversation
will start from scratch every time.
Returns:
- str: LLM's response to the user's message.
"""
LLMmessage : str = ""
internalQueue : List[str] = []
# Reload the key at each run. This allow to have multiple keys randomly selected.
# It is not granted that this may have any practical effect, for example changing
# LLM server or instance.
if (self.model == Models.GPT):
self.LLMClient.api_key = key.GPTAIKey()
elif (self.model == Models.CLAUDE):
self.LLMClient.api_key = key.ClaudeAIKey
elif (self.model == Models.GEMINI):
self.LLMClient.api_key = key.GeminiAIKey
# If a queue is passed, use it. Otherwise, use the internal queue.
# In case the internal queue is used, the conversation will be lost
# at each run. Otherwise, both user and LLM messages will be appended.
if (queue != None):
internalQueue = queue
# If LLM is GPT, then add the system role as first item in the list.
# Claude has a different method for setting it.
if ( (
len(internalQueue) == 0) and
(self.model == Models.GPT) and
(self.roleInitMessage != "")
):
internalQueue.append({"role": "system", "content":self.roleInitMessage})
# Now invoke the completion task with the new user message
if (self.model == Models.GPT):
internalQueue.append({"role":"user","content":userMessage})
response = self.LLMClient.chat.completions.create(
model=self.exact_model,
messages= internalQueue,
# temperature=1.065,
max_tokens=512,
# top_p=1,
# frequency_penalty=0.05,
# presence_penalty=0
)
LLMmessage = response.choices[0].message.content
if (queue != None):
queue.append({"role":"assistant","content":LLMmessage})
elif (self.model == Models.CLAUDE):
internalQueue.append({"role":"user","content":userMessage})
response = self.LLMClient.messages.create(
model=self.exact_model,
system=self.roleInitMessage, # Claude has a specific parameter for role definition
messages= internalQueue,
# temperature=0.065,
max_tokens=512,
# top_p=1,
)
LLMmessage = response.content[0].text
internalQueue.append({"role":"assistant","content":LLMmessage})
elif (self.model == Models.GEMINI):
internalQueue.append({"role":"user","parts":[userMessage]})
convo = self.LLMClient.start_chat(history=internalQueue)
convo.send_message(userMessage)
LLMmessage = convo.last.text
internalQueue.append({"role":"model","content":[LLMmessage]})
return LLMmessage