-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_utils.py
More file actions
61 lines (47 loc) · 1.74 KB
/
rag_utils.py
File metadata and controls
61 lines (47 loc) · 1.74 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
"""
RAG utilities for retrieving relevant medical information from Pinecone.
"""
import os
from pinecone import Pinecone
from sentence_transformers import SentenceTransformer
from dotenv import load_dotenv
load_dotenv()
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
INDEX_NAME = os.getenv("PINECONE_INDEX_NAME", "doctor-assist")
if not PINECONE_API_KEY:
raise ValueError("PINECONE_API_KEY not found in environment variables. Please check your .env file.")
pc = Pinecone(api_key=PINECONE_API_KEY)
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
def get_relevant_context(query, top_k=5):
"""
Retrieve relevant medical context from Pinecone based on query.
Args:
query: The search query
top_k: Number of relevant chunks to retrieve
Returns:
List of relevant text chunks
"""
try:
index = pc.Index(INDEX_NAME)
query_embedding = embedding_model.encode(query).tolist()
results = index.query(
vector=query_embedding,
top_k=top_k,
include_metadata=True
)
contexts = []
for match in results.matches:
if match.metadata and 'text' in match.metadata:
contexts.append(match.metadata['text'])
return contexts
except Exception as e:
print(f"Error retrieving context: {e}")
return []
def format_context_for_prompt(contexts):
"""Format retrieved contexts into a prompt-friendly string."""
if not contexts:
return "No relevant medical information found."
formatted = "Relevant Medical Information:\n\n"
for i, context in enumerate(contexts, 1):
formatted += f"{i}. {context}\n\n"
return formatted