Update app.py
Browse files
app.py
CHANGED
|
@@ -235,43 +235,51 @@ def generate_answer(query, history):
|
|
| 235 |
for chunk, meta in zip(relevant_chunks, metadata)
|
| 236 |
])
|
| 237 |
|
| 238 |
-
# Build
|
| 239 |
-
|
| 240 |
-
if history and len(history) > 0:
|
| 241 |
-
recent_history = history[-3:] if len(history) > 3 else history
|
| 242 |
-
history_parts = []
|
| 243 |
-
for h in recent_history:
|
| 244 |
-
if isinstance(h, (list, tuple)) and len(h) >= 2:
|
| 245 |
-
history_parts.append(f"User: {h[0]}\nAssistant: {h[1]}")
|
| 246 |
-
history_context = "\n".join(history_parts)
|
| 247 |
|
| 248 |
-
#
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
Previous conversation:
|
| 252 |
-
{history_context}
|
| 253 |
-
|
| 254 |
-
Context from documents:
|
| 255 |
-
{context}
|
| 256 |
-
|
| 257 |
-
Current question: {query}
|
| 258 |
|
| 259 |
Instructions:
|
| 260 |
- Answer based strictly on the provided context
|
| 261 |
-
- If the answer isn't in the context, say so
|
| 262 |
- Be concise and accurate
|
| 263 |
-
- Reference specific sources when relevant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
# Call Groq API
|
| 268 |
chat_completion = client.chat.completions.create(
|
| 269 |
-
messages=
|
| 270 |
-
{
|
| 271 |
-
"role": "user",
|
| 272 |
-
"content": prompt,
|
| 273 |
-
}
|
| 274 |
-
],
|
| 275 |
model="llama3-8b-8192",
|
| 276 |
temperature=0.3,
|
| 277 |
max_tokens=1024,
|
|
|
|
| 235 |
for chunk, meta in zip(relevant_chunks, metadata)
|
| 236 |
])
|
| 237 |
|
| 238 |
+
# Build messages array for Groq API
|
| 239 |
+
messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
# Add system message
|
| 242 |
+
system_prompt = """You are a helpful assistant that answers questions based on the provided document context.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
Instructions:
|
| 245 |
- Answer based strictly on the provided context
|
| 246 |
+
- If the answer isn't in the context, say so clearly
|
| 247 |
- Be concise and accurate
|
| 248 |
+
- Reference specific sources when relevant"""
|
| 249 |
+
|
| 250 |
+
messages.append({
|
| 251 |
+
"role": "system",
|
| 252 |
+
"content": system_prompt
|
| 253 |
+
})
|
| 254 |
+
|
| 255 |
+
# Add conversation history (last 3 exchanges)
|
| 256 |
+
if history and len(history) > 0:
|
| 257 |
+
recent_history = history[-3:] if len(history) > 3 else history
|
| 258 |
+
for h in recent_history:
|
| 259 |
+
if isinstance(h, (list, tuple)) and len(h) >= 2:
|
| 260 |
+
messages.append({
|
| 261 |
+
"role": "user",
|
| 262 |
+
"content": h[0]
|
| 263 |
+
})
|
| 264 |
+
messages.append({
|
| 265 |
+
"role": "assistant",
|
| 266 |
+
"content": h[1]
|
| 267 |
+
})
|
| 268 |
+
|
| 269 |
+
# Add current query with context
|
| 270 |
+
user_message = f"""Context from documents:
|
| 271 |
+
{context}
|
| 272 |
|
| 273 |
+
Question: {query}"""
|
| 274 |
+
|
| 275 |
+
messages.append({
|
| 276 |
+
"role": "user",
|
| 277 |
+
"content": user_message
|
| 278 |
+
})
|
| 279 |
|
| 280 |
# Call Groq API
|
| 281 |
chat_completion = client.chat.completions.create(
|
| 282 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
model="llama3-8b-8192",
|
| 284 |
temperature=0.3,
|
| 285 |
max_tokens=1024,
|