-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_engine.py
More file actions
69 lines (53 loc) · 1.63 KB
/
Copy pathrag_engine.py
File metadata and controls
69 lines (53 loc) · 1.63 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
import faiss
import numpy as np
import pickle
from sentence_transformers import SentenceTransformer
import requests
# Load everything once
model = SentenceTransformer('all-MiniLM-L6-v2')
index = faiss.read_index("faiss_index.bin")
with open("metadata.pkl", "rb") as f:
metadata = pickle.load(f)
def retrieve(query, k=5):
q_embedding = model.encode([query])
D, I = index.search(np.array(q_embedding), k)
results = []
for idx, dist in zip(I[0], D[0]):
item = metadata[idx]
item["score"] = float(1 / (1 + dist)) # confidence score
results.append(item)
return results
def generate_response(query, retrieved_docs):
context = "\n".join([
f"{doc['id']} - {doc['title']}: {doc['scope']}"
for doc in retrieved_docs
])
prompt = f"""
You are an expert in BIS standards.
User Query:
{query}
Relevant Standards:
{context}
Task:
Recommend the top 3-5 most relevant BIS standards and explain briefly why each applies.
"""
try:
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "mistral",
"prompt": prompt,
"stream": False
}
)
return response.json()["response"]
except:
# fallback if LLM not running
fallback = "Top Matching Standards:\n"
for doc in retrieved_docs[:3]:
fallback += f"\n{doc['id']} - {doc['title']}\n→ Relevant to {query}\n"
return fallback
def get_recommendations(query):
retrieved = retrieve(query)
answer = generate_response(query, retrieved)
return retrieved, answer