-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieval.py
More file actions
86 lines (66 loc) · 2.48 KB
/
retrieval.py
File metadata and controls
86 lines (66 loc) · 2.48 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
# query_pinecone.py
from pinecone import Pinecone
from langchain_community.embeddings import HuggingFaceEmbeddings
import requests
import os
# Initialize Pinecone and embeddings
os.environ['PINECONE_API_KEY'] = ""
pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
GROQ_API_KEY = ""
index = pc.Index("desi-design-index")
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
def query_pinecone(user_query, top_k=5, namespace=None):
query_vector = embedding_model.embed_query(user_query)
results = index.query(
vector=query_vector,
top_k=top_k,
include_metadata=True,
namespace=namespace
)
for match in results["matches"]:
print(match['metadata']['text'])
return results["matches"]
def generate_answer_with_groq(context_chunks, user_query):
context = "\n\n".join([match['metadata']['text'] for match in context_chunks])
prompt = f"""You are an assistant with deep knowledge of system design.
Use the following context to answer the user's question as clearly and precisely as possible from the context provided in short 2 lines.
### Context:
{context}
### Question:
{user_query}
### Answer:
"""
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "llama-3.3-70b-versatile",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
"temperature": 0.7,
"max_tokens": 512
}
# Send the request to Groq
response = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers=headers,
json=data
)
# Return the generated answer
return response.json()['choices'][0]['message']['content'].strip()
def process_query(file_path, top_k=5, namespace="systemdesign1"):
# Read the query text from a file
with open(file_path, 'r') as file:
query_query = ''.join(file.readlines())
# Get retrieved chunks from Pinecone
retrieved_chunks = query_pinecone(query_query, top_k, namespace)
# Generate an answer using Groq
answer = generate_answer_with_groq(retrieved_chunks, query_query)
# Write the answer to the final output file
with open('final_output.txt', 'w') as finalfile:
finalfile.write(answer.strip())
print("Answer generated and saved in final_output.txt")
process_query('translated_output.txt')