-
Notifications
You must be signed in to change notification settings - Fork 13
/
rag.py
468 lines (375 loc) · 15.1 KB
/
rag.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#
# Credit to https://github.com/kyleavery for the contribution
#
from __future__ import annotations
import asyncio
import os
import time
import typing as t
import uuid
from functools import wraps
from pathlib import Path
import click
import litellm
from elasticsearch import Elasticsearch
from elasticsearch import helpers as es_helpers
from loguru import logger
from pydantic import BaseModel
from typing_extensions import ParamSpec
import rigging as rg
from rigging import logging
if t.TYPE_CHECKING:
from elastic_transport import ObjectApiResponse
# Constants
VECTOR_INDEX = "dreadnode"
EMBEDDING_DIMENSIONS = 1024
SYSTEM_PROMPT = """\
You are an assistant that answers questions about rigging: a lightweight LLM interaction framework for Python.
"""
REF_DIRS = ["docs", "docs/topics", "examples"]
REF_EXTS = [".md", ".py"]
CHUNK_SIZE = 2500
OVERLAP_SIZE = 500
# Helpers
P = ParamSpec("P")
R = t.TypeVar("R")
def wrap_async_to_sync(func: t.Callable[P, t.Coroutine[t.Any, t.Any, R]]) -> t.Callable[P, R]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
return asyncio.run(func(*args, **kwargs))
return wrapper
class RawDocument(t.TypedDict):
title: str
content: str
class Document(RawDocument):
slice_start: int
slice_end: int
def chunk_document(document: RawDocument) -> t.Generator[Document, None, None]:
content = document["content"]
title = document["title"]
for i in range(0, len(content), CHUNK_SIZE - OVERLAP_SIZE):
chunk_content = content[i : i + CHUNK_SIZE]
yield {
"title": f"{title}_{i}",
"content": chunk_content,
"slice_start": i,
"slice_end": i + len(chunk_content),
}
def read_documents_from_path(directory: Path, extensions: list[str]) -> t.Generator[Document, None, None]:
if not directory.is_dir():
raise ValueError(f"{directory} is not a directory")
if not directory.exists():
raise ValueError(f"{directory} does not exist")
for filename in os.listdir(directory):
if any(filename.endswith(ext) for ext in extensions):
filepath = os.path.join(directory, filename)
with open(filepath, encoding="utf-8") as file:
yield from chunk_document({"title": filename, "content": file.read()})
def read_documents(directories: list[Path], extensions: list[str]) -> list[Document]:
documents: list[Document] = []
for directory in directories:
documents.extend(read_documents_from_path(directory, extensions))
return documents
# Models
class Reference(BaseModel):
id: str
title: str
score: float
content: str
slice_start: int
slice_end: int
class ReferenceDB:
def __init__(
self,
embedding_model_id: str,
host: str,
username: str,
password: str,
*,
max_connect_retries: int = 3,
retry_wait: int = 5,
):
self.embedding_model_id = embedding_model_id
self.host = host
self.username = username
self.password = password
self.es_client = self._connect(max_connect_retries, retry_wait)
logger.success("Connected to Elasticsearch")
def _connect(self, max_retries: int, retry_wait: int) -> Elasticsearch:
for _ in range(max_retries):
try:
es_client = Elasticsearch(
hosts=[self.host],
basic_auth=(self.username, self.password),
verify_certs=False,
ssl_show_warn=False,
)
es_client.info()
return es_client
except Exception as e:
logger.error(f"Failed to connect to Elasticsearch: {e}")
logger.info(f"Retrying in {retry_wait} seconds...")
time.sleep(retry_wait)
raise Exception("Max retries exceeded while trying to connect to Elasticsearch")
def index_exists(self, index: str) -> bool:
return self.es_client.indices.exists(index=index).meta.status == 200
def populate_db(self, directories: list[Path], extensions: list[str]) -> None:
try:
self.es_client.indices.delete(index=VECTOR_INDEX)
except Exception:
pass
index_mapping = {
"properties": {
"content_vector": {
"type": "dense_vector",
"dims": EMBEDDING_DIMENSIONS,
"index": "true",
"similarity": "cosine",
},
"id": {"type": "keyword"},
"content": {"type": "text"},
"title": {"type": "text"},
"slice_start": {"type": "integer"},
"slice_end": {"type": "integer"},
}
}
self.es_client.indices.create(index=VECTOR_INDEX, mappings=index_mapping)
logger.success(f"Created index '{VECTOR_INDEX}'")
reference_docs = read_documents(directories, extensions)
logger.info(f"Populating database with {len(reference_docs)} chunks ...")
actions = [
{
"_index": VECTOR_INDEX,
"_source": {
"id": str(uuid.uuid4()),
"title": source["title"],
"content": source["content"],
"content_vector": self.generate_embeddings(source["content"]),
"slice_start": source["slice_start"],
"slice_end": source["slice_end"],
},
}
for source in reference_docs
]
es_helpers.bulk(self.es_client, actions)
logger.success("Done.")
def generate_embeddings(self, input_: str) -> list[float]:
response = litellm.embedding(
input=input_,
model=self.embedding_model_id,
dimensions=(EMBEDDING_DIMENSIONS if "mistral" not in self.embedding_model_id else None),
)
return response.data[-1]["embedding"] # type: ignore
def handle_elastic_results(self, response: ObjectApiResponse[t.Any]) -> list[Reference]:
try:
return [
Reference(
id=hit["_source"]["id"],
title=hit["_source"]["title"],
content=hit["_source"]["content"],
score=hit["_score"],
slice_start=hit["_source"]["slice_start"],
slice_end=hit["_source"]["slice_end"],
)
for hit in response["hits"]["hits"]
]
except KeyError as e:
raise ValueError("Missing expected key in response data") from e
def fuzzy_search(self, search_phrase: str, max_results: int) -> list[Reference]:
"""
Implementation copied from Nemesis:
https://github.com/SpecterOps/Nemesis/blob/84d5986f759161f60dc2e5b538ec88d95b289e43/cmd/nlp/nlp/services/text_search.py#L218
"""
query = {
"bool": {
"must": [
{
"multi_match": {
"query": search_phrase,
"fields": ["content", "title"],
"fuzziness": "AUTO",
}
}
]
}
}
try:
response = self.es_client.search(index=VECTOR_INDEX, query=query, size=max_results)
except Exception as e:
raise Exception("Exception during fuzzy search") from e
return self.handle_elastic_results(response)
def semantic_search(self, search_phrase: str, max_results: int) -> list[Reference]:
"""
Implementation copied from Nemesis:
https://github.com/SpecterOps/Nemesis/blob/84d5986f759161f60dc2e5b538ec88d95b289e43/cmd/nlp/nlp/services/text_search.py#L243
"""
query = {
"field": "content_vector",
"query_vector": self.generate_embeddings(search_phrase),
"k": max_results,
"num_candidates": max_results,
}
try:
es_response = self.es_client.search(index=VECTOR_INDEX, knn=query, size=max_results)
except Exception as e:
raise Exception("Exception during semantic search") from e
return self.handle_elastic_results(es_response)
def reciprocal_rank_fusion(
self, fuzzy_results: list[Reference], semantic_results: list[Reference]
) -> list[Reference]:
"""
Implementation copied from Nemesis:
https://github.com/SpecterOps/Nemesis/blob/84d5986f759161f60dc2e5b538ec88d95b289e43/cmd/nlp/nlp/services/text_search.py#L301
"""
# original 40/60 split was passing on good results
k_fuzzy = 50
k_semantic = 50
max_fuzzy_score = max((result.score for result in fuzzy_results), default=10.0)
unique_refs_dict = {result.id: result for result in fuzzy_results + semantic_results}
fused_scores: dict[str, float] = {}
for rank, result in enumerate(sorted(fuzzy_results, key=lambda x: x.score, reverse=True), 1):
fused_scores[result.id] = fused_scores.get(result.id, 0) + 1 / (k_fuzzy + rank)
for rank, result in enumerate(sorted(semantic_results, key=lambda x: x.score, reverse=True), 1):
fused_scores[result.id] = fused_scores.get(result.id, 0) + 1 / (k_semantic + rank)
combined_results_sorted = sorted(unique_refs_dict.values(), key=lambda x: fused_scores[x.id], reverse=True)
for result in combined_results_sorted:
result.score = round(
((fused_scores[result.id] - 0.00625) * max_fuzzy_score) / (0.0407 - 0.00625),
3,
)
logger.trace(
f"Combined results:\n{chr(10).join([f' {ref.title} ({ref.score})' for ref in combined_results_sorted])}"
)
return combined_results_sorted
def hybrid_search(self, search_phrase: str, max_refs: int) -> str:
"""
Prompt based on RAGnarok:
https://github.com/GhostPack/RAGnarok/blob/69d4a2d333011b3df6785b6a292b08d4c61a3742/ragnarok/pages/1_RAGnarok_Chat.py#L300
"""
logger.info("Fuzzy search ...")
fuzzy_results = self.fuzzy_search(search_phrase, max_refs)
logger.info("Semantic search ...")
semantic_results = self.semantic_search(search_phrase, max_refs)
logger.info("Reranking ...")
refs = self.reciprocal_rank_fusion(fuzzy_results, semantic_results)
sorted_refs = sorted(refs, key=lambda r: r.score, reverse=True)[:max_refs]
for ref in sorted_refs:
logger.info(f" |- {ref.title} ({ref.score})")
refs_xml = "".join(
[
f"""\
<ref>
<title>{ref.title}</title>
<score>{ref.score}</score>
<content>{ref.content}</content>
</ref>
"""
for ref in sorted_refs
]
)
return f"""\
# References
Your answers should utilize references to generate an accurate and informative response.
Each of the following references starts with the reference title, followed by a similarity score reflecting the documents's relevance to the overall prompt, finally followed by the reference content.
Similarity scores represent the assistant's confidence in the reference's relevance to the prompt. Higher scores indicate higher perceived similarity. Utilize the information in all references to enhance your answer, but if any references contain contradictory information use the information that appears to be more relevant and up to date.
If no references contain relevant information, tell the user that you were unable to find an answer.
<references>
{refs_xml}
</references>
"""
# Entrypoints
@click.group(context_settings={"show_default": True})
@click.option(
"-e",
"--embedding-id",
type=str,
default="mistral/mistral-embed",
help="LiteLLM embedding model identifier",
)
@click.option("-eH", "--elastic-host", envvar="ES_HOST", required=True, help="Elasticsearch host (ES_HOST)")
@click.option("-eU", "--elastic-username", envvar="ES_USER", required=True, help="Elasticsearch username (ES_USER)")
@click.option("-eP", "--elastic-password", envvar="ES_PASS", required=True, help="Elasticsearch password (ES_PASS)")
@click.option(
"--log-level",
type=click.Choice(logging.LogLevelList),
default="info",
)
@click.option("--log-file", type=click.Path(path_type=Path), default="rag.log")
@click.option(
"--log-file-level",
type=click.Choice(logging.LogLevelList),
default="trace",
)
@click.pass_context
def cli(
ctx: click.Context,
embedding_id: str,
elastic_host: str,
elastic_username: str,
elastic_password: str,
log_level: logging.LogLevelLiteral,
log_file: Path,
log_file_level: logging.LogLevelLiteral,
) -> None:
"""
Rigging example for simple retrieval-augmented generation (RAG).
"""
ctx.ensure_object(dict)
ctx.obj["embedding_id"] = embedding_id
ctx.obj["elastic_host"] = elastic_host
ctx.obj["elastic_username"] = elastic_username
ctx.obj["elastic_password"] = elastic_password
logging.configure_logging(log_level, log_file, log_file_level)
@cli.command()
@click.argument("query")
@click.option(
"-g",
"--generator-id",
type=str,
default="anthropic/claude-3-sonnet-20240229",
required=True,
help="Rigging generator identifier (gpt-4, mistral/mistral-medium, etc.)",
)
@click.option(
"-r",
"--max-refs",
type=int,
default=3,
help="Maximum number of references to send to the LLM",
)
@click.pass_context
@wrap_async_to_sync
async def search(ctx: click.Context, query: str, generator_id: str, max_refs: int) -> None:
"""
Perform a RAG-augmented query with the Elasticsearch database.
"""
embedding_id = ctx.obj["embedding_id"]
elastic_host = ctx.obj["elastic_host"]
elastic_username = ctx.obj["elastic_username"]
elastic_password = ctx.obj["elastic_password"]
ref_db = ReferenceDB(embedding_id, elastic_host, elastic_username, elastic_password)
if not ref_db.index_exists(VECTOR_INDEX):
logger.error(f"Elasticsearch index '{VECTOR_INDEX}' is empty. Please run the 'populate' command first.")
return
generator = rg.get_generator(generator_id)
chat = await generator.chat(
[
{"role": "system", "content": SYSTEM_PROMPT + "\n\n" + ref_db.hybrid_search(query, max_refs)},
{"role": "user", "content": query},
],
).run()
logger.success(f"Response:\n{chat.last.content}\n")
@cli.command()
@click.pass_context
def populate(ctx: click.Context) -> None:
"""
Populate the Elasticsearch database with example data.
"""
embedding_id = ctx.obj["embedding_id"]
elastic_host = ctx.obj["elastic_host"]
elastic_username = ctx.obj["elastic_username"]
elastic_password = ctx.obj["elastic_password"]
ref_db = ReferenceDB(embedding_id, elastic_host, elastic_username, elastic_password)
ref_db.populate_db([Path(p) for p in REF_DIRS], REF_EXTS)
if __name__ == "__main__":
cli()