Futuresony commited on
Commit
19a2a38
·
verified ·
1 Parent(s): 9666603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -27
app.py CHANGED
@@ -374,13 +374,15 @@ def determine_tool_usage(query: str) -> str:
374
 
375
 
376
  # Function to generate text using the LLM, incorporating tool results if available
377
- def generate_text(prompt: str, tool_results: dict = None) -> str:
378
  """
379
- Generates text using the configured LLM, optionally incorporating tool results.
380
  Args:
381
- prompt: The initial prompt for the LLM.
382
  tool_results: A dictionary containing results from executed tools.
383
  Keys are tool names, values are their outputs.
 
 
384
  Returns:
385
  The generated text from the LLM.
386
  """
@@ -389,34 +391,52 @@ def generate_text(prompt: str, tool_results: dict = None) -> str:
389
  You are a friendly and helpful chatbot. Respond to greetings appropriately (e.g., "Hello!", "Hi there!", "Habari!"). If the user uses Swahili greetings or simple conversational phrases, respond in Swahili. Otherwise, respond in English unless the query is clearly in Swahili. Handle conversational flow and ask follow-up questions when appropriate.
390
  If the user asks a question about other companies or general knowledge, answer their question. However, subtly remind them that your primary expertise and purpose are related to Absa-specific information.
391
  """
392
- full_prompt_builder = [persona_instructions, prompt]
 
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  if tool_results and any(tool_results.values()):
395
- full_prompt_builder.append("\n\nTool Results:\n")
396
- for question, results in tool_results.items(): # Iterate through results per question
397
  if results:
398
- full_prompt_builder.append(f"--- Results for: {question} ---\n") # Add question context
399
  if isinstance(results, list):
400
  for i, result in enumerate(results):
401
- # Check if the result is from business info retrieval
402
- if isinstance(result, dict) and 'Service' in result and 'Description' in result:
403
- full_prompt_builder.append(f"Business Info {i+1}:\nService: {result.get('Service', 'N/A')}\nDescription: {result.get('Description', 'N/A')}\n\n")
404
- elif isinstance(result, dict) and 'url' in result: # Check if the result is from DuckDuckGo
405
- full_prompt_builder.append(f"Search Result {i+1}:\nTitle: {result.get('title', 'N/A')}\nURL: {result.get('url', 'N/A')}\nSnippet: {result.get('body', 'N/A')}\n\n")
406
- else:
407
- full_prompt_builder.append(f"{result}\n\n") # Handle other list items
408
  elif isinstance(results, dict):
409
- for key, value in results.items():
410
- full_prompt_builder.append(f"{key}: {value}\n")
411
- full_prompt_builder.append("\n")
412
  else:
413
- full_prompt_builder.append(f"{results}\n\n") # Handle single string results (like date calculation)
414
- full_prompt_builder.append("Based on the provided tool results, answer the user's original query. If a question was answered by a tool, use the tool's result directly in your response. Maintain the language of the original query if possible, especially for simple greetings or direct questions answered by tools.")
 
415
  print("Added tool results and instruction to final prompt.")
416
  else:
417
- print("No tool results to add to final prompt.")
418
- full_prompt = "".join(full_prompt_builder)
419
- print(f"Sending prompt to LLM:\n---\n{full_prompt}\n---")
 
 
 
 
420
  generation_config = {
421
  "temperature": 0.7,
422
  "max_new_tokens": 500,
@@ -426,9 +446,7 @@ If the user asks a question about other companies or general knowledge, answer t
426
  }
427
  try:
428
  response = client.chat_completion(
429
- messages=[
430
- {"role": "user", "content": full_prompt}
431
- ],
432
  max_tokens=generation_config.get("max_new_tokens", 512),
433
  temperature=generation_config.get("temperature", 0.7),
434
  top_p=generation_config.get("top_p", 0.95)
@@ -517,8 +535,8 @@ Query: {query}
517
 
518
  # Step 5: Final Response Generation
519
  print("\n--- Generating final response ---")
520
- # The generate_text function already handles incorporating tool results if provided
521
- final_response = generate_text(query, tool_results)
522
  print("\n--- Final Response from LLM ---")
523
  print(final_response)
524
  print("\n----------------------------")
 
374
 
375
 
376
  # Function to generate text using the LLM, incorporating tool results if available
377
+ def generate_text(prompt: str, tool_results: dict = None, chat_history: list[dict] = None) -> str:
378
  """
379
+ Generates text using the configured LLM, optionally incorporating tool results and chat history.
380
  Args:
381
+ prompt: The initial prompt for the LLM (the user's latest query).
382
  tool_results: A dictionary containing results from executed tools.
383
  Keys are tool names, values are their outputs.
384
+ chat_history: The history of the conversation as a list of dictionaries
385
+ (as provided by Gradio ChatInterface with type="messages").
386
  Returns:
387
  The generated text from the LLM.
388
  """
 
391
  You are a friendly and helpful chatbot. Respond to greetings appropriately (e.g., "Hello!", "Hi there!", "Habari!"). If the user uses Swahili greetings or simple conversational phrases, respond in Swahili. Otherwise, respond in English unless the query is clearly in Swahili. Handle conversational flow and ask follow-up questions when appropriate.
392
  If the user asks a question about other companies or general knowledge, answer their question. However, subtly remind them that your primary expertise and purpose are related to Absa-specific information.
393
  """
394
+ # Build the messages list for the chat completion API
395
+ messages = [{"role": "user", "content": persona_instructions}] # Start with the persona instructions
396
 
397
+ if chat_history:
398
+ print("Including chat history in LLM prompt.")
399
+ # Iterate through the chat_history provided by Gradio (list of dictionaries)
400
+ # Add only 'user' and 'assistant' roles to the LLM context
401
+ for message_dict in chat_history:
402
+ role = message_dict.get("role")
403
+ content = message_dict.get("content")
404
+ if role in ["user", "assistant"] and content is not None:
405
+ messages.append({"role": role, "content": content})
406
+
407
+
408
+ # Add the current user prompt and tool results
409
+ current_user_content = prompt
410
  if tool_results and any(tool_results.values()):
411
+ current_user_content += "\n\nTool Results:\n"
412
+ for question, results in tool_results.items():
413
  if results:
414
+ current_user_content += f"--- Results for: {question} ---\n"
415
  if isinstance(results, list):
416
  for i, result in enumerate(results):
417
+ if isinstance(result, dict) and 'Service' in result and 'Description' in result:
418
+ current_user_content += f"Business Info {i+1}:\nService: {result.get('Service', 'N/A')}\nDescription: {result.get('Description', 'N/A')}\n\n"
419
+ elif isinstance(result, dict) and 'url' in result:
420
+ current_user_content += f"Search Result {i+1}:\nTitle: {result.get('title', 'N/A')}\nURL: {result.get('url', 'N/A')}\nSnippet: {result.get('body', 'N/A')}\n\n"
421
+ else:
422
+ current_user_content += f"{result}\n\n"
 
423
  elif isinstance(results, dict):
424
+ for key, value in results.items():
425
+ current_user_content += f"{key}: {value}\n"
426
+ current_user_content += "\n"
427
  else:
428
+ current_user_content += f"{results}\n\n"
429
+
430
+ current_user_content += "Based on the provided tool results and the conversation history, answer the user's latest query. If a question was answered by a tool, use the tool's result directly in your response. Maintain the language of the original query if possible, especially for simple greetings or direct questions answered by tools."
431
  print("Added tool results and instruction to final prompt.")
432
  else:
433
+ current_user_content += "Based on the conversation history, answer the user's latest query."
434
+ print("No tool results to add to final prompt, relying on conversation history.")
435
+
436
+ messages.append({"role": "user", "content": current_user_content})
437
+
438
+
439
+ print(f"Sending messages to LLM:\n---\n{messages}\n---")
440
  generation_config = {
441
  "temperature": 0.7,
442
  "max_new_tokens": 500,
 
446
  }
447
  try:
448
  response = client.chat_completion(
449
+ messages=messages, # Pass the list of messages
 
 
450
  max_tokens=generation_config.get("max_new_tokens", 512),
451
  temperature=generation_config.get("temperature", 0.7),
452
  top_p=generation_config.get("top_p", 0.95)
 
535
 
536
  # Step 5: Final Response Generation
537
  print("\n--- Generating final response ---")
538
+ # Pass the chat_history (which is a list of dictionaries when using type="messages")
539
+ final_response = generate_text(query, tool_results, chat_history)
540
  print("\n--- Final Response from LLM ---")
541
  print(final_response)
542
  print("\n----------------------------")