-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlanguage_model.py
More file actions
390 lines (327 loc) · 13.7 KB
/
language_model.py
File metadata and controls
390 lines (327 loc) · 13.7 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os
import re
import sys
import time
from abc import ABC, abstractmethod
import anthropic
import google.generativeai as genai
import requests
from dotenv import load_dotenv
from groq import Groq
from llamaapi import LlamaAPI
from openai import OpenAI
from transformers import AutoModelForCausalLM, AutoTokenizer
from utils import clean_list, clean_string
# NOTE: Change MAX_RETRIES based on max number of attempts for calling APIs
MAX_RETRIES = 40
load_dotenv()
num_es = sys.argv[3]
class LanguageModel(ABC):
"""
Abstract base class for language model-based reasoning pipelines.
Provides a structure for interacting with LLM APIs to retrieve evidence,
extract entity relationships, find relevant entities, combine relationships,
and generate answers.
Subclasses must implement the `call_api` method, which interfaces with the actual language model.
"""
@abstractmethod
def call_api(self, prompt, system_message=None):
"""
Abstract method to call the underlying language model API.
Args:
prompt (str): The prompt to send to the model.
system_message (str, optional): An optional system-level instruction.
Returns:
str: The response from the language model.
"""
pass
def retrieve_evidences(self, question):
"""
Retrieves a fixed number of textual evidences relevant to answering the given question.
Evidence is ordered from most to least relevant.
"""
system_message = '''You are an assistant in charge of generating factual evidences that aid in solving the provided question.
Provide only the evidences with no additional remarks. Do not give the answer away directly in the evidence.
'''
retrieve_evidence_prompt = f'''Generate {num_es} evidences that pertain to answering the following question: {question}
The evidences must be ordered from most relevant to least relevant to answering the question.
Separate each evidence with '$$$'.
'''
evidences = []
while len(evidences) != num_es:
evidences = self.call_api(retrieve_evidence_prompt, system_message)
evidences = evidences.split("$$$")
evidences = [x.strip() for x in evidences if len(x) > 1]
evidences = clean_list(evidences)
if len(evidences) > num_es:
evidences = evidences[:num_es]
return evidences
def extract_triplets(self, evidences):
"""
Extracts entity-relationship triplets from a list of evidences using the language model.
"""
system_message = f'''You are an assistant in charge of extracting entities and entity relationships from various statements.
For each statement provided, extract the most important relationship between two entities in the statement.
In total there must be {num_es} relationships extracted.
You must end each entry with $$$.
For example, for the statement "Sagittarius A* aligns with the dynamical center of the galaxy", the output should be:
Entity 1:Sagittarius A*
Entity 2: galaxy
Relationship: Sagittarius aligns with the dynamical center of the galaxy
$$$
'''
extract_triplets_prompt = f'''
Statements: {evidences}
'''
entity1_indices = []
entity2_indices = []
relationship_indices = []
while len(entity1_indices) != num_es or len(entity2_indices) != num_es or len(relationship_indices) != num_es:
response = self.call_api(extract_triplets_prompt, system_message)
entity1_indices = [m.start() for m in re.finditer('Entity 1:', response)]
entity2_indices = [m.start() for m in re.finditer('Entity 2:', response)]
relationship_indices = [m.start() for m in re.finditer('Relationship:', response)]
entity1_vals = []
entity2_vals = []
relationships = []
for i in range(len(entity1_indices)):
ent1_ind = entity1_indices[i]
ent2_ind = entity2_indices[i]
rel_ind = relationship_indices[i]
entity1 = response[ent1_ind+len('Entity 1:'):ent2_ind]
entity2 = response[ent2_ind+len('Entity 2:'):rel_ind]
if i != len(entity1_indices) - 1:
rel = response[rel_ind+len('Relationship:'):entity1_indices[i+1]]
else:
rel = response[rel_ind+len('Relationship:'):]
entity1_vals.append(entity1.strip())
entity2_vals.append(entity2.strip())
relationships.append(rel.strip())
entity1_vals = clean_list(entity1_vals)
entity2_vals = clean_list(entity2_vals)
relationships = clean_list(relationships)
return entity1_vals, entity2_vals, relationships
def find_relevant_entities(self, question, all_entities):
"""
Identifies which entities from a list are mentioned in a question.
"""
system_message = '''You are in charge of finding all entities in the provided entity list that are mentioned in the provided question.
You must separate each value in your response with a comma.
'''
prompt = f'''Question: {question}
Entity List: {all_entities}
'''
response = self.call_api(prompt, system_message)
relevant_entities = response.split(',')
return relevant_entities
def combine_relationships(self, relationships):
"""
Combines multiple relationship statements into a concise, coherent summary.
"""
system_message = '''You are an assistant in charge of combining the provided statements into one summarized statement. Be concise without losing any of the information.
'''
prompt = f'''Statements: {relationships}'''
response = self.call_api(prompt, system_message)
return response
def generate_answer(self, question):
"""
Generates a direct answer to the question using the language model.
"""
system_message = '''You are a teacher in charge of correctly answering questions.
'''
prompt = f'''Question: {question}'''
response = self.call_api(prompt, system_message)
return response
class GeminiRetriever(LanguageModel):
def __init__(self, model='gemini-1.5-flash'):
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
self.model = genai.GenerativeModel(model)
def call_api(self, prompt, system_message):
attempt = 0
while attempt < MAX_RETRIES:
try:
response = self.model.generate_content(prompt)
return response.text
except Exception as e:
print(f"An error occurred: {str(e)}")
attempt += 1
time.sleep(10)
print(f"Failed to get chat completion after {MAX_RETRIES} attempts")
return None
class GPTRetriever(LanguageModel):
def __init__(self, model='gpt-3.5-turbo'):
self.client = OpenAI(api_key=os.environ['OPENAI_KEY'])
self.model=model
def call_api(self, prompt, system_message):
client = self.client
messages=[
{
"role": "system",
"content": system_message,
},
{
"role": "user",
"content": prompt,
}
]
attempt = 0
while attempt < MAX_RETRIES:
try:
response = client.chat.completions.create(messages=messages,
model=self.model)
return response.choices[0].message.content
except Exception as e:
print(f"An error occurred: {str(e)}")
attempt += 1
time.sleep(10)
print(f"Failed to get chat completion after {MAX_RETRIES} attempts")
return None
class GroqRetriever(LanguageModel):
'''
Using Llama API from https://console.groq.com/docs/text-chat
Available models: https://console.groq.com/docs/models
'''
def __init__(self, model='llama3-8b-8192'):
self.client = Groq(
api_key=os.environ['GROQ_KEY'],
)
self.model = model
def call_api(self, prompt, system_message):
messages=[
{
"role": "system",
"content": system_message,
},
{
"role": "user",
"content": prompt,
}
]
attempt = 0
while attempt < MAX_RETRIES:
try:
response = self.client.chat.completions.create(
messages=messages,
model=self.model
)
return response.choices[0].message.content
except Exception as e:
print(f"An error occurred: {str(e)}")
attempt += 1
time.sleep(10)
print(f"Failed to get chat completion after {MAX_RETRIES} attempts")
return None
class LlamaRetriever(LanguageModel):
def __init__(self, model='llama3.3-70b'):
self.model = model
self.client = LlamaAPI(os.environ['LLAMA_KEY'])
def call_api(self, prompt, system_message):
request = {
"model": self.model,
"messages": [
{
"role": "user",
"content": f'{system_message}\n{prompt}'
},
],
}
attempt = 0
while attempt < MAX_RETRIES:
try:
response = self.client.run(request)
text_response = response.json()['choices'][0]['message']['content']
return clean_string(text_response)
except Exception as e:
print(f"Error occurred: {str(e)}")
attempt += 1
time.sleep(10)
class LlamaWithWeightsRetriever(LanguageModel):
def __init__(self, model='Llama-2-7b-chat-hf'):
self.tokenizer = AutoTokenizer.from_pretrained(f"meta-llama/{model}")
self.model = AutoModelForCausalLM.from_pretrained(f"meta-llama/{model}")
def call_api(self, prompt, system_message):
inputs = self.tokenizer(prompt, return_tensors="pt")
output = self.model.generate(
input_ids=inputs['input_ids'],
temperature=0.7, # lower = more deterministic
num_beams=5 # higher = better quality, slower
)
generated_text = self.tokenizer.decode(output[0], skip_special_tokens=True)
return generated_text
class ClaudeRetriever(LanguageModel):
def __init__(self, model=''):
self.client = anthropic.Anthropic(api_key=os.environ['CLAUDE_KEY'])
def call_api(self, prompt, system_message):
attempt = 0
while attempt < MAX_RETRIES:
try:
response = self.client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
temperature=0,
system=system_message,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
)
return clean_string(response.content[0].text)
except Exception as e:
print(f"An error occurred: {str(e)}")
attempt += 1
time.sleep(10)
print(f"Failed to get chat completion after {MAX_RETRIES} attempts")
return None
class DeepSeekRetriever(LanguageModel):
def __init__(self, model='deepseek-chat'):
self.client = OpenAI(api_key=os.environ['DEEPSEEK_KEY'], base_url="https://api.deepseek.com")
self.model = model
def call_api(self, prompt, system_message):
attempt = 0
while attempt < MAX_RETRIES:
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": prompt},
],
stream=False
)
response_text = response.choices[0].message.content
return clean_string(response_text)
except Exception as e:
print(f"Error occurred: {str(e)}")
attempt += 1
time.sleep(10)
print(f"Failed to get chat completion after {MAX_RETRIES} attempts")
return None
def get_retriever():
"""
Returns an instance of a retriever class based on the language model name provided as a command-line argument.
The function reads the first command-line argument (sys.argv[1]) to determine which retriever to use.
Supported options:
- 'gpt' → returns GPTRetriever()
- 'gemini' → returns GeminiRetriever()
- 'claude' → returns ClaudeRetriever()
- 'llama' → returns LlamaRetriever()
- 'deepseek' → returns DeepSeekRetriever()
"""
llm_name = sys.argv[1]
if llm_name == 'gpt':
return GPTRetriever()
elif llm_name == 'gemini':
return GeminiRetriever()
elif llm_name == 'claude':
return ClaudeRetriever()
elif llm_name == 'llama':
return LlamaRetriever()
elif llm_name == 'deepseek':
return DeepSeekRetriever()