errchh commited on
Commit
f2ea75b
·
1 Parent(s): 135697e

fix basic agent

Browse files
Files changed (1) hide show
  1. app.py +6 -13
app.py CHANGED
@@ -14,26 +14,19 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
  class BasicAgent:
 
17
  def __init__(self):
18
  print("BasicAgent initialized.")
19
  self.graph = build_graph()
20
 
21
  def __call__(self, question: str) -> str:
22
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
23
  messages = [HumanMessage(content=question)]
24
- result = self.graph.invoke({"messages": messages})
25
- # Extract the final answer message from the state
26
- # The final message should be the content of the last AIMessage
27
- submitted_answer = "Could not get answer from agent output." # Default if extraction fails
28
- if result and "messages" in result:
29
- for msg in reversed(result["messages"]): # Look for the last AI message
30
- # Check if the message is an AIMessage and has content
31
- if isinstance(msg, AIMessage) and hasattr(msg, 'content') and msg.content:
32
- # Convert content to string to ensure type consistency with return hint
33
- submitted_answer = str(msg.content)
34
- break # Found the last AI message content
35
-
36
- return submitted_answer
37
 
38
  def run_and_submit_all( profile: gr.OAuthProfile | None):
39
  """
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
  class BasicAgent:
17
+ """A langgraph agent."""
18
  def __init__(self):
19
  print("BasicAgent initialized.")
20
  self.graph = build_graph()
21
 
22
  def __call__(self, question: str) -> str:
23
  print(f"Agent received question (first 50 chars): {question[:50]}...")
24
+ # Wrap the question in a HumanMessage from langchain_core
25
  messages = [HumanMessage(content=question)]
26
+ messages = self.graph.invoke({"messages": messages})
27
+ answer = messages['messages'][-1].content
28
+ return answer[14:]
29
+
 
 
 
 
 
 
 
 
 
30
 
31
  def run_and_submit_all( profile: gr.OAuthProfile | None):
32
  """