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

fix basic agent

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -23,9 +23,30 @@ class BasicAgent:
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):
 
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
+ # The graph.invoke method returns the final state of the graph.
27
+ # The state is expected to contain a 'messages' key with a list of messages.
28
+ result = self.graph.invoke({"messages": messages})
29
+ # Extract the final answer message from the state
30
+ # The final message should be the content of the last AIMessage with content
31
+ submitted_answer = "Could not get answer from agent output." # Default if extraction fails
32
+
33
+ # Check if the result is a dictionary and contains a list of messages
34
+ if isinstance(result, dict) and "messages" in result and isinstance(result["messages"], list):
35
+ # Iterate through messages in reverse to find the last one from the AI
36
+ for msg in reversed(result["messages"]):
37
+ # Check if the message is an AIMessage and has content (non-empty string or non-empty list/dict representation)
38
+ # The 'content' attribute exists on AIMessage, so hasattr check is redundant.
39
+ if isinstance(msg, AIMessage) and msg.content is not None:
40
+ # Ensure the content is treated as a string for submission.
41
+ # LangChain AIMessage content can be str or list[str | dict].
42
+ # str() handles conversion for both cases.
43
+ submitted_answer = str(msg.content)
44
+ break # Found the last AI message with content, stop searching
45
+
46
+ # If the loop finishes without finding an AIMessage with content,
47
+ # submitted_answer remains the default value.
48
+
49
+ return submitted_answer
50
 
51
 
52
  def run_and_submit_all( profile: gr.OAuthProfile | None):