Shreyas094 commited on
Commit
0c9ba4e
·
verified ·
1 Parent(s): a422324

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -464,12 +464,31 @@ After writing the document, please provide a list of sources used in your respon
464
  if not full_response:
465
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
466
 
 
 
 
 
 
 
 
 
 
 
 
467
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
468
  search_results = duckduckgo_search(query)
469
- context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
470
- for result in search_results if 'body' in result)
 
 
 
 
 
 
471
 
472
- prompt = f"""Using the following context:
 
 
473
  {context}
474
  Write a detailed and complete research document that fulfills the following user request: '{query}'
475
  After writing the document, please provide a list of sources used in your response."""
@@ -495,6 +514,7 @@ After writing the document, please provide a list of sources used in your respon
495
  main_content += chunk
496
  yield main_content, "" # Yield partial main content without sources
497
 
 
498
  INSTRUCTION_PROMPTS = {
499
  "Asset Managers": "Summarize the key financial metrics, assets under management, and performance highlights for this asset management company.",
500
  "Consumer Finance Companies": "Provide a summary of the company's loan portfolio, interest income, credit quality, and key operational metrics.",
 
464
  if not full_response:
465
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
466
 
467
+ def create_web_search_vectors(search_results):
468
+ embed = get_embeddings()
469
+
470
+ documents = []
471
+ for result in search_results:
472
+ if 'body' in result:
473
+ content = f"{result['title']}\n{result['body']}\nSource: {result['href']}"
474
+ documents.append(Document(page_content=content, metadata={"source": result['href']}))
475
+
476
+ return FAISS.from_documents(documents, embed)
477
+
478
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
479
  search_results = duckduckgo_search(query)
480
+ web_search_database = create_web_search_vectors(search_results)
481
+
482
+ if not web_search_database:
483
+ yield "No web search results available. Please try again.", ""
484
+ return
485
+
486
+ retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
487
+ relevant_docs = retriever.get_relevant_documents(query)
488
 
489
+ context = "\n".join([doc.page_content for doc in relevant_docs])
490
+
491
+ prompt = f"""Using the following context from web search results:
492
  {context}
493
  Write a detailed and complete research document that fulfills the following user request: '{query}'
494
  After writing the document, please provide a list of sources used in your response."""
 
514
  main_content += chunk
515
  yield main_content, "" # Yield partial main content without sources
516
 
517
+
518
  INSTRUCTION_PROMPTS = {
519
  "Asset Managers": "Summarize the key financial metrics, assets under management, and performance highlights for this asset management company.",
520
  "Consumer Finance Companies": "Provide a summary of the company's loan portfolio, interest income, credit quality, and key operational metrics.",