EtienneB commited on
Commit
0bb6942
·
1 Parent(s): 6044144
Files changed (2) hide show
  1. app.py +30 -2
  2. tools.py +2 -2
app.py CHANGED
@@ -5,7 +5,7 @@ import gradio as gr
5
  import pandas as pd
6
  import requests
7
  # Additional libraries
8
- from langchain_core.messages import HumanMessage
9
 
10
  from agent import build_graph
11
 
@@ -54,7 +54,35 @@ class BasicAgent:
54
  # The answer is expected to be in the 'content' of the last message.
55
  answer = response_messages['messages'][-1].content
56
  print(f"Agent full response: {answer}")
57
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
 
 
5
  import pandas as pd
6
  import requests
7
  # Additional libraries
8
+ from langchain_core.messages import AIMessage, HumanMessage
9
 
10
  from agent import build_graph
11
 
 
54
  # The answer is expected to be in the 'content' of the last message.
55
  answer = response_messages['messages'][-1].content
56
  print(f"Agent full response: {answer}")
57
+
58
+ final_answer = ""
59
+ if not messages:
60
+ print(f"No messages found in the result state for task {task_id}.")
61
+ return "AGENT ERROR: No messages returned by the agent."
62
+
63
+ for msg in reversed(messages):
64
+ if hasattr(msg, "content") and msg.content:
65
+ content = msg.content
66
+ if isinstance(content, str):
67
+ if "FINAL ANSWER:" in content:
68
+ final_answer = content.split("FINAL ANSWER:", 1)[1].strip()
69
+ break
70
+ elif isinstance(msg, AIMessage):
71
+ # If it's an AIMessage and no "FINAL ANSWER:" has been found yet,
72
+ # tentatively set it. This will be overridden if a "FINAL ANSWER:" is found later.
73
+ if not final_answer:
74
+ final_answer = content
75
+
76
+ # If after checking all messages, final_answer is still from a non-"FINAL ANSWER:" AIMessage, that's our best guess.
77
+ # If final_answer is empty, it means no AIMessage with content or "FINAL ANSWER:" was found.
78
+ if not final_answer: # This means no "FINAL ANSWER:" and no AIMessage content was suitable
79
+ final_answer = "AGENT ERROR: Could not extract a final answer from the agent's messages."
80
+ print(f"Could not extract final answer for task {task_id}. Messages: {messages}")
81
+
82
+ print(f"FinalAgent returning answer for task_id '{task_id}': {final_answer[:100]}...")
83
+ return final_answer
84
+
85
+ # return answer
86
 
87
 
88
 
tools.py CHANGED
@@ -10,12 +10,12 @@ import pandas
10
  import pytz
11
  import torch
12
  from bs4 import BeautifulSoup
13
- from langchain.schema import HumanMessage
14
  from langchain_community.document_loaders import (
15
  ArxivLoader, AssemblyAIAudioTranscriptLoader, WikipediaLoader)
16
  from langchain_community.document_loaders.generic import GenericLoader
17
  from langchain_community.document_loaders.parsers import LanguageParser
18
- from langchain_community.tools import DuckDuckGoSearchRun
 
19
  from langchain_core.tools import tool
20
  from langchain_google_genai import ChatGoogleGenerativeAI
21
  from langchain_tavily import TavilySearch
 
10
  import pytz
11
  import torch
12
  from bs4 import BeautifulSoup
 
13
  from langchain_community.document_loaders import (
14
  ArxivLoader, AssemblyAIAudioTranscriptLoader, WikipediaLoader)
15
  from langchain_community.document_loaders.generic import GenericLoader
16
  from langchain_community.document_loaders.parsers import LanguageParser
17
+ from langchain_core.messages import HumanMessage
18
+ # from langchain_community.tools import DuckDuckGoSearchRun
19
  from langchain_core.tools import tool
20
  from langchain_google_genai import ChatGoogleGenerativeAI
21
  from langchain_tavily import TavilySearch