hibam1
add dynamic_tool_agent
1870dee
Raw History Blame Contribute Delete
1.57 kB
from typing import TypedDict, Annotated, Any
from langchain_core.messages import AnyMessage
from langgraph.graph.message import add_messages
tools_config = [
{
"name": "book_table",
"description": "Book a table at a restaurant",
"parameters": [
{"name": "restaurant", "type": "string", "required": True},
{"name": "people", "type": "integer", "required": True},
{"name": "date", "type": "string", "required": True},
{"name": "time", "type": "string", "required": True}
]
},
{
"name": "check_menu",
"description": "Check if a dish is on the restaurant menu",
"parameters": [
{"name": "restaurant", "type": "string", "required": True},
{"name": "dish", "type": "string", "required": True}
]
}
]
def build_parameter_catalog(tools_config):
catalog = {}
for tool in tools_config:
for param in tool["parameters"]:
name = param["name"]
if name not in catalog:
catalog[name] = {
"type": param["type"]
}
return catalog
parameter_catalog = build_parameter_catalog(tools_config)
def merge_dicts(old: dict, new: dict) -> dict:
return {**old, **new}
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
global_params: Annotated[dict[str, Any], merge_dicts]
selected_tool: str | None
missing_params: list[str]
tool_args: dict[str, Any]
tool_result: dict[str, Any] | None