Pijush2023 commited on
Commit
5c92e55
·
verified ·
1 Parent(s): 70e3ee2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -1
app.py CHANGED
@@ -428,18 +428,88 @@ def clean_response(response_text):
428
  return cleaned_response
429
 
430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
  def generate_answer(message, choice, retrieval_mode, selected_model):
432
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
433
 
434
  try:
 
435
  if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
436
  response = fetch_google_hotels()
437
  return response, extract_addresses(response)
438
 
 
439
  if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
440
  response = fetch_yelp_restaurants()
441
  return response, extract_addresses(response)
442
 
 
443
  if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
444
  response = fetch_google_flights()
445
  return response, extract_addresses(response)
@@ -462,7 +532,9 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
462
 
463
  elif selected_model == phi_pipe:
464
  retriever = phi_retriever
465
- context = retriever.get_relevant_documents(message)
 
 
466
  prompt = phi_short_template.format(context=context, question=message)
467
 
468
  logging.debug(f"Phi-3.5 Prompt: {prompt}")
@@ -499,6 +571,7 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
499
 
500
 
501
 
 
502
  def bot(history, choice, tts_choice, retrieval_mode, model_choice):
503
  if not history:
504
  return history
 
428
  return cleaned_response
429
 
430
 
431
+ # def generate_answer(message, choice, retrieval_mode, selected_model):
432
+ # logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
433
+
434
+ # try:
435
+ # if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
436
+ # response = fetch_google_hotels()
437
+ # return response, extract_addresses(response)
438
+
439
+ # if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
440
+ # response = fetch_yelp_restaurants()
441
+ # return response, extract_addresses(response)
442
+
443
+ # if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
444
+ # response = fetch_google_flights()
445
+ # return response, extract_addresses(response)
446
+
447
+ # if retrieval_mode == "VDB":
448
+ # if selected_model == chat_model:
449
+ # retriever = gpt_retriever
450
+ # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
451
+ # context = retriever.get_relevant_documents(message)
452
+ # prompt = prompt_template.format(context=context, question=message)
453
+
454
+ # qa_chain = RetrievalQA.from_chain_type(
455
+ # llm=chat_model,
456
+ # chain_type="stuff",
457
+ # retriever=retriever,
458
+ # chain_type_kwargs={"prompt": prompt_template}
459
+ # )
460
+ # response = qa_chain({"query": message})
461
+ # return response['result'], extract_addresses(response['result'])
462
+
463
+ # elif selected_model == phi_pipe:
464
+ # retriever = phi_retriever
465
+ # context = retriever.get_relevant_documents(message)
466
+ # prompt = phi_short_template.format(context=context, question=message)
467
+
468
+ # logging.debug(f"Phi-3.5 Prompt: {prompt}")
469
+
470
+ # response = selected_model(prompt, **{
471
+ # "max_new_tokens": 160, # Increased to handle longer responses
472
+ # "return_full_text": True,
473
+ # "temperature": 0.7, # Adjusted to avoid cutting off
474
+ # "do_sample": True, # Allow sampling to increase response diversity
475
+ # })
476
+
477
+ # if response:
478
+ # generated_text = response[0]['generated_text']
479
+ # logging.debug(f"Phi-3.5 Response: {generated_text}")
480
+ # cleaned_response = clean_response(generated_text)
481
+ # return cleaned_response, extract_addresses(cleaned_response)
482
+ # else:
483
+ # logging.error("Phi-3.5 did not return any response.")
484
+ # return "No response generated.", []
485
+
486
+ # elif retrieval_mode == "KGF":
487
+ # response = chain_neo4j.invoke({"question": message})
488
+ # return response, extract_addresses(response)
489
+ # else:
490
+ # return "Invalid retrieval mode selected.", []
491
+
492
+ # except Exception as e:
493
+ # logging.error(f"Error in generate_answer: {e}")
494
+ # return "Sorry, I encountered an error while processing your request.", []
495
+
496
+
497
+
498
  def generate_answer(message, choice, retrieval_mode, selected_model):
499
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
500
 
501
  try:
502
+ # Handle hotel-related queries
503
  if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
504
  response = fetch_google_hotels()
505
  return response, extract_addresses(response)
506
 
507
+ # Handle restaurant-related queries
508
  if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
509
  response = fetch_yelp_restaurants()
510
  return response, extract_addresses(response)
511
 
512
+ # Handle flight-related queries
513
  if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
514
  response = fetch_google_flights()
515
  return response, extract_addresses(response)
 
532
 
533
  elif selected_model == phi_pipe:
534
  retriever = phi_retriever
535
+ context_documents = retriever.get_relevant_documents(message)
536
+ context = "\n".join([doc.page_content for doc in context_documents])
537
+
538
  prompt = phi_short_template.format(context=context, question=message)
539
 
540
  logging.debug(f"Phi-3.5 Prompt: {prompt}")
 
571
 
572
 
573
 
574
+
575
  def bot(history, choice, tts_choice, retrieval_mode, model_choice):
576
  if not history:
577
  return history