mirror of
https://github.com/langchain-ai/docs.git
synced 2026-08-27 02:41:59 -04:00
282 lines
8.2 KiB
Plaintext
282 lines
8.2 KiB
Plaintext
<CodeGroup>
|
|
```python Google
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="google_genai:gemini-3.6-flash",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python OpenAI
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="openai:gpt-5.5",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python Anthropic
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="anthropic:claude-sonnet-4-6",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python OpenRouter
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="openrouter:z-ai/glm-5.2",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python Fireworks
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="fireworks:accounts/fireworks/models/glm-5p2",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python Baseten
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="baseten:zai-org/GLM-5.2",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
|
|
```python Ollama
|
|
from langchain.agents.middleware import wrap_tool_call
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
|
|
|
|
@tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather in a city."""
|
|
return f"The weather in {city} is sunny."
|
|
|
|
|
|
call_count = [0] # Use list to allow modification in nested function
|
|
|
|
|
|
@wrap_tool_call
|
|
def log_tool_calls(request, handler):
|
|
"""Intercept and log every tool call - demonstrates cross-cutting concern."""
|
|
call_count[0] += 1
|
|
tool_name = request.name if hasattr(request, "name") else str(request)
|
|
|
|
print(f"[Middleware] Tool call #{call_count[0]}: {tool_name}")
|
|
print(f"[Middleware] Arguments: {request.args if hasattr(request, 'args') else 'N/A'}")
|
|
|
|
# Execute the tool call
|
|
result = handler(request)
|
|
|
|
# Log the result
|
|
print(f"[Middleware] Tool call #{call_count[0]} completed")
|
|
|
|
return result
|
|
|
|
|
|
agent = create_deep_agent(
|
|
model="ollama:north-mini-code-1.0",
|
|
tools=[get_weather],
|
|
middleware=[log_tool_calls],
|
|
)
|
|
```
|
|
</CodeGroup>
|