wt002 commited on
Commit
d977a88
·
verified ·
1 Parent(s): 31f3113

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +70 -26
agent.py CHANGED
@@ -350,31 +350,6 @@ async def start_questions(request: Request):
350
  return {"message": f"Loaded {len(docs)} questions", "docs": [doc.page_content for doc in docs]}
351
 
352
 
353
- # -------------------------------
354
- # Step 3: Create Documents from Each JSON Object
355
- # -------------------------------
356
- docs = []
357
- for task in tasks:
358
- # Debugging: Print the keys of each task to ensure 'question' exists
359
- print(f"Keys in task: {task.keys()}")
360
-
361
- # Ensure the required field 'question' exists
362
- if 'question' not in task:
363
- print(f"Skipping task with missing 'question' field: {task}")
364
- continue
365
-
366
- content = task.get('question', "").strip()
367
- if not content:
368
- print(f"Skipping task with empty 'question': {task}")
369
- continue
370
-
371
- # Add unique ID to each document
372
- task['id'] = str(uuid.uuid4())
373
-
374
- # Create a document from the task data
375
- docs.append(Document(page_content=content, metadata=task))
376
-
377
-
378
 
379
 
380
  # -------------------------------
@@ -633,8 +608,77 @@ def generate_final_answer(state: dict, task_results: dict) -> str:
633
  return "🤖 Unable to generate a specific answer."
634
 
635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  # ----------------------------------------------------------------
637
- # Process Function (Main Agent Runner)
638
  # ----------------------------------------------------------------
639
  def process_question(question: str):
640
  tasks = planner(question)
 
350
  return {"message": f"Loaded {len(docs)} questions", "docs": [doc.page_content for doc in docs]}
351
 
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
 
355
  # -------------------------------
 
608
  return "🤖 Unable to generate a specific answer."
609
 
610
 
611
+ def answer_question(question: str) -> str:
612
+ """Process a single question and return the answer."""
613
+ print(f"Processing question: {question[:50]}...") # Debugging: show first 50 chars
614
+
615
+ # Wrap the question in a HumanMessage from langchain_core
616
+ messages = [HumanMessage(content=question)]
617
+ messages = graph.invoke({"messages": messages}) # Assuming `graph` is defined elsewhere
618
+
619
+ # Extract the answer from the response
620
+ answer = messages['messages'][-1].content
621
+ return answer[14:] # Assuming 'answer[14:]' is correct based on your example
622
+
623
+
624
+ def process_all_tasks(tasks: list):
625
+ """Process a list of tasks."""
626
+ results = {}
627
+
628
+ for task in tasks:
629
+ # Ensure task has a question and process it
630
+ question = task.get("question", "").strip()
631
+ if not question:
632
+ print(f"Skipping task with missing or empty 'question': {task}")
633
+ continue
634
+
635
+ print(f"\n🟢 Processing Task: {task['task_id']} - Question: {question}")
636
+
637
+ # Call the existing process_question logic
638
+ response = process_question(question)
639
+
640
+ print(f"✅ Response: {response}")
641
+ results[task['task_id']] = response
642
+
643
+ return results
644
+
645
+
646
+ def process_question(question: str):
647
+ tasks = planner(question)
648
+ print(f"Tasks to perform: {tasks}")
649
+
650
+ task_type = task_classifier(question)
651
+ print(f"Task type: {task_type}")
652
+
653
+ state = {"question": question, "last_response": "", "messages": [HumanMessage(content=question)]}
654
+ next_task = decide_task(state)
655
+ print(f"Next task: {next_task}")
656
+
657
+ if node_skipper(state):
658
+ print(f"Skipping task: {next_task}")
659
+ return "Task skipped."
660
+
661
+ try:
662
+ if task_type == "wiki_search":
663
+ response = wiki_tool.run(question)
664
+ elif task_type == "math":
665
+ # You should dynamically parse these inputs in real use
666
+ response = calc_tool.run(question)
667
+ elif task_type == "retriever":
668
+ retrieval_result = retriever(state)
669
+ response = retrieval_result["messages"][-1].content
670
+ else:
671
+ response = "Default fallback answer."
672
+
673
+ return generate_final_answer(state, {task_type: response})
674
+
675
+ except Exception as e:
676
+ print(f"❌ Error: {e}")
677
+ return "Sorry, I encountered an error processing your request."
678
+
679
+
680
  # ----------------------------------------------------------------
681
+ # Process Function (Main Agent Runner) OLD Code
682
  # ----------------------------------------------------------------
683
  def process_question(question: str):
684
  tasks = planner(question)