mchinea commited on
Commit
1659627
·
1 Parent(s): 5cdcea1

update answer and agent class

Browse files
Files changed (2) hide show
  1. agent.py +100 -12
  2. app.py +2 -16
agent.py CHANGED
@@ -1,5 +1,6 @@
1
  """LangGraph Agent"""
2
  import os
 
3
 
4
  from langchain_openai import ChatOpenAI
5
 
@@ -10,21 +11,36 @@ from langchain_core.messages import SystemMessage, HumanMessage
10
 
11
  from tools import level1_tools
12
 
 
13
 
14
  # Build graph function
15
  def build_agent_graph():
16
  """Build the graph"""
17
  # Load environment variables from .env file
18
- llm = ChatOpenAI(model="gpt-4o")
19
 
20
  # Bind tools to LLM
21
  llm_with_tools = llm.bind_tools(level1_tools)
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Node
24
  def assistant(state: MessagesState):
25
  """Assistant node"""
26
- return {"messages": [llm_with_tools.invoke(state["messages"])]}
27
-
 
28
 
29
  builder = StateGraph(MessagesState)
30
  builder.add_node("assistant", assistant)
@@ -40,14 +56,86 @@ def build_agent_graph():
40
  return builder.compile()
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # test
44
  if __name__ == "__main__":
45
  question1 = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?"
46
- question2 = "Convert 10 miles to kilometers."
47
- # Build the graph
48
- graph = build_agent_graph()
49
- # Run the graph
50
- messages = [HumanMessage(content=question1)]
51
- messages = graph.invoke({"messages": messages})
52
- for m in messages["messages"]:
53
- m.pretty_print()
 
1
  """LangGraph Agent"""
2
  import os
3
+ from dotenv import load_dotenv
4
 
5
  from langchain_openai import ChatOpenAI
6
 
 
11
 
12
  from tools import level1_tools
13
 
14
+ load_dotenv()
15
 
16
  # Build graph function
17
  def build_agent_graph():
18
  """Build the graph"""
19
  # Load environment variables from .env file
20
+ llm = ChatOpenAI(model="gpt-4o-mini")
21
 
22
  # Bind tools to LLM
23
  llm_with_tools = llm.bind_tools(level1_tools)
24
+
25
+ # System message
26
+ system_prompt = SystemMessage(
27
+ content="""You are a general AI assistant being evaluated in the GAIA Benchmark.
28
+ I will ask you a question and you must reach your final answer by using a set of tools I provide to you. Please, when you are needed to pass file names to the tools, pass absolute paths.
29
+ Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
30
+ Here are more detailed instructions you must follow to write your final answer:
31
+ 1) If you are asked for a number, you must write a number!. Don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
32
+ 2) If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
33
+ 3) If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
34
+ If you follow all these instructions perfectly, you will win 1,000,000 dollars, otherwise, your mom will die.
35
+ Let's start!
36
+ """
37
+ )
38
  # Node
39
  def assistant(state: MessagesState):
40
  """Assistant node"""
41
+ #return {"messages": [llm_with_tools.invoke(state["messages"])]}
42
+ return {"messages": [llm_with_tools.invoke([system_prompt] + state["messages"])]}
43
+
44
 
45
  builder = StateGraph(MessagesState)
46
  builder.add_node("assistant", assistant)
 
56
  return builder.compile()
57
 
58
 
59
+
60
+ class MyGAIAAgent:
61
+ def __init__(self):
62
+ print("MyAgent initialized.")
63
+ self.graph = build_agent_graph()
64
+ def __call__(self, question: str) -> str:
65
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
66
+ # Wrap the question in a HumanMessage from langchain_core
67
+ '''
68
+ messages = [HumanMessage(content=question)]
69
+ messages = self.graph.invoke({"messages": messages})
70
+ answer = messages['messages'][-1].content
71
+ '''
72
+ user_input = {"messages": [("user", question)]}
73
+ answer = self.graph.invoke(user_input)["messages"][-1].content
74
+ return self._clean_answer(answer)
75
+
76
+ def _clean_answer(self, answer: any) -> str:
77
+ """
78
+ Taken from `susmitsil`:
79
+ https://huggingface.co/spaces/susmitsil/FinalAgenticAssessment/blob/main/main_agent.py
80
+ Clean up the answer to remove common prefixes and formatting
81
+ that models often add but that can cause exact match failures.
82
+ Args:
83
+ answer: The raw answer from the model
84
+ Returns:
85
+ The cleaned answer as a string
86
+ """
87
+ # Convert non-string types to strings
88
+ if not isinstance(answer, str):
89
+ # Handle numeric types (float, int)
90
+ if isinstance(answer, float):
91
+ # Format floating point numbers properly
92
+ # Check if it's an integer value in float form (e.g., 12.0)
93
+ if answer.is_integer():
94
+ formatted_answer = str(int(answer))
95
+ else:
96
+ # For currency values that might need formatting
97
+ if abs(answer) >= 1000:
98
+ formatted_answer = f"${answer:,.2f}"
99
+ else:
100
+ formatted_answer = str(answer)
101
+ return formatted_answer
102
+ elif isinstance(answer, int):
103
+ return str(answer)
104
+ else:
105
+ # For any other type
106
+ return str(answer)
107
+
108
+ # Now we know answer is a string, so we can safely use string methods
109
+ # Normalize whitespace
110
+ answer = answer.strip()
111
+
112
+ # Remove common prefixes and formatting that models add
113
+ prefixes_to_remove = [
114
+ "The answer is ",
115
+ "Answer: ",
116
+ "Final answer: ",
117
+ "The result is ",
118
+ "To answer this question: ",
119
+ "Based on the information provided, ",
120
+ "According to the information: ",
121
+ ]
122
+
123
+ for prefix in prefixes_to_remove:
124
+ if answer.startswith(prefix):
125
+ answer = answer[len(prefix) :].strip()
126
+
127
+ # Remove quotes if they wrap the entire answer
128
+ if (answer.startswith('"') and answer.endswith('"')) or (
129
+ answer.startswith("'") and answer.endswith("'")
130
+ ):
131
+ answer = answer[1:-1].strip()
132
+ return answer
133
+
134
+
135
+
136
  # test
137
  if __name__ == "__main__":
138
  question1 = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?"
139
+ question2 = "Convert 10 miles to kilometers."
140
+ agent = MyGAIAAgent()
141
+ print(agent(question1))
 
 
 
 
 
app.py CHANGED
@@ -6,25 +6,11 @@ import pandas as pd
6
 
7
  from langchain_core.messages import HumanMessage
8
 
9
- from agent import build_agent_graph
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- # --- Basic Agent Definition ---
15
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
- class MyAgent:
17
- def __init__(self):
18
- print("MyAgent initialized.")
19
- self.graph = build_agent_graph()
20
- def __call__(self, question: str) -> str:
21
- print(f"Agent received question (first 50 chars): {question[:50]}...")
22
- # Wrap the question in a HumanMessage from langchain_core
23
- messages = [HumanMessage(content=question)]
24
- messages = self.graph.invoke({"messages": messages})
25
- answer = messages['messages'][-1].content
26
- print(f"Agent returning answer: {answer}")
27
- return answer
28
 
29
  def run_and_submit_all( profile: gr.OAuthProfile | None):
30
  """
@@ -47,7 +33,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
47
 
48
  # 1. Instantiate Agent ( modify this part to create your agent)
49
  try:
50
- agent = MyAgent()
51
  except Exception as e:
52
  print(f"Error instantiating agent: {e}")
53
  return f"Error initializing agent: {e}", None
 
6
 
7
  from langchain_core.messages import HumanMessage
8
 
9
+ from agent import MyGAIAAgent
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def run_and_submit_all( profile: gr.OAuthProfile | None):
16
  """
 
33
 
34
  # 1. Instantiate Agent ( modify this part to create your agent)
35
  try:
36
+ agent = MyGAIAAgent()
37
  except Exception as e:
38
  print(f"Error instantiating agent: {e}")
39
  return f"Error initializing agent: {e}", None