Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langgraph: allow model names as strings in create_react_agent #3031

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion libs/langgraph/langgraph/prebuilt/chat_agent_executor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import (

Check notice on line 1 in libs/langgraph/langgraph/prebuilt/chat_agent_executor.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 61.7 ms +- 1.4 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 54.2 ms +- 1.0 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 76.1 ms +- 1.7 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 95.7 ms +- 2.1 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 643 ms +- 44 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 530 ms +- 18 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 793 ms +- 50 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 968 ms +- 24 ms ......................................... react_agent_10x: Mean +- std dev: 30.9 ms +- 0.6 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.1 ms +- 0.5 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 38.7 ms +- 1.0 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 37.0 ms +- 0.5 ms ......................................... react_agent_100x: Mean +- std dev: 347 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 274 ms +- 4 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 672 ms +- 25 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 640 ms +- 23 ms ......................................... wide_state_25x300: Mean +- std dev: 23.2 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.3 ms +- 0.3 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 249 ms +- 13 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 246 ms +- 13 ms ......................................... wide_state_15x600: Mean +- std dev: 27.1 ms +- 0.5 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 17.7 ms +- 0.2 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 430 ms +- 14 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 424 ms +- 13 ms ......................................... wide_state_9x1200: Mean +- std dev: 27.2 ms +- 0.6 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 17.9 ms +- 0.4 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 283 ms +- 15 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 279 ms +- 14 ms

Check notice on line 1 in libs/langgraph/langgraph/prebuilt/chat_agent_executor.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+------------------------------------+---------+-----------------------+ | Benchmark | main | changes | +====================================+=========+=======================+ | wide_state_15x600_sync | 17.7 ms | 17.7 ms: 1.00x slower | +------------------------------------+---------+-----------------------+ | wide_state_9x1200_sync | 17.7 ms | 17.9 ms: 1.01x slower | +------------------------------------+---------+-----------------------+ | react_agent_10x_checkpoint | 38.2 ms | 38.7 ms: 1.01x slower | +------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_sync | 53.3 ms | 54.2 ms: 1.02x slower | +------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_checkpoint | 74.6 ms | 76.1 ms: 1.02x slower | +------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_sync | 519 ms | 530 ms: 1.02x slower | +------------------------------------+---------+-----------------------+ | react_agent_100x_checkpoint_sync | 623 ms | 640 ms: 1.03x slower | +------------------------------------+---------+-----------------------+ | react_agent_100x_checkpoint | 649 ms | 672 ms: 1.04x slower | +------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_checkpoint | 764 ms | 793 ms: 1.04x slower | +------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x | 612 ms | 643 ms: 1.05x slower | +------------------------------------+---------+-----------------------+ | Geometric mean | (ref) | 1.01x slower | +------------------------------------+---------+-----------------------+ Benchmark hidden because not significant (18): wide_state_25x300_checkpoint_sync, wide_state_15x600, wide_state_25x300, react_agent_100x_sync, fanout_to_subgraph_10x_checkpoint_sync, react_agent_10x, wide_state_25x300_checkpoint, fanout_to_subgraph_100x_checkpoint_sync, react_agent_10x_checkpoint_sync, wide_state_25x300_sync, wide_state_15x600_checkpoint_sync, wide_state_9x1200, wide_state_15x600_checkpoint, react_agent_10x_sync, fanout_to_subgraph_10x, react_agent_100x, wide_state_9x1200_checkpoint_sync, wide_state_9x1200_checkpoint
Callable,
Literal,
Optional,
Expand Down Expand Up @@ -223,7 +223,7 @@

@deprecated_parameter("messages_modifier", "0.1.9", "state_modifier", removal="0.3.0")
def create_react_agent(
model: LanguageModelLike,
model: Union[str, LanguageModelLike],
tools: Union[ToolExecutor, Sequence[BaseTool], ToolNode],
*,
state_schema: Optional[StateSchemaType] = None,
Expand Down Expand Up @@ -595,6 +595,18 @@
# get the tool functions wrapped in a tool class from the ToolNode
tool_classes = list(tool_node.tools_by_name.values())

if isinstance(model, str):
try:
from langchain.chat_models import ( # type: ignore[import-not-found]
init_chat_model,
)
except ImportError:
raise ImportError(
"Please install langchain (`pip install langchain`) to use '<provider>:<model>' string syntax for `model` parameter."
)

model = cast(BaseChatModel, init_chat_model(model))

tool_calling_enabled = len(tool_classes) > 0

if _should_bind_tools(model, tool_classes) and tool_calling_enabled:
Expand Down
Loading