Shreyas094 commited on
Commit
ee18607
·
verified ·
1 Parent(s): abc17d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -64
app.py CHANGED
@@ -288,11 +288,7 @@ def chatbot_interface(message, history, use_web_search, model, temperature, num_
288
  history = history + [(message, "")]
289
 
290
  try:
291
- if use_web_search:
292
- history[-1] = (message, "Generating response... (This may take a moment)")
293
- yield history
294
-
295
- for response in respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, instruction_key):
296
  history[-1] = (message, response)
297
  yield history
298
  except gr.CancelledError:
@@ -327,7 +323,10 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
327
  use_web_search = False # Ensure we use PDF search for summaries
328
 
329
  if use_web_search:
330
- for response, _ in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
 
 
 
331
  yield response
332
  else:
333
  embed = get_embeddings()
@@ -487,70 +486,36 @@ def get_response_with_search(query, model, num_calls=3, temperature=0.1):
487
  retriever = web_search_database.as_retriever(search_kwargs={"k": 10})
488
  relevant_docs = retriever.get_relevant_documents(query)
489
 
490
- context = "\n".join([doc.page_content for doc in relevant_docs[:5]])
491
 
492
- initial_prompt = f"""Using the following context from web search results:
493
  {context}
494
  Write a detailed and complete research document that fulfills the following user request: '{query}'
495
- Stick closely to the information provided in the context and avoid making unsupported claims."""
 
496
 
497
- try:
 
 
 
 
 
 
 
498
  client = InferenceClient(model, token=huggingface_token)
499
 
500
- # Generate initial response
501
- initial_response = client.text_generation(initial_prompt, max_new_tokens=1000, temperature=temperature)
502
-
503
- # Generate critique
504
- critique = critique_response(initial_response, context, query, model)
505
-
506
- final_prompt = f"""Given the following initial response, context, critique, and original query, provide a revised response that addresses the identified issues and sticks closely to the information provided in the context while fully answering the user's query in a detailed and complete research document, after writing the document, please provide a list of sources used in your response.
507
-
508
- User Query: {query}
509
-
510
- Initial Response:
511
- {initial_response}
512
-
513
- Context:
514
- {context}
515
-
516
- Critique:
517
- {critique}
518
-
519
- Revised Response:"""
520
-
521
- # Generate final response
522
- full_response = ""
523
- for chunk in client.text_generation(final_prompt, max_new_tokens=1500, temperature=temperature, stream=True):
524
- full_response += chunk
525
- yield full_response, ""
526
-
527
- # Add a disclaimer
528
- disclaimer = ("\nNote: This response was generated by an AI model based on web search results. "
529
- "While efforts have been made to ensure accuracy, please verify important information from authoritative sources.")
530
- full_response += disclaimer
531
- yield full_response, ""
532
-
533
- except Exception as e:
534
- logging.error(f"Error in multi-step generation process: {str(e)}")
535
- yield f"An error occurred during the response generation process: {str(e)}", ""
536
-
537
- def critique_response(response, context, query, model):
538
- critique_prompt = f"""Given the following response, original context, and user query, identify any statements that might be inaccurate, unsupported by the context, or irrelevant to the query. Be specific about which parts may be hallucinations or extrapolations beyond the given information.
539
-
540
- User Query: {query}
541
-
542
- Response:
543
- {response}
544
-
545
- Original Context:
546
- {context}
547
-
548
- Critique:"""
549
-
550
- client = InferenceClient(model, token=huggingface_token)
551
- critique = client.text_generation(critique_prompt, max_new_tokens=500, temperature=0.2)
552
-
553
- return critique
554
 
555
 
556
  INSTRUCTION_PROMPTS = {
 
288
  history = history + [(message, "")]
289
 
290
  try:
291
+ for response in respond(message, history, model, temperature, num_calls, use_web_search):
 
 
 
 
292
  history[-1] = (message, response)
293
  yield history
294
  except gr.CancelledError:
 
323
  use_web_search = False # Ensure we use PDF search for summaries
324
 
325
  if use_web_search:
326
+ for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
327
+ response = f"{main_content}\n\n{sources}"
328
+ first_line = response.split('\n')[0] if response else ''
329
+ # logging.info(f"Generated Response (first line): {first_line}")
330
  yield response
331
  else:
332
  embed = get_embeddings()
 
486
  retriever = web_search_database.as_retriever(search_kwargs={"k": 10})
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.
495
+ Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context."""
496
 
497
+ After writing the document, please provide a list of sources used in your response."""
498
+
499
+ if model == "@cf/meta/llama-3.1-8b-instruct":
500
+ # Use Cloudflare API
501
+ for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
502
+ yield response, "" # Yield streaming response without sources
503
+ else:
504
+ # Use Hugging Face API
505
  client = InferenceClient(model, token=huggingface_token)
506
 
507
+ main_content = ""
508
+ for i in range(num_calls):
509
+ for message in client.chat_completion(
510
+ messages=[{"role": "user", "content": prompt}],
511
+ max_tokens=10000,
512
+ temperature=temperature,
513
+ stream=True,
514
+ ):
515
+ if message.choices and message.choices[0].delta and message.choices[0].delta.content:
516
+ chunk = message.choices[0].delta.content
517
+ main_content += chunk
518
+ yield main_content, "" # Yield partial main content without sources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
 
520
 
521
  INSTRUCTION_PROMPTS = {