Repository navigation
Expand file tree
/
Copy pathgroq_rag.py
More file actions
176 lines (119 loc) · 4.16 KB
/
Copy pathgroq_rag.py
File metadata and controls
176 lines (119 loc) · 4.16 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import chromadb
from dotenv import load_dotenv
from groq import Groq
from sentence_transformers import SentenceTransformer
# --------------------------------------------------
# Configuration
# --------------------------------------------------
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY is not set")
groq_client = Groq(api_key=GROQ_API_KEY)
# --------------------------------------------------
# Embedding model
# --------------------------------------------------
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
# --------------------------------------------------
# ChromaDB
# --------------------------------------------------
chroma_client = chromadb.PersistentClient(path="./chroma")
collection = chroma_client.get_or_create_collection(
name="company_docs"
)
# --------------------------------------------------
# Documents
# --------------------------------------------------
documents = [
"""
Refund Policy
Customers can request a refund within 7 days of receiving
their order. The product must be unused and in its original
condition. Refunds are processed within 5 business days after
approval.
""",
"""
Shipping Policy
Standard shipping usually takes 3 to 5 business days.
Express shipping takes 1 to 2 business days.
Shipping is available throughout Bangladesh.
"""
]
# --------------------------------------------------
# Add documents to ChromaDB
# --------------------------------------------------
embeddings = embedding_model.encode(documents).tolist()
print( f"Adding documents to ChromaDB...{len(documents)} documents added.")
print(f"Embeddings shape: {len(embeddings)} x {len(embeddings[0])} x {len(embeddings[1])})")
# print(f"Sample embedding for first document: {embeddings[0]}")
collection.upsert(
ids=["refund-policy", "shipping-policy"],
documents=documents,
embeddings=embeddings
)
# --------------------------------------------------
# Retrieval
# --------------------------------------------------
def retrieve_documents(query: str, top_k: int = 3):
query_embedding = embedding_model.encode([query]).tolist()
print(f"Query embedding shape: {len(query_embedding)} x {len(query_embedding[0])}")
# print(f"Sample query embedding: {query_embedding[0]}")
results = collection.query(
query_embeddings=query_embedding,
n_results=top_k
)
return results["documents"][0]
# --------------------------------------------------
# Generate answer using Groq
# --------------------------------------------------
def generate_answer(query: str, context: list[str]):
# print(f"Retrieved context: {context}")
for c in context:
print(f"Context snippet: {c[:]}...\n\n") # Print first 100 characters of each context snippet
context_text = "\n\n".join(context)
prompt = f"""
You are a helpful assistant.
Answer the user's question using ONLY the provided context.
If the answer cannot be found in the context, say:
"I don't know based on the provided information."
Context:
----------------
{context_text}
----------------
Question:
{query}
Answer:
"""
response = groq_client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0
)
return response.choices[0].message.content
# --------------------------------------------------
# RAG pipeline
# --------------------------------------------------
def ask(query: str):
# 1. Retrieve relevant documents
context = retrieve_documents(query)
# 2. Generate answer using retrieved context
answer = generate_answer(query, context)
return answer
# --------------------------------------------------
# Interactive chat
# --------------------------------------------------
if __name__ == "__main__":
print("RAG chatbot started.")
print("Type 'exit' to quit.\n")
while True:
query = input("You: ")
if query.lower() == "exit":
break
answer = ask(query)
print(f"\nAssistant: {answer}\n")