Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +9 -27
utils/database.py
CHANGED
|
@@ -335,7 +335,8 @@ def display_vector_store_info():
|
|
| 335 |
except Exception as e:
|
| 336 |
st.error(f"Error displaying vector store info: {e}")
|
| 337 |
st.error(traceback.format_exc())
|
| 338 |
-
|
|
|
|
| 339 |
def initialize_qa_system(vector_store):
|
| 340 |
"""Initialize QA system with proper chat handling."""
|
| 341 |
try:
|
|
@@ -344,35 +345,31 @@ def initialize_qa_system(vector_store):
|
|
| 344 |
model_name="gpt-4",
|
| 345 |
api_key=os.environ.get("OPENAI_API_KEY")
|
| 346 |
)
|
| 347 |
-
|
| 348 |
# Create retriever function
|
| 349 |
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
| 350 |
-
|
| 351 |
# Create a template that enforces clean formatting
|
| 352 |
prompt = ChatPromptTemplate.from_messages([
|
| 353 |
("system", """You are a helpful assistant analyzing RFP documents.
|
| 354 |
-
Format your responses in a clean, professional manner
|
| 355 |
|
| 356 |
-
1. Start with a brief executive summary
|
| 357 |
-
2. Use
|
| 358 |
3. Use bullet points for lists
|
| 359 |
-
4. Include source attribution at the end
|
| 360 |
-
5.
|
| 361 |
-
6. Use bold for emphasis on important points
|
| 362 |
-
7. Keep the formatting clean and professional"""),
|
| 363 |
MessagesPlaceholder(variable_name="chat_history"),
|
| 364 |
("human", "{input}\n\nContext: {context}")
|
| 365 |
])
|
| 366 |
|
| 367 |
def get_chat_history(inputs):
|
| 368 |
-
"""Get formatted chat history."""
|
| 369 |
chat_history = inputs.get("chat_history", [])
|
| 370 |
if not isinstance(chat_history, list):
|
| 371 |
return []
|
| 372 |
return [msg for msg in chat_history if isinstance(msg, BaseMessage)]
|
| 373 |
|
| 374 |
def get_context(inputs):
|
| 375 |
-
"""Get formatted context from documents."""
|
| 376 |
docs = retriever.get_relevant_documents(inputs["input"])
|
| 377 |
context_parts = []
|
| 378 |
for doc in docs:
|
|
@@ -380,20 +377,6 @@ def initialize_qa_system(vector_store):
|
|
| 380 |
context_parts.append(f"\nFrom {source}:\n{doc.page_content}")
|
| 381 |
return "\n".join(context_parts)
|
| 382 |
|
| 383 |
-
def format_response(response_text):
|
| 384 |
-
"""Clean up the response formatting."""
|
| 385 |
-
# Remove technical metadata
|
| 386 |
-
if 'content=' in response_text:
|
| 387 |
-
response_text = response_text.split('content=')[1].split('response_metadata')[0]
|
| 388 |
-
|
| 389 |
-
# Remove outer quotes if present
|
| 390 |
-
response_text = response_text.strip("'")
|
| 391 |
-
|
| 392 |
-
# Remove escaped newlines and replace with actual newlines
|
| 393 |
-
response_text = response_text.replace('\\n', '\n')
|
| 394 |
-
|
| 395 |
-
return response_text
|
| 396 |
-
|
| 397 |
chain = (
|
| 398 |
{
|
| 399 |
"context": get_context,
|
|
@@ -402,7 +385,6 @@ def initialize_qa_system(vector_store):
|
|
| 402 |
}
|
| 403 |
| prompt
|
| 404 |
| llm
|
| 405 |
-
| format_response
|
| 406 |
)
|
| 407 |
|
| 408 |
return chain
|
|
|
|
| 335 |
except Exception as e:
|
| 336 |
st.error(f"Error displaying vector store info: {e}")
|
| 337 |
st.error(traceback.format_exc())
|
| 338 |
+
|
| 339 |
+
|
| 340 |
def initialize_qa_system(vector_store):
|
| 341 |
"""Initialize QA system with proper chat handling."""
|
| 342 |
try:
|
|
|
|
| 345 |
model_name="gpt-4",
|
| 346 |
api_key=os.environ.get("OPENAI_API_KEY")
|
| 347 |
)
|
| 348 |
+
|
| 349 |
# Create retriever function
|
| 350 |
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
| 351 |
+
|
| 352 |
# Create a template that enforces clean formatting
|
| 353 |
prompt = ChatPromptTemplate.from_messages([
|
| 354 |
("system", """You are a helpful assistant analyzing RFP documents.
|
| 355 |
+
Format your responses in a clean, professional manner:
|
| 356 |
|
| 357 |
+
1. Start with a brief executive summary
|
| 358 |
+
2. Use clear section headers
|
| 359 |
3. Use bullet points for lists
|
| 360 |
+
4. Include source attribution at the end
|
| 361 |
+
5. Keep the formatting clean and professional"""),
|
|
|
|
|
|
|
| 362 |
MessagesPlaceholder(variable_name="chat_history"),
|
| 363 |
("human", "{input}\n\nContext: {context}")
|
| 364 |
])
|
| 365 |
|
| 366 |
def get_chat_history(inputs):
|
|
|
|
| 367 |
chat_history = inputs.get("chat_history", [])
|
| 368 |
if not isinstance(chat_history, list):
|
| 369 |
return []
|
| 370 |
return [msg for msg in chat_history if isinstance(msg, BaseMessage)]
|
| 371 |
|
| 372 |
def get_context(inputs):
|
|
|
|
| 373 |
docs = retriever.get_relevant_documents(inputs["input"])
|
| 374 |
context_parts = []
|
| 375 |
for doc in docs:
|
|
|
|
| 377 |
context_parts.append(f"\nFrom {source}:\n{doc.page_content}")
|
| 378 |
return "\n".join(context_parts)
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
chain = (
|
| 381 |
{
|
| 382 |
"context": get_context,
|
|
|
|
| 385 |
}
|
| 386 |
| prompt
|
| 387 |
| llm
|
|
|
|
| 388 |
)
|
| 389 |
|
| 390 |
return chain
|