Update agents/agents_nodes.py
Browse files- agents/agents_nodes.py +31 -75
agents/agents_nodes.py
CHANGED
|
@@ -26,54 +26,19 @@ text_generator = pipeline(
|
|
| 26 |
|
| 27 |
|
| 28 |
llm = HuggingFacePipeline(pipeline=text_generator)
|
| 29 |
-
# llm_instantiated = llm.bind_tools(
|
| 30 |
-
# [time_value_tool],
|
| 31 |
-
# tool_choice={"type": "function", "function": {"name": "time_value_tool"}}
|
| 32 |
-
# )
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# tool_choice={"type": "function", "function": {"name": "time_value_tool"}}
|
| 38 |
-
# )
|
| 39 |
-
# )
|
| 40 |
-
|
| 41 |
-
# llm_instantiated = llm | RunnableLambda(
|
| 42 |
-
# lambda x: x.bind(
|
| 43 |
-
# [time_value_tool],
|
| 44 |
-
# tool_choice={"type": "function", "function": {"name": "time_value_tool"}}
|
| 45 |
-
# )
|
| 46 |
-
# )
|
| 47 |
-
|
| 48 |
-
# Replace this
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# With this
|
| 52 |
-
llm_instantiated = llm.bind(
|
| 53 |
-
tools=[time_value_tool],
|
| 54 |
-
tool_choice="time_value_tool"
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# def agent_node(state: AgentState):
|
| 60 |
-
# response = llm_instantiated.invoke(state["messages"])
|
| 61 |
-
# if not (hasattr(response, 'tool_calls') and response.tool_calls):
|
| 62 |
-
# error_message = AIMessage(content="Error: Model failed to generate tool call.")
|
| 63 |
-
# return {"messages": [error_message]}
|
| 64 |
-
# return {"messages": [response]}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Update tool invocation in agent_node
|
| 68 |
def agent_node(state: AgentState):
|
| 69 |
response = llm_instantiated.invoke(state["messages"])
|
| 70 |
-
if not hasattr(response, 'tool_calls')
|
| 71 |
-
|
| 72 |
-
|
| 73 |
return {"messages": [response]}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
# Tool node executes the tool
|
| 78 |
tool_node = ToolNode([time_value_tool])
|
| 79 |
|
|
@@ -86,46 +51,37 @@ F_MAPPING = {
|
|
| 86 |
|
| 87 |
def format_output(state: AgentState):
|
| 88 |
try:
|
| 89 |
-
#
|
| 90 |
-
if
|
| 91 |
-
return {"output": {"error":
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
#
|
| 98 |
-
if not isinstance(
|
| 99 |
-
return {"output": {"error":
|
| 100 |
-
if not isinstance(second_last_msg, AIMessage):
|
| 101 |
-
return {"output": {"error": f"Second-last message is {type(second_last_msg).__name__}, expected AIMessage"}}
|
| 102 |
-
|
| 103 |
-
# 4. 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 |
-
# 5. Validate tool call structure
|
| 110 |
-
if not hasattr(second_last_msg, 'tool_calls') or not second_last_msg.tool_calls:
|
| 111 |
-
return {"output": {"error": "AIMessage contains no tool calls"}}
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
result_key = F_MAPPING[factor_type]
|
| 123 |
if result_key not in tool_result:
|
| 124 |
-
return {"output": {"error": f"
|
| 125 |
-
|
| 126 |
-
# 8. Format final output
|
| 127 |
value = tool_result[result_key]
|
| 128 |
return {"output": {result_key: round(float(value), 2)}}
|
| 129 |
|
| 130 |
-
except
|
| 131 |
-
return {"output": {"error": f"
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
llm = HuggingFacePipeline(pipeline=text_generator)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
llm_instantiated = llm.bind(
|
| 31 |
+
[time_value_tool],
|
| 32 |
+
tool_choice={"type": "function", "function": {"name": "time_value_tool"}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def agent_node(state: AgentState):
|
| 36 |
response = llm_instantiated.invoke(state["messages"])
|
| 37 |
+
if not (hasattr(response, 'tool_calls') and response.tool_calls):
|
| 38 |
+
error_message = AIMessage(content="Error: Model failed to generate tool call.")
|
| 39 |
+
return {"messages": [error_message]}
|
| 40 |
return {"messages": [response]}
|
| 41 |
|
|
|
|
|
|
|
| 42 |
# Tool node executes the tool
|
| 43 |
tool_node = ToolNode([time_value_tool])
|
| 44 |
|
|
|
|
| 51 |
|
| 52 |
def format_output(state: AgentState):
|
| 53 |
try:
|
| 54 |
+
# The last message should be the ToolMessage (from the tool node)
|
| 55 |
+
if not state["messages"] or not isinstance(state["messages"][-1], ToolMessage):
|
| 56 |
+
return {"output": {"error": "No tool result found in the last message"}}
|
| 57 |
|
| 58 |
+
tool_message = state["messages"][-1]
|
| 59 |
+
# Parse the content of the tool message as JSON
|
| 60 |
+
tool_result = json.loads(tool_message.content)
|
| 61 |
+
|
| 62 |
+
# The second last message should be the AIMessage with the tool call
|
| 63 |
+
if len(state["messages"]) < 2 or not isinstance(state["messages"][-2], AIMessage):
|
| 64 |
+
return {"output": {"error": "No AI message (with tool call) found before the tool message"}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
ai_message = state["messages"][-2]
|
| 67 |
+
if not ai_message.tool_calls:
|
| 68 |
+
return {"output": {"error": "The AI message does not contain tool calls"}}
|
| 69 |
|
| 70 |
+
# We take the first tool call (since we forced one tool)
|
| 71 |
+
tool_call = ai_message.tool_calls
|
| 72 |
+
args = tool_call["args"]
|
| 73 |
+
|
| 74 |
+
# Get the factor type from the args
|
| 75 |
+
factor_type = args["F"]
|
| 76 |
+
if factor_type not in F_MAPPING:
|
| 77 |
+
return {"output": {"error": f"Unrecognized factor type: {factor_type}"}}
|
| 78 |
|
| 79 |
result_key = F_MAPPING[factor_type]
|
| 80 |
if result_key not in tool_result:
|
| 81 |
+
return {"output": {"error": f"Expected key {result_key} not found in tool result"}}
|
| 82 |
+
|
|
|
|
| 83 |
value = tool_result[result_key]
|
| 84 |
return {"output": {result_key: round(float(value), 2)}}
|
| 85 |
|
| 86 |
+
except (KeyError, TypeError, json.JSONDecodeError, IndexError) as e:
|
| 87 |
+
return {"output": {"error": f"Result formatting failed: {str(e)}"}}
|