-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaskwikidata.py
327 lines (274 loc) · 11.3 KB
/
askwikidata.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
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
import glob
import json
import os
import requests
import datetime
import time
import pandas as pd
from tqdm import tqdm
from langchain.embeddings.huggingface import HuggingFaceBgeEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from annoy import AnnoyIndex
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from generate import LLM
class AskWikidata:
df = pd.DataFrame()
local_llm = None
def __init__(
self,
chunk_overlap=0,
chunk_size=768,
context_chunks=7,
embedding_model_name="BAAI/bge-small-en-v1.5",
index_trees=10,
qa_model_url="https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1",
reranker_model_name="BAAI/bge-reranker-base",
retrieval_chunks=64,
cache_file=None,
):
self.chunk_overlap = chunk_overlap
self.chunk_size = chunk_size
self.context_chunks = context_chunks
self.embedding_model_name = embedding_model_name
self.index_trees = index_trees
self.qa_model_url = qa_model_url
self.reranker_model_name = reranker_model_name
self.retrieval_chunks = retrieval_chunks
if not cache_file:
emn = embedding_model_name.replace("/", "-")
self.cache_file = f"cache-{chunk_size}-{chunk_overlap}-{emn}.json"
else:
self.cache_file = cache_file
self.device = "cpu"
if torch.cuda.is_available():
print("CUDA available to torch.")
self.device = "cuda"
def setup(self):
self.load_models()
if not self.load_cache():
self.read_data()
self.create_embeds()
self.save_cache()
self.create_index()
def load_models(self):
print("Loading models...")
self.embedding_model = HuggingFaceBgeEmbeddings(
model_name=self.embedding_model_name,
model_kwargs={"device": self.device},
encode_kwargs={"normalize_embeddings": True},
query_instruction="Represent this sentence for searching relevant passages: ",
)
self.rerank_tokenizer = AutoTokenizer.from_pretrained(self.reranker_model_name)
self.rerank_model = AutoModelForSequenceClassification.from_pretrained(
self.reranker_model_name
)
self.rerank_model.to(self.device)
if not "https://" in self.qa_model_url:
self.local_llm = LLM(self.qa_model_url)
def read_data(self):
directory_path = "./text_representations"
texts = []
metas = []
files = glob.glob(os.path.join(directory_path, "*.txt"))
print("Loading text representations...")
for file_path in files:
with open(file_path, "r") as file:
texts.append(file.read())
file_name = file_path.split("/")[-1]
q_id = file_name.split(".")[0]
metas.append({"source": f"https://www.wikidata.org/wiki/{q_id}"})
print(f" {len(files)} files loaded.")
print("Creating chunks...")
text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n"],
chunk_size=self.chunk_size,
chunk_overlap=self.chunk_overlap,
length_function=len,
)
chunks = text_splitter.create_documents(texts, metadatas=metas)
self.df = pd.DataFrame(columns=["id", "text", "source"])
for i, c in enumerate(chunks):
self.df.loc[i] = [i, c.page_content, c.metadata["source"]]
print(f" {len(self.df)} chunks.")
def create_embeds(self):
embeds = []
print("Creating embeddings...")
for _, row in tqdm(self.df.iterrows(), total=len(self.df)):
text = str(row["text"])
embeddings = self.embedding_model.embed_documents([text])
embeds.append(embeddings[0])
self.df["embeddings"] = embeds
def save_cache(self):
print(f"Saving dataframe to {self.cache_file}...")
# start = time.time()
self.df.to_json(self.cache_file)
# print(f" {int(time.time() - start)} seconds.")
def load_cache(self):
if os.path.exists(self.cache_file):
print(f"Loading dataframe from {self.cache_file}...")
# start = time.time()
self.df = pd.read_json(self.cache_file)
# print(f" {int(time.time() - start)} seconds.")
return True
return False
def create_index(self):
print("Creating embedding index...")
embed_dims = len(self.df.iloc[0]["embeddings"])
self.index = AnnoyIndex(embed_dims, "angular")
for i, e in enumerate(self.df["embeddings"]):
self.index.add_item(i, e)
self.index.build(self.index_trees)
def retrieve(self, query: str) -> pd.DataFrame:
print("Retrieving...")
query_embed = self.embedding_model.embed_query(query)
query_embed_float = [float(value) for value in query_embed]
nns = self.index.get_nns_by_vector(
query_embed_float, self.retrieval_chunks, include_distances=True
)
nns_ids = nns[0]
nns_distances = nns[1]
ret = self.df.iloc[nns_ids].copy()
ret["retrieve_distance"] = nns_distances
ret = ret.sort_values("retrieve_distance")
return ret
def rerank(self, query: str, df: pd.DataFrame):
print("Reranking...")
start = time.time()
pairs = []
for _, n in df.iterrows():
pairs.append([query, n["text"]])
with torch.no_grad():
inputs = self.rerank_tokenizer(
pairs,
padding=True,
truncation=True,
return_tensors="pt",
max_length=512,
).to(self.device)
# TODO: do we truncate? do we loose information here?
scores = (
self.rerank_model(**inputs, return_dict=True)
.logits.view(
-1,
)
.float()
)
df["rank"] = scores.to("cpu")
ret = df.sort_values(by="rank", ascending=False).head(self.context_chunks)
seconds = time.time() - start
return ret, seconds
def print_data(self):
pd.set_option("display.max_rows", None)
print(self.df)
def context(self, df: pd.DataFrame):
context = ""
for index in reversed(df.index):
row = df.loc[index]
context += row["text"] + "\n"
return context.replace("\n\n", "\n")
def llm_generate(self, query: str, df: pd.DataFrame):
context = self.context(df)
prompt_func = None
if "llama" in self.qa_model_url:
prompt_func = self.llama_prompt
elif "mistral" in self.qa_model_url:
prompt_func = self.mistral_prompt
elif "Qwen" in self.qa_model_url:
prompt_func = self.qwen25_prompt
else:
raise Exception(f"unknown qa_model_name {self.qa_model_url}")
if "huggingface.co" in self.qa_model_url:
return self.hf_generate(query, context, self.qa_model_url, prompt_func)
else:
return self.local_generate(query, context, prompt_func)
def llm_generate_plain(self, query: str):
prompt_func = None
if "llama" in self.qa_model_url:
prompt_func = self.llama_prompt
elif "mistral" in self.qa_model_url:
prompt_func = self.mistral_prompt
elif "Qwen" in self.qa_model_url:
prompt_func = self.qwen25_prompt
else:
raise Exception(f"unknown qa_model_name {self.qa_model_url}")
# TODO: DRY (see llm_generate)
if "huggingface.co" in self.qa_model_url:
return self.hf_generate(query, None, self.qa_model_url, prompt_func)
else:
return self.local_generate(query, None, prompt_func)
def ask(self, query: str):
retrieved = self.retrieve(query)
reranked, _ = self.rerank(query, retrieved)
answer = self.llm_generate(query, reranked)
sources = set(reranked["source"])
sources_bullet_list = "Sources:\n" + "\n".join(f"- {s}" for s in sources)
return answer + "\n\n" + sources_bullet_list
def system_from_context(self, context):
system = (
"You are answering questions for a given context. "
+ "Answer based on information from the given context only, but do not mention the context in your response. "
+ "If the answer is not in the context say that you do not know the answer. "
+ "Only give the answer, do not provide any further explanations. "
+ "Do not mention the context. "
+ "Dates and timespans will be presented to you in YYYY-MM-DD format. Interpret those. "
+ "For reference, today is the "
+ f"{datetime.date.today().strftime('%Y-%m-%d')}. "
+ "Respond with the most current information unless requested otherwise.\n"
+ f"\nCONTEXT:\n{context}"
)
return system
DEFAULT_SYSTEM = (
"You are a helpful assistent. Please answer the following question. "
)
def llama_prompt(self, text, system=DEFAULT_SYSTEM):
return f"<s>[INST] <<SYS>>\n{system}\n<</SYS>>\n\n{text} [/INST]"
def mistral_prompt(self, text, system=DEFAULT_SYSTEM):
return f"<s>[INST] {system}\n\nQUESTION: {text} [/INST]"
def qwen25_prompt(self, text, system=DEFAULT_SYSTEM):
return f"<|im_start|>system\n{system}\n<|im_end|>\n<|im_start|>assistant\nQUESTION: {text}\n<|im_end|>\n"
def hf_generate(self, question, context, model_url, prompt_func):
huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
if huggingface_api_key is None:
raise Exception("HUGGINGFACE_API_KEY is None.")
print("Generating...")
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
prompt = ""
if context:
system = self.system_from_context(context)
prompt = prompt_func(question, system)
else:
prompt = prompt_func(question)
# print(f"Sending the following prompt to {model_url}:")
# print(prompt)
# print(f"({len(prompt)} chars, about {int(len(prompt)/3)} tokens)")
response = requests.post(
model_url,
headers=headers,
json={
"inputs": prompt,
"parameters": {
# max is 250 https://huggingface.co/docs/api-inference/detailed_parameters#text-generation-task
"max_new_tokens": 250,
},
},
)
# print(response.json())
try:
return response.json()[0]["generated_text"].replace(prompt, "").strip()
except Exception as e:
print(f"An unexpected error occurred: {e}")
print(response)
raise e
def local_generate(self, question, context, prompt_func):
if self.local_llm is None:
raise Exception("no local llm loaded")
print("Generating...")
prompt = ""
if context:
system = self.system_from_context(context)
prompt = prompt_func(question, system)
else:
prompt = prompt_func(question)
result = self.local_llm(prompt)
return result