Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +30 -38
utils/database.py
CHANGED
|
@@ -337,7 +337,7 @@ def display_vector_store_info():
|
|
| 337 |
st.error(traceback.format_exc())
|
| 338 |
|
| 339 |
def initialize_qa_system(vector_store):
|
| 340 |
-
"""Initialize QA system with
|
| 341 |
try:
|
| 342 |
llm = ChatOpenAI(
|
| 343 |
temperature=0.5,
|
|
@@ -345,53 +345,45 @@ def initialize_qa_system(vector_store):
|
|
| 345 |
api_key=os.environ.get("OPENAI_API_KEY")
|
| 346 |
)
|
| 347 |
|
| 348 |
-
# Create
|
| 349 |
-
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
| 350 |
-
|
| 351 |
-
# Create a template that encourages structured responses
|
| 352 |
prompt = ChatPromptTemplate.from_messages([
|
| 353 |
-
("system", """You are a helpful assistant analyzing RFP documents
|
| 354 |
-
|
| 355 |
-
1. Structure your response clearly with sections
|
| 356 |
-
2. Use bullet points for key information
|
| 357 |
-
3. Always cite the source document and relevant sections
|
| 358 |
-
4. Provide a brief summary at the start
|
| 359 |
-
5. Be concise and clear"""),
|
| 360 |
-
MessagesPlaceholder(variable_name="chat_history"),
|
| 361 |
-
("human", """{input}
|
| 362 |
-
|
| 363 |
-
Context from documents:
|
| 364 |
-
{context}
|
| 365 |
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
])
|
| 372 |
|
| 373 |
-
def
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
return
|
| 386 |
|
| 387 |
chain = (
|
| 388 |
{
|
| 389 |
-
"context": get_context_with_sources,
|
| 390 |
-
"chat_history":
|
| 391 |
"input": lambda x: x["input"]
|
| 392 |
}
|
| 393 |
-
| prompt
|
| 394 |
| llm
|
|
|
|
| 395 |
)
|
| 396 |
|
| 397 |
return chain
|
|
|
|
| 337 |
st.error(traceback.format_exc())
|
| 338 |
|
| 339 |
def initialize_qa_system(vector_store):
|
| 340 |
+
"""Initialize QA system with clean response formatting."""
|
| 341 |
try:
|
| 342 |
llm = ChatOpenAI(
|
| 343 |
temperature=0.5,
|
|
|
|
| 345 |
api_key=os.environ.get("OPENAI_API_KEY")
|
| 346 |
)
|
| 347 |
|
| 348 |
+
# Create a template that enforces clean formatting
|
|
|
|
|
|
|
|
|
|
| 349 |
prompt = ChatPromptTemplate.from_messages([
|
| 350 |
+
("system", """You are a helpful assistant analyzing RFP documents.
|
| 351 |
+
Format your responses in a clean, professional manner using markdown formatting:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
+
1. Start with a brief executive summary in plain text
|
| 354 |
+
2. Use proper markdown headers for sections (e.g., ## Key Points)
|
| 355 |
+
3. Use bullet points for lists
|
| 356 |
+
4. Include source attribution at the end in italics
|
| 357 |
+
5. Don't include any technical metadata or delimiters
|
| 358 |
+
6. Use bold for emphasis on important points
|
| 359 |
+
7. Keep the formatting clean and professional"""),
|
| 360 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 361 |
+
("human", "{input}\n\nContext: {context}")
|
| 362 |
])
|
| 363 |
|
| 364 |
+
def format_response(response_text):
|
| 365 |
+
"""Clean up the response formatting."""
|
| 366 |
+
# Remove technical metadata
|
| 367 |
+
if 'content=' in response_text:
|
| 368 |
+
response_text = response_text.split('content=')[1].split('response_metadata')[0]
|
| 369 |
+
|
| 370 |
+
# Remove outer quotes if present
|
| 371 |
+
response_text = response_text.strip("'")
|
| 372 |
+
|
| 373 |
+
# Remove escaped newlines and replace with actual newlines
|
| 374 |
+
response_text = response_text.replace('\\n', '\n')
|
| 375 |
+
|
| 376 |
+
return response_text
|
| 377 |
|
| 378 |
chain = (
|
| 379 |
{
|
| 380 |
+
"context": lambda x: get_context_with_sources(x["input"], retriever),
|
| 381 |
+
"chat_history": lambda x: x.get("chat_history", []),
|
| 382 |
"input": lambda x: x["input"]
|
| 383 |
}
|
| 384 |
+
| prompt
|
| 385 |
| llm
|
| 386 |
+
| format_response
|
| 387 |
)
|
| 388 |
|
| 389 |
return chain
|