### Install Full Functionality with UV Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Installs langchain-dev-utils and its 'standard' extras using uv. This method ensures that all dependencies required for the full feature set are included. ```shell uv add langchain-dev-utils[standard] ``` -------------------------------- ### Verify Langchain-dev-utils Installation Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Checks if the langchain-dev-utils package has been successfully installed by importing it and printing its version number. This is a simple verification step. ```python import langchain_dev_utils print(langchain_dev_utils.__version__) ``` -------------------------------- ### Install Full Functionality with Pip Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Installs langchain-dev-utils along with its 'standard' extras, which include additional dependencies like langchain-openai. Use this command for full package functionality. ```shell pip install -U langchain-dev-utils[standard] ``` -------------------------------- ### Setup and Run Tests with UV Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Clones the langchain-dev-utils repository, changes directory, synchronizes test dependencies using uv, and then runs the project's tests. Requires git and uv. ```bash git clone https://github.com/TBice123123/langchain-dev-utils.git cd langchain-dev-utils uv sync --group test uv run pytest . ``` -------------------------------- ### Install Full Functionality with Poetry Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Adds langchain-dev-utils with its 'standard' extras to a Poetry-managed project. This ensures all optional dependencies for full functionality are included. ```shell poetry add langchain-dev-utils[standard] ``` -------------------------------- ### Install langchain-dev-utils with UV Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Installs the langchain-dev-utils package and its core dependencies using the uv package manager. uv is known for its speed and efficiency in handling Python packages. ```shell uv add langchain-dev-utils ``` -------------------------------- ### Install langchain-dev-utils with Pip Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Installs the latest version of the langchain-dev-utils package and its core dependencies using pip. This is a standard method for Python package management. ```shell pip install -U langchain-dev-utils ``` -------------------------------- ### Install langchain-dev-utils with Poetry Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/installation Adds the langchain-dev-utils package and its core dependencies to a project managed by Poetry. This ensures proper dependency resolution within the Poetry ecosystem. ```shell poetry add langchain-dev-utils ``` -------------------------------- ### Batch Registering Model Providers (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates using `batch_register_model_provider` to register multiple model providers efficiently. This function accepts a list of provider configurations, simplifying the setup process. ```python from langchain_dev_utils.chat_models import batch_register_model_provider from langchain_core.language_models.fake_chat_models import FakeChatModel batch_register_model_provider( providers=[ { "provider_name": "fake_provider", "chat_model": FakeChatModel, }, { "provider_name": "vllm", "chat_model": "openai-compatible", "base_url": "http://localhost:8000/v1", }, ] ) ``` -------------------------------- ### Loading a Chat Model Instance (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Provides examples of using the `load_chat_model` function to instantiate a chat model. It shows two methods: specifying the provider and model name together, or separately using the `model_provider` argument. ```python # Method 1 model = load_chat_model("vllm:qwen3-4b") ``` ```python # Method 2 model = load_chat_model("qwen3-4b", model_provider="vllm") ``` -------------------------------- ### Example: Registering FakeChatModel Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates how to register a fake chat model for testing purposes using `register_model_provider`. ```APIDOC ## Python Example: Registering FakeChatModel ### Description This example shows how to register a `FakeChatModel` from `langchain_core.language_models.fake_chat_models` using the `register_model_provider` function from `langchain_dev_utils.chat_models`. ### Method N/A (Code Snippet) ### Endpoint N/A (Code Snippet) ### Parameters None ### Request Example ```python from langchain_core.language_models.fake_chat_models import FakeChatModel from langchain_dev_utils.chat_models import register_model_provider register_model_provider( provider_name="fake_provider", chat_model=FakeChatModel, ) ``` ### Response None (This is a code example for registration.) ``` -------------------------------- ### Enable ModelRouterMiddleware in Agent Creation Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware This Python code demonstrates how to integrate ModelRouterMiddleware when creating a Langchain agent. It shows the necessary imports and how to pass the `router_model` and `model_list` to the middleware during agent initialization. The example also illustrates how the middleware dynamically selects the appropriate model for a given task, such as code generation. ```python from langchain_dev_utils.agents.middleware import ModelRouterMiddleware from langchain_core.messages import HumanMessage agent = create_agent( model="vllm:qwen3-4b", # This model is only a placeholder, actually replaced dynamically by middleware tools=[run_python_code, get_current_time], middleware=[ ModelRouterMiddleware( router_model="vllm:qwen3-4b", model_list=model_list, ) ], ) # The routing middleware will automatically select the most suitable model based on the input content response = agent.invoke({"messages": [HumanMessage(content="Help me write a bubble sort code")]}) print(response) ``` -------------------------------- ### Wrap Agent as Tool for Multi-Agent Scenarios Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/prebuilt Illustrates how to wrap a pre-created LangChain agent (like `time_agent`) into a tool using `wrap_agent_as_tool`. This wrapped tool can then be used by another agent in a multi-agent system. The example shows creating a time agent, wrapping it, and then using the wrapped tool within a main agent. ```python import datetime from langchain_core.messages import HumanMessage from langchain_core.tools import tool from langchain_dev_utils.agents import wrap_agent_as_tool, create_agent @tool def get_current_time() -> str: """Get current timestamp""" return str(datetime.datetime.now().timestamp()) # Define an agent for querying time time_agent = create_agent( "vllm:qwen3-4b", tools=[get_current_time], name="time_agent" ) call_time_agent_tool = wrap_agent_as_tool( time_agent, tool_name="call_time_agent", tool_description="Call time agent" ) # Use it as a tool agent = create_agent("vllm:qwen3-4b", tools=[call_time_agent_tool], name="agent") response = agent.invoke({"messages": [HumanMessage(content="What time is it now?")]}) print(response) ``` -------------------------------- ### Define Model List for Routing Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware This Python code defines a list of model configurations for the ModelRouterMiddleware. Each configuration includes the model name, a description of its capabilities, optional model arguments, a system prompt, and a list of tools it can access. This setup is crucial for enabling dynamic model selection. ```python model_list = [ { "model_name": "vllm:qwen3-8b", "model_description": "Suitable for ordinary tasks, such as dialogue, text generation, etc.", "model_kwargs": { "temperature": 0.7, "extra_body": {"chat_template_kwargs": {"enable_thinking": False}} }, "model_system_prompt": "You are an assistant, good at handling ordinary tasks, such as dialogue, text generation, etc.", }, { "model_name": "openrouter:qwen/qwen3-vl-32b-instruct", "model_description": "Suitable for visual tasks", "tools": [], # If this model does not need any tools, please set this field to an empty list [] }, { "model_name": "openrouter:qwen/qwen3-coder-plus", "model_description": "Suitable for code generation tasks", "tools": [run_python_code], # Only allows the use of run_python_code tool }, ] ``` -------------------------------- ### Example Message History with Reasoning Content (JSON) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates the structure of message history when `keep_reasoning_content` is set to `False` (default) versus `True`. When `True`, the `reasoning_content` field is included in assistant messages. ```json [ { "role": "user", "content": "Hello" }, { "role": "assistant", "content": "Hello! How can I help you today?" } ] ``` ```json [ { "role": "user", "content": "Hello" }, { "role": "assistant", "content": "Hello! How can I help you today?", "reasoning_content": "The user said 'Hello', which is a greeting. I should respond politely and proactively ask how I can assist." } ] ``` -------------------------------- ### Basic Invocation with invoke() Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates how to perform a simple synchronous invocation of a chat model using the `invoke` method. It requires loading a chat model and passing a list of messages, typically starting with a HumanMessage. ```python from langchain_dev_utils.chat_models import load_chat_model from langchain_core.messages import HumanMessage model = load_chat_model("vllm:qwen3-4b") response = model.invoke([HumanMessage("Hello")]) print(response) ``` -------------------------------- ### Create Single Agent with Custom Model Provider Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/prebuilt Demonstrates how to create a single LangChain agent using `create_agent` with a custom model provider (`vllm`). It includes registering the model provider and defining a simple tool for getting the current time. The agent can then be invoked with messages. ```python from langchain_core.messages import HumanMessage from langchain_dev_utils.chat_models import register_model_provider from langchain_dev_utils.agents import create_agent from langchain_core.tools import tool import datetime # Register model provider register_model_provider( provider_name="vllm", chat_model="openai-compatible", base_url="http://localhost:8000/v1", ) @tool def get_current_time() -> str: """Get current timestamp""" return str(datetime.datetime.now().timestamp()) agent = create_agent("vllm:qwen3-4b", tools=[get_current_time]) # Usage is exactly the same as langchain's create_agent response = agent.invoke({"messages": [HumanMessage(content="What time is it now?")]}) print(response) ``` -------------------------------- ### Initialize ModelRouterMiddleware Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Initializes the ModelRouterMiddleware for dynamic model routing. It requires a router model and a list of available models, each with a name and description. Optional parameters include tools, model arguments, and system prompts. ```python class ModelRouterMiddleware(AgentMiddleware): state_schema = ModelRouterState def __init__( self, router_model: str | BaseChatModel, model_list: list[ModelDict], router_prompt: Optional[str] = None, ) -> None: pass ``` ```python model_router_middleware = ModelRouterMiddleware( router_model="vllm:qwen3-4b", model_list=[ { "model_name": "vllm:qwen3-4b", "model_description": "Suitable for general tasks like dialogue, text generation, etc." }, { "model_name": "vllm:qwen3-8b", "model_description": "Suitable for complex tasks like code generation, data analysis, etc.", }, ] ) ``` -------------------------------- ### Create Tools for Plan Management in Python Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware Demonstrates how to create individual tools for writing, finishing, and reading plan subtasks. These tools are essential for the PlanMiddleware but can be used independently if needed. Ensure the state schema includes the plan key. ```python from langchain_dev_utils.agents.middleware.plan import ( create_write_plan_tool, create_finish_sub_plan_tool, create_read_plan_tool, PlanState, ) # Example of creating individual tools write_tool = create_write_plan_tool() finish_tool = create_finish_sub_plan_tool() read_tool = create_read_plan_tool() # Example agent setup using these tools (not recommended best practice) # agent = create_agent( # model="vllm:qwen3-4b", # state_schema=PlanState, # tools=[write_tool, finish_tool, read_tool], # ) ``` -------------------------------- ### Implement Task Planning with PlanMiddleware in Python Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware Shows the recommended way to use PlanMiddleware for agent task planning. This approach simplifies agent initialization by automatically handling prompt construction and state management. The middleware utilizes `write_plan` and `finish_sub_plan` tools by default, and `read_plan` is enabled unless explicitly disabled. ```python from langchain_dev_utils.agents.middleware import PlanMiddleware from langchain_core.messages import HumanMessage # Example agent setup using PlanMiddleware (recommended best practice) agent = create_agent( model="vllm:qwen3-4b", middleware=[ PlanMiddleware( use_read_plan_tool=True, # Set to False if read_plan tool is not needed # Optional parameters like system_prompt, write_plan_tool_description, etc. can be passed here ) ], ) # Example agent invocation response = agent.invoke( {"messages": [HumanMessage(content="I want to visit New York for a few days, help me plan my itinerary")]} ) print(response) ``` -------------------------------- ### Custom Asynchronous Handler for Human-in-the-Loop Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/tool-calling/human-in-the-loop Provides an example of a custom asynchronous handler (`custom_handler`) for the `human_in_the_loop_async` decorator. This handler demonstrates how to implement specific interrupt behaviors, such as only allowing 'accept' or 'reject' actions, and customizing the interrupt prompt. It utilizes LangGraph's `interrupt` function. ```python from typing import Any from langchain_dev_utils import human_in_the_loop_async, InterruptParams from langgraph.types import interrupt async def custom_handler(params: InterruptParams) -> Any: response = interrupt( f"I'm about to call tool {params['tool_call_name']} with parameters {params['tool_call_args']}, please confirm if I should proceed" ) if response["type"] == "accept": return await params["tool"].ainvoke(params["tool_call_args"]) elif response["type"] == "reject": return "User rejected calling this tool" else: raise ValueError(f"Unsupported response type: {response['type']}") @human_in_the_loop_async(handler=custom_handler) @tool async def get_weather(city: str) -> str: """Get weather information""" return f"The weather in {city} is sunny" ``` -------------------------------- ### LLMToolEmulator for Simulating Tool Calls Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware Demonstrates using LLMToolEmulator middleware to simulate tool interactions with large language models. This is useful for testing agent logic without actual tool execution. It requires specifying the model to be used for emulation. ```python from langchain_core.messages import HumanMessage from langchain_core.tools import tool from langchain_dev_utils.agents.middleware import ( LLMToolEmulator, ) # Assume create_agent is defined elsewhere and works with middleware # For demonstration, we'll use a placeholder def create_agent(**kwargs): class MockAgent: def invoke(self, input): print(f"MockAgent invoked with: {input}") return "Mock Response" return MockAgent() @tool def get_current_time() -> str: """Get current time""" return "14:00" agent = create_agent( model="vllm:qwen3-4b", tools=[get_current_time], middleware=[ LLMToolEmulator( model="vllm:qwen3-4b" ) ], ) response = agent.invoke({"messages": [HumanMessage(content="What time is it now?")]}) print(response) ``` -------------------------------- ### Provider Registration with Default Supported Tool Choice (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Shows how to register a model provider with default configuration values for supported parameters like `supported_tool_choice`. This sets a baseline for all models from that provider. ```python register_model_provider( ..., provider_config={"supported_tool_choice": ["auto", "none", "required"]}, ) ``` -------------------------------- ### Registering a Fake Chat Model Provider Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat This snippet demonstrates how to register a custom chat model provider using `register_model_provider`. It's suitable for scenarios where a LangChain-compatible chat model class is available. The example uses `FakeChatModel` for illustrative purposes, which is intended for testing. In production, a functional `ChatModel` class should be used. ```python from langchain_core.language_models.fake_chat_models import FakeChatModel from langchain_dev_utils.chat_models import register_model_provider register_model_provider( provider_name="fake_provider", chat_model=FakeChatModel, ) ``` -------------------------------- ### Create Read Plan Tool for Execution Status Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Creates a tool designed to read the execution status of a plan. It accepts an optional description string. The function returns an instance of BaseTool. ```python def create_read_plan_tool( description: Optional[str] = None, ) -> BaseTool: # ... function implementation ... ``` -------------------------------- ### Create Tool for Writing or Updating Plans (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent The `create_write_plan_tool` function generates a BaseTool designed for writing or updating plans. It accepts an optional tool description and a message key, which defaults to 'messages', for updating messages associated with the plan. ```python def create_write_plan_tool( description: Optional[str] = None, message_key: Optional[str] = None, ) -> BaseTool: """Creates a tool for writing or updating plans.""" pass ``` ```python write_plan_tool = create_write_plan_tool() ``` -------------------------------- ### Load Custom Chat Model with vLLM Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates loading a chat model using vLLM with the `langchain_dev_utils.chat_models.load_chat_model` function. This requires the `vllm` provider to be available and configured. ```python from langchain_dev_utils.chat_models import load_chat_model from langchain_core.messages import HumanMessage model = load_chat_model("vllm:qwen3-4b", use_responses_api=True) response = model.invoke([HumanMessage(content="Hello")]) print(response) ``` -------------------------------- ### LLMToolEmulator Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Middleware for using large language models to emulate tool calls. This allows LLMs to perform actions that would typically require predefined tools. ```APIDOC ## LLMToolEmulator ### Description Middleware for using large language models to emulate tool calls. ### Method N/A (Class) ### Endpoint N/A (Class) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python llm_tool_emulator = LLMToolEmulator(model="vllm:qwen3-4b", tools=[get_current_time]) ``` ### Response #### Success Response (200) - **middleware_instance** (LLMToolEmulator) - An instance of the LLM tool emulator middleware. #### Response Example ```json { "middleware_instance": "" } ``` ``` -------------------------------- ### Plan Middleware for Agent Plan Management Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent A middleware class for managing agent plans. It supports system prompts and descriptions for tools related to writing, finishing, and reading plans. It can optionally use a read plan tool and specifies a message key for updates. ```python class PlanMiddleware(AgentMiddleware): state_schema = PlanState def __init__( self, *, system_prompt: Optional[str] = None, write_plan_tool_description: Optional[str] = None, finish_sub_plan_tool_description: Optional[str] = None, read_plan_tool_description: Optional[str] = None, use_read_plan_tool: bool = True, message_key: Optional[str] = None, ) -> None: # ... constructor implementation ... ``` -------------------------------- ### Create Agent with Extended Model Specification (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent The `create_agent` function from Langchain-dev-utils provides functionality identical to LangChain's `create_agent` but allows for extended model specification via strings. It takes a model string, a list of tools, and various optional parameters for system prompts, middleware, response formats, state schemas, context schemas, checkpointers, stores, interrupt points, debug mode, names, and caching. It returns a CompiledStateGraph. ```python def create_agent( model: str, tools: Sequence[BaseTool | Callable | dict[str, Any]] | None = None, *, system_prompt: str | None = None, middleware: Sequence[AgentMiddleware[AgentState[ResponseT], ContextT]] = (), response_format: ResponseFormat[ResponseT] | type[ResponseT] | None = None, state_schema: type[AgentState[ResponseT]] | None = None, context_schema: type[ContextT] | None = None, checkpointer: Checkpointer | None = None, store: BaseStore | None = None, interrupt_before: list[str] | None = None, interrupt_after: list[str] | None = None, debug: bool = False, name: str | None = None, cache: BaseCache | None = None, ) -> CompiledStateGraph: """Prebuilt agent function that provides identical functionality to the official LangChain `create_agent`, but extends model specification via strings.""" pass ``` ```python agent = create_agent(model="vllm:qwen3-4b", tools=[get_current_time]) ``` -------------------------------- ### Sequential Graph Construction with StateGraph (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/graph-orchestration/pipeline Demonstrates an alternative method for building sequential workflows using LangGraph's `StateGraph`. This approach involves explicitly adding nodes and edges, which can be more verbose than `create_sequential_pipeline` for simple sequential execution. ```python from langgraph.graph import StateGraph, END from langgraph.checkpoint.memory import MemorySaver # Assume AgentState, graph1, graph2, graph3 are defined elsewhere # Placeholder for AgentState schema (replace with actual definition) class AgentState: messages: list # Placeholder for graph objects (replace with actual graph definitions) graph1 = None graph2 = None graph3 = None graph = StateGraph(AgentState) graph.add_sequence([("graph1", graph1), ("graph2", graph2), ("graph3", graph3)]) graph.add_edge("__start__", "graph1") graph = graph.compile() ``` -------------------------------- ### Configure vLLM for OpenAI-Compatible API (Bash) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Launch a vLLM service with an OpenAI-compatible API endpoint. This command specifies the model to serve, enables reasoning parsing, auto tool choice, and sets the host and port for the API. The service address will be http://localhost:8000/v1. ```bash vllm serve Qwen/Qwen3-4B \ --reasoning-parser qwen3 \ --enable-auto-tool-choice --tool-call-parser hermes \ --host 0.0.0.0 --port 8000 \ --served-model-name qwen3-4b ``` -------------------------------- ### Tool Calling API Reference Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/tool_calling Reference for functions and decorators related to tool calling in Langchain. ```APIDOC ## has_tool_calling ### Description Checks if a message contains tool calls. ### Method N/A (Python function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **message** (AIMessage) - Required - The message to be checked. ### Request Example ```python if has_tool_calling(response): # Handle tool calls pass ``` ### Response #### Success Response (200) - **Return Value** (bool) - Returns True if the message contains tool calls, otherwise returns False. #### Response Example N/A ``` ```APIDOC ## parse_tool_calling ### Description Parses tool call arguments from a message. ### Method N/A (Python function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **message** (AIMessage) - Required - The message to be parsed. - **first_tool_call_only** (bool) - Optional - Whether to return only the first tool call, defaults to False. ### Request Example ```python # Get all tool calls tool_calls = parse_tool_calling(response) # Get only the first tool call name, args = parse_tool_calling(response, first_tool_call_only=True) ``` ### Response #### Success Response (200) - **Return Value** (Union[tuple[str, dict], list[tuple[str, dict]]]) - A tuple of tool call name and parameters, or a list of tuples containing tool call names and parameters. #### Response Example N/A ``` ```APIDOC ## human_in_the_loop ### Description A decorator that adds "human-in-the-loop" manual review capability to synchronous tool functions. ### Method N/A (Python decorator) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **func** (Optional[Callable]) - Optional - The synchronous function to be decorated (decorator syntax sugar). - **handler** (Optional[HumanInterruptHandler]) - Optional - Custom interrupt handler function. ### Request Example ```python @human_in_the_loop def get_current_time(): """Get current time""" return datetime.now().strftime("%Y-%m-%d %H:%M:%S") ``` ### Response #### Success Response (200) - **Return Value** (Union[Callable[[Callable], BaseTool], BaseTool]) - BaseTool type, the decorated tool instance. #### Response Example N/A ``` ```APIDOC ## human_in_the_loop_async ### Description A decorator that adds "human-in-the-loop" manual review capability to asynchronous tool functions. ### Method N/A (Python decorator) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **func** (Optional[Callable]) - Optional - The asynchronous function to be decorated (decorator syntax sugar). - **handler** (Optional[HumanInterruptHandler]) - Optional - Custom interrupt handler function. ### Request Example ```python @human_in_the_loop_async async def get_current_time(): """Get current time""" return datetime.now().strftime("%Y-%m-%d %H:%M:%S") ``` ### Response #### Success Response (200) - **Return Value** (Union[Callable[[Callable], BaseTool], BaseTool]) - BaseTool type, the decorated asynchronous tool instance. #### Response Example N/A ``` ```APIDOC ## InterruptParams ### Description Parameter type passed to interrupt handler functions. ### Method N/A (Python TypedDict) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **tool_call_name** (str) - Tool call name. - **tool_call_args** (Dict[str, Any]) - Tool call arguments. - **tool** (BaseTool) - Tool instance. ### Request Example N/A ### Response #### Success Response (200) - **Field Descriptions**: - `tool_call_name` (str) - Tool call name. - `tool_call_args` (Dict[str, Any]) - Tool call arguments. - `tool` (BaseTool) - Tool instance. #### Response Example N/A ``` ```APIDOC ## HumanInterruptHandler ### Description Type alias for interrupt handler functions. ### Method N/A (Python Type Alias) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) - **Type**: Callable[[InterruptParams], Any] #### Response Example N/A ``` -------------------------------- ### Create Tool for Finishing Sub-Plan Execution (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent The `create_finish_sub_plan_tool` function creates a BaseTool that updates the execution status after completing subtasks. Similar to `create_write_plan_tool`, it accepts an optional description and a message key, defaulting to 'messages'. ```python def create_finish_sub_plan_tool( description: Optional[str] = None, message_key: Optional[str] = None, ) -> BaseTool: """Creates a tool for updating execution status after completing subtasks.""" pass ``` ```python finish_sub_plan_tool = create_finish_sub_plan_tool() ``` -------------------------------- ### Synchronous Tool with Human-in-the-Loop Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/tool-calling/human-in-the-loop Demonstrates how to apply the `human_in_the_loop` decorator to a synchronous tool function. This enables human approval before the tool's execution. It requires the `langchain_dev_utils` and `langchain_core.tools` libraries. ```python from langchain_dev_utils import human_in_the_loop from langchain_core.tools import tool import datetime @human_in_the_loop @tool def get_current_time() -> str: """Get current timestamp""" return str(datetime.datetime.now().timestamp()) ``` -------------------------------- ### Wrap Agent as Tool - wrap_agent_as_tool Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/prebuilt The `wrap_agent_as_tool` function enables wrapping a LangGraph `CompiledStateGraph` agent into a tool that can be utilized by other agents in a multi-agent system. ```APIDOC ## POST /wrap_agent_as_tool ### Description Wraps an agent instance into a tool that can be called by other agents. ### Method POST ### Endpoint /wrap_agent_as_tool #### Parameters ##### Request Body - **agent** (object) - Required - The agent to wrap, must be a LangGraph `CompiledStateGraph`. - **tool_name** (string) - Optional - The name for the tool. Defaults to `transfor_to_agent_name`. - **tool_description** (string) - Optional - The description for the tool. Defaults to a predefined content. - **pre_input_hooks** (function | tuple) - Optional - Pre-processing hook(s) for agent input (synchronous or async). - **post_output_hooks** (function | tuple) - Optional - Post-processing hook(s) for agent output (synchronous or async). ### Request Example ```json { "agent": "", "tool_name": "call_time_agent", "tool_description": "Call time agent" } ``` ### Response #### Success Response (200) - **tool** (object) - The wrapped agent as a tool. #### Response Example ```json { "tool": "" } ``` ``` -------------------------------- ### Postprocess Agent Output with Python Hooks Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/prebuilt Provides synchronous and asynchronous functions to post-process agent output, extracting specific information or transforming it into a `Command` object. These hooks are essential for structuring the agent's final response. ```python from langgraph.types import Command def process_output_sync(request: str, messages: list, runtime: ToolRuntime) -> Command: return Command(update={ "messages":[ToolMessage(content=messages[-1].content, tool_call_id=runtime.tool_call_id)] }) async def process_output_async(request: str, messages: list, runtime: ToolRuntime) -> Command: return Command(update={ "messages":[ToolMessage(content=messages[-1].content, tool_call_id=runtime.tool_call_id)] }) # Usage call_agent_tool = wrap_agent_as_tool( agent, post_output_hooks=(process_output_sync, process_output_async) ) ``` -------------------------------- ### Using OpenAI's Latest responses_api Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Explains how to utilize OpenAI's latest `responses_api` with compatible model providers. If a provider supports this API style, you can enable it by passing `use_responses_api=True` during model configuration, as demonstrated with vLLM. ```python # Example demonstrating the concept, actual implementation might vary based on provider support. # from langchain_dev_utils.chat_models import load_chat_model # # model = load_chat_model("vllm:some-model", provider_config={"use_responses_api": True}) # response = model.invoke(...) # print(response) ``` -------------------------------- ### PlanMiddleware Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Middleware for agent plan management. This middleware assists in managing an agent's plan, including creating, updating, and reading plan steps. ```APIDOC ## PlanMiddleware ### Description Middleware for agent plan management. ### Method N/A (Class) ### Endpoint N/A (Class) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python plan_middleware = PlanMiddleware() ``` ### Response #### Success Response (200) - **middleware_instance** (PlanMiddleware) - An instance of the plan middleware. #### Response Example ```json { "middleware_instance": "" } ``` ``` -------------------------------- ### LLMToolSelectorMiddleware for Tool Selection Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware Illustrates the use of LLMToolSelectorMiddleware to enable an LLM to select tools when multiple options are available. This middleware helps manage tool usage by limiting the number of tools the LLM can choose from. It requires the model name and the maximum number of tools to consider. ```python from langchain_core.messages import HumanMessage from langchain_dev_utils.agents.middleware import ( LLMToolSelectorMiddleware, ) from langchain_core.tools import tool # Assume create_agent is defined elsewhere and works with middleware # For demonstration, we'll use a placeholder def create_agent(**kwargs): class MockAgent: def invoke(self, input): print(f"MockAgent invoked with: {input}") return "Mock Response" return MockAgent() @tool def get_current_time() -> str: """Get current time""" return "14:00" @tool def get_current_weather() -> str: """Get current weather""" return "Today's weather is sunny" @tool def search() -> str: """Search""" return "Search results" @tool def run_python() -> str: """Run Python code""" return "Running Python code" agent = create_agent( "vllm:qwen3-4b", tools=[get_current_time, get_current_weather, search, run_python], name="agent", middleware=[ LLMToolSelectorMiddleware(model="vllm:qwen3-4b", max_tools=2), ], ) response = agent.invoke({"messages": [HumanMessage(content="What time is it now?")]}) print(response) ``` -------------------------------- ### Load a Chat Model Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Instantiate a chat model by its registered name and provider. ```APIDOC ## POST /load_chat_model ### Description Loads and instantiates a chat model based on the provided model name and optionally, the model provider. Supports passing additional keyword arguments for model configuration. ### Method POST ### Endpoint /load_chat_model ### Parameters #### Request Body - **model** (string) - Required - The name of the chat model to load. - **model_provider** (string) - Optional - The name of the registered model provider. If not specified, the system will attempt to find the model using default mechanisms. - **kwargs** (any) - Optional - Additional keyword arguments to configure the chat model (e.g., `temperature`, `max_tokens`, `extra_body`). ### Request Example ```json { "model": "gpt-3.5-turbo", "model_provider": "openai", "temperature": 0.7, "max_tokens": 150 } ``` ### Response #### Success Response (200) - **chat_model_instance** (object) - An instance of the loaded chat model. #### Response Example ```json { "chat_model_instance": "" } ``` ``` -------------------------------- ### Tool Calling with bind_tools() Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Shows how to enable tool calling capabilities for a chat model using the `bind_tools` method. This requires defining tools (e.g., using `@tool`) and binding them to the model before invocation. The model then decides when to use these tools based on the user's prompt. ```python from langchain_dev_utils.chat_models import load_chat_model from langchain_core.messages import HumanMessage from langchain_core.tools import tool import datetime @tool def get_current_time() -> str: """Get the current timestamp""" return str(datetime.datetime.now().timestamp()) model = load_chat_model("vllm:qwen3-4b").bind_tools([get_current_time]) response = model.invoke([HumanMessage("Get the current timestamp")]) print(response) ``` -------------------------------- ### Format List of Strings with Numbered Prefix and Custom Separator - Python Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/message-conversion/format Shows how to format a list of simple strings into a single string, including a numeric prefix for each item and a custom separator '>'. This is ideal for creating ordered lists or numbered steps from plain text inputs. ```python format3 = format_sequence( [ "str1", "str2", "str3", ], separator=">", with_num=True, ) print(format3) ``` -------------------------------- ### Batch Register Chat Model Providers Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/chat_model Registers multiple chat model providers simultaneously using a list of ChatModelProvider configurations. This is useful for setting up several models at once. ```python def batch_register_model_provider( providers: list[ChatModelProvider], ) -> None: # Implementation details omitted for brevity ``` -------------------------------- ### Summarization Middleware for Agent Context Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Implements middleware for intelligent agent context summarization. It requires a model identifier and offers configuration for trigger conditions, context to keep, token counting, and summarization prompts. This class extends _SummarizationMiddleware. ```python class SummarizationMiddleware(_SummarizationMiddleware): def __init__( self, model: str, *, trigger: ContextSize | list[ContextSize] | None = None, keep: ContextSize = ("messages", _DEFAULT_MESSAGES_TO_KEEP), token_counter: TokenCounter = count_tokens_approximately, summary_prompt: str = DEFAULT_SUMMARY_PROMPT, trim_tokens_to_summarize: int | None = _DEFAULT_TRIM_TOKEN_LIMIT, **deprecated_kwargs: Any, ) -> None: # ... constructor implementation ... ``` -------------------------------- ### Create Sequential Pipeline using Python Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/pipeline Combines multiple subgraphs with identical states into a sequential pipeline. Requires a list of SubGraph objects and a state schema. Optional parameters include graph name, context, input, output schemas, and LangGraph components like checkpointer, store, and cache. ```python def create_sequential_pipeline( sub_graphs: list[SubGraph], state_schema: type[StateT], graph_name: Optional[str] = None, context_schema: type[ContextT] | None = None, input_schema: type[InputT] | None = None, output_schema: type[OutputT] | None = None, checkpointer: Checkpointer | None = None, store: BaseStore | None = None, cache: BaseCache | None = None, ) -> CompiledStateGraph[StateT, ContextT, InputT, OutputT]: # Function implementation details ``` ```python create_sequential_pipeline( sub_graphs=[graph1, graph2], state_schema=State, graph_name="sequential_pipeline", context_schema=Context, input_schema=Input, output_schema=Output, ) ``` -------------------------------- ### Basic Invocation and Asynchronous Invocation Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Demonstrates how to invoke a chat model for basic and asynchronous calls using the `invoke` and `ainvoke` methods. ```APIDOC ## Basic Invocation ### Description Supports `invoke` for simple, synchronous calls to the chat model. ### Method `invoke` ### Parameters - `messages` (list) - Required - A list of messages to send to the model. ### Request Example ```python from langchain_dev_utils.chat_models import load_chat_model from langchain_core.messages import HumanMessage model = load_chat_model("vllm:qwen3-4b") response = model.invoke([HumanMessage("Hello")]) print(response) ``` ## Asynchronous Invocation ### Description Supports `ainvoke` for asynchronous calls to the chat model, allowing non-blocking operations. ### Method `ainvoke` ### Parameters - `messages` (list) - Required - A list of messages to send to the model. ### Request Example ```python from langchain_dev_utils.chat_models import load_chat_model from langchain_core.messages import HumanMessage model = load_chat_model("vllm:qwen3-4b") response = await model.ainvoke([HumanMessage("Hello")]) print(response) ``` ``` -------------------------------- ### Batch Register Model Providers Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/chat_model Batch registers multiple model providers simultaneously. This is useful for setting up several chat model integrations at once using a list of provider configurations. ```APIDOC ## POST /batch_register_model_provider ### Description Batch registers model providers. ### Method POST ### Endpoint /batch_register_model_provider ### Parameters #### Request Body - **providers** (list[ChatModelProvider]) - Required - List of provider configurations ### Request Example ```json [ { "provider_name": "fakechat", "chat_model": "FakeChatModel" }, { "provider_name": "vllm", "chat_model": "openai-compatible", "base_url": "http://localhost:8000/v1" } ] ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message #### Response Example ```json { "message": "Model providers registered successfully." } ``` ``` -------------------------------- ### Load Officially Supported OpenAI Chat Model Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/model-management/chat Shows how to load an officially supported OpenAI chat model using `langchain_dev_utils.chat_models.load_chat_model`. This leverages LangChain's built-in support for providers like OpenAI. ```python model = load_chat_model("openai:gpt-4o-mini") # or model = load_chat_model("gpt-4o-mini", model_provider="openai") ``` -------------------------------- ### ModelFallbackMiddleware for Model Failure Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/agent-development/middleware Shows how to implement ModelFallbackMiddleware to switch to a backup model when the primary model call fails. This ensures service continuity by providing alternative model options. It takes the primary model string and one or more fallback model strings as arguments. ```python from langchain_core.messages import HumanMessage from langchain_dev_utils.agents.middleware import ( ModelFallbackMiddleware, ) # Assume create_agent is defined elsewhere and works with middleware # For demonstration, we'll use a placeholder def create_agent(**kwargs): class MockAgent: def invoke(self, input): print(f"MockAgent invoked with: {input}") return "Mock Response" return MockAgent() agent = create_agent( model="vllm:qwen3-4b", middleware=[ ModelFallbackMiddleware( "vllm:qwen3-8b", "openrouter:meta-llama/llama-3.3-8b-instruct:free", ) ], ) response = agent.invoke({"messages": [HumanMessage(content="Hello.")]}) print(response) ``` -------------------------------- ### Create Parallel Pipeline for Agent Workflows (Python) Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/graph-orchestration/pipeline Demonstrates how to use `create_parallel_pipeline` to combine agent-based state graphs for parallel development of e-commerce modules. It defines tools for each module and assigns them to specialized agents, executing concurrently to improve efficiency. ```python from langchain_dev_utils.pipeline import create_parallel_pipeline from langgraph.graph import StateGraph from langgraph.checkpoint.base import BaseCheckpointSaver from langgraph.store.base import BaseStore from langgraph.cache.base import BaseCache from typing import Any, Callable, List from langgraph.types import Send # Assuming create_agent, AgentState, tool, HumanMessage are defined elsewhere # For demonstration purposes, let's define placeholders def tool(func): return func class HumanMessage: def __init__(self, content): self.content = content class AgentState: pass def create_agent(**kwargs): # Placeholder for create_agent function print(f"Creating agent with: {kwargs}") return StateGraph([]) # Dummy graph @tool def develop_user_module(): """Develop user module functionality""" return "User module development completed, including registration, login, and personal profile management functions." @tool def develop_product_module(): """Develop product module functionality""" return "Product module development completed, including product display, search, and categorization functions." @tool def develop_order_module(): """Develop order module functionality""" return "Order module development completed, including order placement, payment, and order inquiry functions." # Build parallel workflow (pipeline) for frontend module development graph = create_parallel_pipeline( sub_graphs=[ create_agent( model="vllm:qwen3-4b", tools=[develop_user_module], system_prompt="You are a frontend development engineer responsible for developing user-related modules.", name="user_module_agent", ), create_agent( model="vllm:qwen3-4b", tools=[develop_product_module], system_prompt="You are a frontend development engineer responsible for developing product-related modules.", name="product_module_agent", ), create_agent( model="vllm:qwen3-4b", tools=[develop_order_module], system_prompt="You are a frontend development engineer responsible for developing order-related modules.", name="order_module_agent", ), ], state_schema=AgentState, # Example of optional parameters (uncomment and provide actual values if needed) # branches_fn=lambda state: [Send(to=sub_graph_name) for sub_graph_name in ['user_module_agent', 'product_module_agent', 'order_module_agent']], # graph_name="ecommerce_dev_pipeline", # context_schema={}, # input_schema={}, # output_schema={}, # checkpoint=None, # Provide an instance of BaseCheckpointSaver # store=None, # Provide an instance of BaseStore # cache=None # Provide an instance of BaseCache ) # Dummy invoke call for demonstration # response = graph.invoke({"messages": [HumanMessage("Develop three core modules of the e-commerce website in parallel")]}) # print(response) ``` -------------------------------- ### Asynchronous Tool with Human-in-the-Loop Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/tool-calling/human-in-the-loop Shows how to use the `human_in_the_loop_async` decorator for asynchronous tool functions. This allows for human review of asynchronous tool calls. Dependencies include `langchain_dev_utils`, `langchain_core.tools`, and `asyncio`. ```python from langchain_dev_utils import human_in_the_loop_async from langchain_core.tools import tool import asyncio import datetime @human_in_the_loop_async @tool async def async_get_current_time() -> str: """Asynchronously get current timestamp""" await asyncio.sleep(1) return str(datetime.datetime.now().timestamp()) ``` -------------------------------- ### LLM Tool Selector Middleware for Agent Tool Selection Source: https://tbice123123.github.io/langchain-dev-utils-docs/en/api-reference/agent Provides middleware functionality for agent tool selection using a large language model. It requires a model identifier and allows configuration of an optional system prompt, maximum number of tools, and tools that should always be included. ```python class LLMToolSelectorMiddleware(_LLMToolSelectorMiddleware): def __init__( self, *, model: str, system_prompt: Optional[str] = None, max_tools: Optional[int] = None, always_include: Optional[list[str]] = None, ) -> None: # ... constructor implementation ... ```