-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag.py
More file actions
105 lines (86 loc) · 3.05 KB
/
Copy pathrag.py
File metadata and controls
105 lines (86 loc) · 3.05 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
"""
RAG Pipeline complet
Combină Retriever + Generator într-un singur query flow.
"""
import time
from retriever import Retriever
from generator import generate_answer, generate_answer_stream
from config import TOP_K
class RAGPipeline:
"""Pipeline complet: întrebare → retrieval → generation → răspuns."""
def __init__(self):
print("Inițializăm RAG Pipeline ...")
self.retriever = Retriever()
print("RAG Pipeline gata!\n")
def query(self, question: str, top_k: int = TOP_K) -> dict:
"""
Procesează o întrebare end-to-end.
Returns:
dict cu: answer, sources, timing
"""
start = time.time()
# Pas 1: Retrieval
context, sources = self.retriever.build_context(question, top_k)
retrieval_time = time.time() - start
# Pas 2: Generation
gen_start = time.time()
answer = generate_answer(question, context)
generation_time = time.time() - gen_start
return {
"question": question,
"answer": answer,
"sources": [
{
"text": s["text"][:200],
"source": s["metadata"].get("source", "?"),
"score": round(s["score"], 3),
}
for s in sources
],
"timing": {
"retrieval_ms": round(retrieval_time * 1000),
"generation_ms": round(generation_time * 1000),
"total_ms": round((time.time() - start) * 1000),
},
}
def query_stream(self, question: str, top_k: int = TOP_K):
"""
Procesează o întrebare cu streaming pentru răspuns.
Yields:
dict-uri cu type="source" sau type="token"
"""
context, sources = self.retriever.build_context(question, top_k)
# Trimite sursele mai întâi
yield {
"type": "sources",
"sources": [
{
"text": s["text"][:200],
"source": s["metadata"].get("source", "?"),
"score": round(s["score"], 3),
}
for s in sources
],
}
# Apoi stream-ul de tokens
for token in generate_answer_stream(question, context):
yield {"type": "token", "content": token}
if __name__ == "__main__":
rag = RAGPipeline()
questions = [
"How do I create a basic FastAPI application?",
"What is dependency injection in FastAPI?",
"How do I handle errors?",
]
for q in questions:
print(f"\n{'='*60}")
print(f"Q: {q}")
print("-" * 60)
result = rag.query(q)
print(f"A: {result['answer']}")
print(f"\nSurse:")
for s in result["sources"]:
print(f" - {s['source']} (scor: {s['score']})")
print(f"\nTimp: {result['timing']['total_ms']}ms "
f"(retrieval: {result['timing']['retrieval_ms']}ms, "
f"generation: {result['timing']['generation_ms']}ms)")