-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
51 lines (43 loc) · 1.62 KB
/
Copy pathquery.py
File metadata and controls
51 lines (43 loc) · 1.62 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
"""
CLI helper for issuing ad-hoc questions against the RAG stack.
Used for:
- quick debugging
- manual evaluation
- demo runs
"""
from __future__ import annotations
from config import DEFAULT_HYBRID_ALPHA, DEFAULT_RETRIEVAL_STRATEGY, TOP_K_RESULTS
from context import build_context, generate_answer
from embedding import get_embedder
from retrieval import retrieve_chunks
from db import close_weaviate_client
def ask(
question: str,
top_k: int = TOP_K_RESULTS,
*,
strategy: str | None = None,
alpha: float | None = None,
) -> str:
model = get_embedder()
print("Retrieving relevant chunks...")
results = retrieve_chunks(
model,
question,
top_k=top_k,
strategy=strategy or DEFAULT_RETRIEVAL_STRATEGY,
alpha=alpha if alpha is not None else DEFAULT_HYBRID_ALPHA,
)
context = build_context(results)
print("CONTEXT USED:\n", context[:800], "...\n")
print("Generating answer...")
source_ids = [chunk.get("doc_id") or chunk.get("source_file") for chunk in results]
answer = generate_answer(question, context, source_ids=source_ids)
print("ANSWER:\n", answer)
return answer
if __name__ == "__main__":
try :
# ask("What are the main claims about intellectual property in these documents?")
# ADD asks here : ask("Summarize the key points related to patent law.")
ask("How do Drahos and Braithwaite (2002) describe the role of TRIPS and TRIPS-plus agreements in shifting the balance of power over medicines between multinational pharmaceutical companies and developing countries?")
finally:
close_weaviate_client()