Shago commited on
Commit
8d60062
·
verified ·
1 Parent(s): fea2f17

Update agents/agents_nodes.py

Browse files
Files changed (1) hide show
  1. agents/agents_nodes.py +72 -26
agents/agents_nodes.py CHANGED
@@ -86,37 +86,83 @@ F_MAPPING = {
86
 
87
  def format_output(state: AgentState):
88
  try:
89
- # The last message should be the ToolMessage (from the tool node)
90
- if not state["messages"] or not isinstance(state["messages"][-1], ToolMessage):
91
- return {"output": {"error": "No tool result found in the last message"}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- tool_message = state["messages"][-1]
94
- # Parse the content of the tool message as JSON
95
- tool_result = json.loads(tool_message.content)
96
 
97
- # The second last message should be the AIMessage with the tool call
98
- if len(state["messages"]) < 2 or not isinstance(state["messages"][-2], AIMessage):
99
- return {"output": {"error": "No AI message (with tool call) found before the tool message"}}
100
-
101
- ai_message = state["messages"][-2]
102
- if not ai_message.tool_calls:
103
- return {"output": {"error": "The AI message does not contain tool calls"}}
104
-
105
- # We take the first tool call (since we forced one tool)
106
- tool_call = ai_message.tool_calls
107
- args = tool_call["args"]
108
-
109
- # Get the factor type from the args
110
- factor_type = args["F"]
111
- if factor_type not in F_MAPPING:
112
- return {"output": {"error": f"Unrecognized factor type: {factor_type}"}}
113
 
114
  result_key = F_MAPPING[factor_type]
115
  if result_key not in tool_result:
116
- return {"output": {"error": f"Expected key {result_key} not found in tool result"}}
117
-
118
  value = tool_result[result_key]
119
  return {"output": {result_key: round(float(value), 2)}}
120
 
121
- except (KeyError, TypeError, json.JSONDecodeError, IndexError) as e:
122
- return {"output": {"error": f"Result formatting failed: {str(e)}"}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  def format_output(state: AgentState):
88
  try:
89
+ # Validate messages list length
90
+ if len(state["messages"]) < 2:
91
+ return {"output": {"error": "Insufficient messages to process"}}
92
+
93
+ # Safely access list indices
94
+ last_msg = state["messages"][-1]
95
+ second_last_msg = state["messages"][-2]
96
+
97
+ # Verify message types
98
+ if not isinstance(last_msg, ToolMessage):
99
+ return {"output": {"error": "Last message must be ToolMessage"}}
100
+ if not isinstance(second_last_msg, AIMessage):
101
+ return {"output": {"error": "Second-last message must be AIMessage"}}
102
+
103
+ # Parse tool result
104
+ try:
105
+ tool_result = json.loads(last_msg.content)
106
+ except json.JSONDecodeError:
107
+ return {"output": {"error": "ToolMessage content is invalid JSON"}}
108
+
109
+ # Validate tool call structure
110
+ if not second_last_msg.tool_calls:
111
+ return {"output": {"error": "AIMessage has no tool calls"}}
112
 
113
+ first_tool_call = second_last_msg.tool_calls
114
+ args = first_tool_call.get("args", {})
 
115
 
116
+ # Process factor type
117
+ factor_type = args.get("F")
118
+ if not factor_type or factor_type not in F_MAPPING:
119
+ return {"output": {"error": f"Invalid factor type: {factor_type}"}}
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  result_key = F_MAPPING[factor_type]
122
  if result_key not in tool_result:
123
+ return {"output": {"error": f"Result key {result_key} not found"}}
124
+
125
  value = tool_result[result_key]
126
  return {"output": {result_key: round(float(value), 2)}}
127
 
128
+ except Exception as e:
129
+ return {"output": {"error": f"Formatting failed: {str(e)}"}}
130
+
131
+
132
+
133
+ # def format_output(state: AgentState):
134
+ # try:
135
+ # # The last message should be the ToolMessage (from the tool node)
136
+ # if not state["messages"] or not isinstance(state["messages"][-1], ToolMessage):
137
+ # return {"output": {"error": "No tool result found in the last message"}}
138
+
139
+ # tool_message = state["messages"][-1]
140
+ # # Parse the content of the tool message as JSON
141
+ # tool_result = json.loads(tool_message.content)
142
+
143
+ # # The second last message should be the AIMessage with the tool call
144
+ # if len(state["messages"]) < 2 or not isinstance(state["messages"][-2], AIMessage):
145
+ # return {"output": {"error": "No AI message (with tool call) found before the tool message"}}
146
+
147
+ # ai_message = state["messages"][-2]
148
+ # if not ai_message.tool_calls:
149
+ # return {"output": {"error": "The AI message does not contain tool calls"}}
150
+
151
+ # # We take the first tool call (since we forced one tool)
152
+ # tool_call = ai_message.tool_calls
153
+ # args = tool_call["args"]
154
+
155
+ # # Get the factor type from the args
156
+ # factor_type = args["F"]
157
+ # if factor_type not in F_MAPPING:
158
+ # return {"output": {"error": f"Unrecognized factor type: {factor_type}"}}
159
+
160
+ # result_key = F_MAPPING[factor_type]
161
+ # if result_key not in tool_result:
162
+ # return {"output": {"error": f"Expected key {result_key} not found in tool result"}}
163
+
164
+ # value = tool_result[result_key]
165
+ # return {"output": {result_key: round(float(value), 2)}}
166
+
167
+ # except (KeyError, TypeError, json.JSONDecodeError, IndexError) as e:
168
+ # return {"output": {"error": f"Result formatting failed: {str(e)}"}}