cryogenic22 commited on
Commit
8c249d6
·
verified ·
1 Parent(s): 71ba388

Update utils/database.py

Browse files
Files changed (1) hide show
  1. 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 structured responses."""
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 retriever function
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 to help companies reuse the knowledge based and be efficient with new RFP responses.
354
- When providing answers:
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
- Please provide a structured response with:
367
- - Summary
368
- - Key Points
369
- - Source Details
370
- """)
 
 
 
 
371
  ])
372
 
373
- def get_chat_history(inputs):
374
- chat_history = inputs.get("chat_history", [])
375
- if not isinstance(chat_history, list):
376
- return []
377
- return [msg for msg in chat_history if isinstance(msg, BaseMessage)]
378
-
379
- def get_context_with_sources(inputs):
380
- docs = retriever.get_relevant_documents(inputs["input"])
381
- contexts = []
382
- for doc in docs:
383
- source = doc.metadata.get('source', 'Unknown source')
384
- contexts.append(f"\nSource: {source}\nContent: {doc.page_content}")
385
- return "\n---\n".join(contexts)
386
 
387
  chain = (
388
  {
389
- "context": get_context_with_sources,
390
- "chat_history": get_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