### Install for Examples Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/00-START-HERE.md Install optional dependencies for running examples and set the OpenAI API key. ```bash pip install "langchain[openai]" export OPENAI_API_KEY="sk-..." ``` -------------------------------- ### Basic Agent Setup with Tool Registration and Execution Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md A minimal example demonstrating how to set up an agent with a single tool, LLM, embeddings, and an in-memory store. It then compiles and invokes the agent. ```python from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langchain_core.tools import tool from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent @tool def get_weather(location: str) -> str: """Get weather for a location.""" return f"Sunny in {location}" tool_registry = {"weather": get_weather} llm = init_chat_model("openai:gpt-4o-mini") embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore(index={"embed": embeddings, "dims": 1536, "fields": ["description"]}) store.put(("tools",), "weather", {"description": "weather: Get weather for a location."}) builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) result = agent.invoke({"messages": "What's the weather?"}) ``` -------------------------------- ### Environment Setup and Installation Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Set up your Python virtual environment, install necessary dependencies including LangGraph BigTool and Langchain with OpenAI support, and configure API keys. ```bash # Create virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install dependencies pip install langgraph-bigtool "langchain[openai]" # Set up API keys export OPENAI_API_KEY="sk-..." # Run your app python my_app.py ``` -------------------------------- ### Complete LangGraph BigTool Setup and Execution Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/configuration.md A comprehensive example showing tool definition, registry creation, LLM and store initialization, tool indexing, agent compilation, and execution. ```python import uuid from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent from langchain_core.tools import tool # Define tools @tool def calculate_sum(a: int, b: int) -> int: """Add two numbers.""" return a + b @tool def calculate_product(a: int, b: int) -> int: """Multiply two numbers.""" return a * b # Create registry tool_registry = { str(uuid.uuid4()): calculate_sum, str(uuid.uuid4()): calculate_product, } # Initialize LLM llm = init_chat_model("openai:gpt-4o-mini") # Configure embeddings and store embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={ "embed": embeddings, "dims": 1536, "fields": ["description"], } ) # Index tools for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}, ) # Create and compile agent builder = create_agent( llm, tool_registry, limit=3, namespace_prefix=("tools",), ) agent = builder.compile(store=store) # Execute result = agent.invoke({"messages": "What is 5 plus 3?"}) ``` -------------------------------- ### Quick Start: Create and Run LangGraph BigTool Agent Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/00-START-HERE.md This snippet demonstrates the basic setup for creating and running a LangGraph agent. It includes defining a tool, setting up the LLM, embeddings, and an in-memory store, indexing the tool, and finally compiling and invoking the agent. ```python from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langchain_core.tools import tool from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent # Define a tool @tool def get_weather(location: str) -> str: """Get weather for a location.""" return f"Sunny in {location}" # Create registry tool_registry = {"weather": get_weather} # Setup LLM and store llm = init_chat_model("openai:gpt-4o-mini") embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) # Index tool store.put(("tools",), "weather", {"description": "weather: Get weather for a location."}) # Create and run agent builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) result = agent.invoke({"messages": "What's the weather?"}) ``` -------------------------------- ### Minimal Agent Setup Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Demonstrates a minimal setup for creating an agent with a single tool, an LLM, an embeddings store, and invoking the agent. ```python from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langchain_core.tools import tool from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent @tool def my_tool(x: int) -> int: """Do something.""" return x * 2 tool_registry = {"my_tool": my_tool} llm = init_chat_model("openai:gpt-4o-mini") embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) store.put(("tools",), "my_tool", {"description": "my_tool: Do something."}) builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) result = agent.invoke({"messages": "Use my tool with 5"}) ``` -------------------------------- ### Agent Setup with Category-Based Custom Tool Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/usage-patterns.md This example shows how to implement custom tool retrieval based on categories instead of semantic search. Tools are organized into lists by category, and a custom function directs the agent to retrieve tools based on a specified category, enabling targeted tool access. ```python from typing import Literal from langgraph.prebuilt import InjectedStore from langgraph.store.base import BaseStore from typing_extensions import Annotated from langgraph_bigtool import create_agent # Tools organized by category billing_tools = [get_balance, transfer_money] support_tools = [get_ticket, create_ticket] admin_tools = [list_users, ban_user] tool_registry = { "balance": get_balance, "transfer": transfer_money, "ticket_get": get_ticket, "ticket_create": create_ticket, "users_list": list_users, "users_ban": ban_user, } # Custom retrieval by category def retrieve_tools_by_category( category: Literal["billing", "support", "admin"], ) -> list[str]: """Retrieve tools by category.""" categories = { "billing": ["balance", "transfer"], "support": ["ticket_get", "ticket_create"], "admin": ["users_list", "users_ban"], } return categories.get(category, []) # Create agent with custom retrieval builder = create_agent( llm, tool_registry, retrieve_tools_function=retrieve_tools_by_category, ) agent = builder.compile() # LLM will use the category parameter to fetch tools result = agent.invoke({"messages": "Show me support tools to help this customer"}) ``` -------------------------------- ### Initial State Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/graph-architecture.md Illustrates the initial state of the graph before any user interaction or processing. ```python { "messages": [HumanMessage(content="user query")], "selected_tool_ids": [], # Empty initially } ``` -------------------------------- ### Tool Binding Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/graph-architecture.md Demonstrates how to dynamically bind tools to an LLM based on the `selected_tool_ids` in the state, including always-available and dynamically added tools. ```python selected_tools = [tool_registry[id] for id in state["selected_tool_ids"]] llm_with_tools = llm.bind_tools([retrieve_tools, *selected_tools]) ``` -------------------------------- ### Basic Agent Setup with Tools and Semantic Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/usage-patterns.md This snippet demonstrates the simplest way to create an agent using a few predefined tools and default semantic retrieval. It requires initializing an LLM, embeddings, and an in-memory store for tool indexing. ```python import uuid from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langchain_core.tools import tool from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent # Define tools @tool def get_balance(account_id: str) -> str: """Get account balance.""" return f"Balance: $1000" @tool def transfer_money(from_id: str, to_id: str, amount: float) -> str: """Transfer money between accounts.""" return f"Transferred ${amount}" # Registry with unique IDs tool_registry = { str(uuid.uuid4()): get_balance, str(uuid.uuid4()): transfer_money, } # LLM and embeddings llm = init_chat_model("openai:gpt-4o-mini") embeddings = init_embeddings("openai:text-embedding-3-small") # Store with semantic search store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) # Index tools for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}) # Create agent builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Execute result = agent.invoke({"messages": "What is the account balance?"}) ``` -------------------------------- ### Install langgraph-bigtool and Langchain with OpenAI support Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/README.md Install the langgraph-bigtool library along with Langchain and OpenAI support, and set your OpenAI API key. ```bash pip install langgraph-bigtool "langchain[openai]" export OPENAI_API_KEY= ``` -------------------------------- ### Install langgraph-bigtool Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/README.md Install the langgraph-bigtool library using pip. ```bash pip install langgraph-bigtool ``` -------------------------------- ### Install LangChain for Other Providers Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Install specific langchain extras for Anthropic or Google models, and set the corresponding API keys. ```bash # Anthropic pip install "langchain[anthropic]" export ANTHROPIC_API_KEY="sk-ant-..." # Google pip install "google-generativeai" export GOOGLE_API_KEY="..." ``` -------------------------------- ### Install LangGraph BigTool Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Install the core library. Use the `langchain` extra for OpenAI models and embeddings. ```bash pip install langgraph-bigtool pip install "langchain[openai]" export OPENAI_API_KEY="sk-..." ``` -------------------------------- ### Type Hint Example: Tool Registry Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Example of type hinting for a tool registry dictionary. ```python # For tool registry tool_registry: dict[str, BaseTool | Callable] = {...} ``` -------------------------------- ### Example of _add_new Reducer Usage Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/State.md Demonstrates how the _add_new reducer works by adding tool IDs to an initial state, including handling duplicate entries. ```python # Initial state state = {"selected_tool_ids": ["tool_1"]} # First update: add tool_2 new_selected = ["tool_2"] result = _add_new(state["selected_tool_ids"], new_selected) # Result: ["tool_1", "tool_2"] # Second update: add tool_1 again (duplicate) new_selected = ["tool_1", "tool_3"] result = _add_new(result, new_selected) # Result: ["tool_1", "tool_2", "tool_3"] (tool_1 not duplicated) ``` -------------------------------- ### Optimized Agent Setup Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Configure an agent with performance optimizations, including a balanced tool limit, a narrow namespace prefix, and asynchronous retrieval for I/O-bound operations. ```python # Optimized setup builder = create_agent( llm, tool_registry, limit=3, # Balance between context and retrieval steps namespace_prefix=("services", "billing"), # Narrow namespace retrieve_tools_coroutine=aretrieve_tools, # Async for I/O ) ``` -------------------------------- ### Initialize Compatible Language Model Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Example of initializing a language model that conforms to the LanguageModelLike type. ```python from langchain_core.language_models import LanguageModelLike from langchain.chat_models import init_chat_model llm: LanguageModelLike = init_chat_model("openai:gpt-4o") ``` -------------------------------- ### Compile and Invoke StateGraph Synchronously Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Illustrates how to create an agent using StateGraph, compile it with a store, and then invoke it synchronously to get a result. ```python builder: StateGraph = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Synchronous execution result = agent.invoke({"messages": "What time is it?"}) ``` -------------------------------- ### Type Hint Example: Graph Builder Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Example of using type hints to create a graph builder. ```python # For the graph builder: StateGraph = create_agent(llm, tool_registry) ``` -------------------------------- ### Tool Decorator Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Example usage of the @tool decorator from langchain_core.tools. ```python @tool def my_tool(x: int) -> str: """Tool description.""" ... ``` -------------------------------- ### Type Hint Example: LLM Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Example of type hinting for an LLM instance. ```python # For LLM llm: LanguageModelLike = ... ``` -------------------------------- ### Initialize Agent State Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Example of initializing the State object with conversation history and selected tool IDs. ```python from langgraph_bigtool.graph import State state: State = { "messages": [HumanMessage(content="What tools do I have?")], "selected_tool_ids": ["tool_1", "tool_2"], } ``` -------------------------------- ### Index Tool Metadata in Store Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/configuration.md Example of how to index tool descriptions within a specified namespace in the LangGraph Store. ```python # Indexing example for tool_id, tool in tool_registry.items(): store.put( ("services", "billing", "tools"), # matches namespace_prefix tool_id, {"description": f"{tool.name}: {tool.description}"}, ) ``` -------------------------------- ### Type Hint Example: Custom Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Example of type hinting for a custom retrieval function with store injection. ```python # For custom retrieval with Store def retrieve_tools( query: str, *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: ... ``` -------------------------------- ### Semantic Retrieval Setup with InMemoryStore Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md Initializes an InMemoryStore with OpenAI embeddings for semantic search. Tool descriptions are indexed to enable the agent to find relevant tools based on queries. ```python from langgraph.store.memory import InMemoryStore from langchain.embeddings import init_embeddings embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) # Index tool descriptions for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}) # Agent will search these descriptions to find relevant tools ``` -------------------------------- ### Default Retrieval Tool Usage Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/get_default_retrieval_tool.md Demonstrates how to get default retrieval functions, set up an InMemoryStore with embeddings, index tools, and create a StructuredTool for use in an agent. ```python from langgraph_bigtool.tools import get_default_retrieval_tool from langgraph.store.memory import InMemoryStore from langchain.embeddings import init_embeddings # Get default retrieval functions retrieve_tools, aretrieve_tools = get_default_retrieval_tool( ("tools",), limit=3, ) # Create store with embeddings embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={ "embed": embeddings, "dims": 1536, "fields": ["description"], } ) # Index some tools store.put(("tools",), "tool_1", {"description": "calculate sum of two numbers"}) store.put(("tools",), "tool_2", {"description": "fetch weather forecast data"}) store.put(("tools",), "tool_3", {"description": "get current time in timezone"}) # Create a StructuredTool from the function from langchain_core.tools import StructuredTool retrieval_tool = StructuredTool.from_function( func=retrieve_tools, coroutine=aretrieve_tools, ) # Use in agent from langgraph_bigtool import create_agent builder = create_agent(llm, tool_registry) ``` -------------------------------- ### Register Tools with ToolId Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Example of a tool registry mapping ToolId strings to BaseTool instances. ```python from langgraph_bigtool.tools import ToolId tool_registry: dict[ToolId, BaseTool] = { "weather_tool": weather_tool_instance, "time_tool": time_tool_instance, } ``` -------------------------------- ### Agent Setup with Hundreds of Tools using Semantic Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/usage-patterns.md This pattern leverages semantic retrieval to efficiently manage and expose relevant tools when dealing with a large number of available functions. It converts math functions to tools and indexes them, allowing the agent to retrieve only necessary trigonometric functions for a query. ```python import math import types import uuid from langgraph_bigtool.utils import convert_positional_only_function_to_tool # Convert all math functions to tools all_tools = [] for name in dir(math): func = getattr(math, name) if isinstance(func, types.BuiltinFunctionType): if tool := convert_positional_only_function_to_tool(func): all_tools.append(tool) # Create registry with ~50 tools tool_registry = {str(uuid.uuid4()): tool for tool in all_tools} # Index in store embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}) # Create agent with higher limit (still only retrieves what's relevant) builder = create_agent(llm, tool_registry, limit=5) agent = builder.compile(store=store) # Query will only retrieve trigonometric functions even though 50 are available result = agent.invoke({"messages": "Calculate sine of 0.5"}) ``` -------------------------------- ### Beta Decorator Usage Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/module-overview.md Example of using the `@beta()` decorator from langchain_core._api to mark a function as a beta API. ```python # In utils.py from typing import Callable from langchain_core._api import beta @beta() def convert_positional_only_function_to_tool(func: Callable): # Function body pass ``` -------------------------------- ### Check for Store Injection in a Simple Tool Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/get_store_arg.md This example demonstrates how to use get_store_arg on a tool that does not utilize Store injection. It will return None, indicating no injection is required. ```python from langchain_core.tools import StructuredTool from langgraph_bigtool.tools import get_store_arg def simple_retrieval(query: str, category: str = "general") -> list[str]: """Tool retrieval without store injection.""" if category == "billing": return ["tool_1", "tool_2"] else: return ["tool_3", "tool_4"] tool = StructuredTool.from_function(simple_retrieval) store_param_name = get_store_arg(tool) print(store_param_name) # Output: None ``` -------------------------------- ### Custom Retrieval Function Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md Demonstrates how to replace default semantic search with a custom retrieval function that categorizes tools. The function uses Annotated and InjectedStore for dependency injection. ```python from typing import Literal from langgraph.prebuilt import InjectedStore from typing_extensions import Annotated def retrieve_by_category( category: Literal["billing", "support", "admin"], *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: """Retrieve tools by category.""" return CATEGORY_MAP.get(category, []) builder = create_agent( llm, tool_registry, retrieve_tools_function=retrieve_by_category, ) ``` -------------------------------- ### Test Tool Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Provides examples for testing both default and custom tool retrieval functions. It shows how to obtain the retrieval function and then use it to find tool IDs based on a query. ```python # For default retrieval retrieve_fn, _ = get_default_retrieval_tool(("tools",)) # For custom retrieval tool_ids = retrieve_fn(query="your query", store=store) print(f"Retrieved: {tool_ids}") ``` -------------------------------- ### Create Agent with Custom Asynchronous Retrieval Function Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/create_agent.md Shows how to create an agent with a custom asynchronous function for tool retrieval. The `aretrieve_tools` coroutine is provided via `retrieve_tools_coroutine`. Includes an example of asynchronous execution. ```python async def aretrieve_tools( query: str, *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: """Async tool retrieval.""" results = await store.asearch(("tools",), query=query, limit=5) return [result.key for result in results] builder = create_agent( llm, tool_registry, retrieve_tools_coroutine=aretrieve_tools, ) agent = builder.compile(store=store) # Async execution result = await agent.ainvoke({"messages": "What time is it?"}) ``` -------------------------------- ### Tool Registry Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md Defines a tool registry mapping string IDs to tool objects or callables. Supports LangChain BaseTool instances and raw Python callables. ```python tool_registry = { "weather_tool": get_weather, "time_tool": get_current_time, "calculate_sum": lambda a, b: a + b, } ``` -------------------------------- ### Standard File Organization for LangGraph BigTool Project Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Illustrates a typical file structure for a LangGraph BigTool project, separating configuration, tool definitions, agent setup, and main execution logic into distinct files. ```python # config.py - Configuration EMBEDDING_MODEL = "openai:text-embedding-3-small" CHAT_MODEL = "openai:gpt-4o-mini" RETRIEVAL_LIMIT = 3 # tools.py - Tool definitions @tool def tool_1(x: int) -> int: """...""" pass # agent.py - Agent setup def create_my_agent(): tool_registry = {"t1": tool_1, ...} embeddings = init_embeddings(EMBEDDING_MODEL) store = InMemoryStore(...) llm = init_chat_model(CHAT_MODEL) for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": ...}) builder = create_agent(llm, tool_registry, limit=RETRIEVAL_LIMIT) return builder.compile(store=store) # main.py - Usage async def main(): agent = create_my_agent() result = await agent.ainvoke({"messages": "user query"}) return result ``` -------------------------------- ### Initialize Basic Chat Models Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/configuration.md Demonstrates how to initialize chat models from different providers like OpenAI, Anthropic, and Google. ```python from langchain.chat_models import init_chat_model # OpenAI llm = init_chat_model("openai:gpt-4o-mini") # Anthropic llm = init_chat_model("anthropic:claude-3-5-sonnet") # Google llm = init_chat_model("google_genai:gemini-2.0-flash") ``` -------------------------------- ### Create and Compile an Agent Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md This snippet shows how to create an agent using `create_agent` and then compile it with a specified store. It's useful for setting up the core agent functionality. ```python builder = create_agent(llm, tool_registry, limit=5) agent = builder.compile(store=store) ``` -------------------------------- ### Customize Agent Creation with Parameters Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Demonstrates how to create an agent and progressively customize its behavior by adjusting parameters like retrieval limit, filtering, and custom retrieval functions. ```python # Phase 1: Get it working builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Phase 2: Tune retrieval limit builder = create_agent(llm, tool_registry, limit=5) # Phase 3: Add custom filtering builder = create_agent(llm, tool_registry, filter={"enabled": True}) # Phase 4: Custom retrieval logic builder = create_agent(llm, tool_registry, retrieve_tools_function=custom_fn) ``` -------------------------------- ### Create Tool Registry and Agent with Math Tools Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/convert_positional_only_function_to_tool.md Converts specific math functions into LangChain tools, creates a tool registry, indexes tool descriptions in an in-memory store, and then compiles a LangGraph agent using these tools and an LLM. ```python import math from langgraph_bigtool.utils import convert_positional_only_function_to_tool from langgraph_bigtool import create_agent from langchain.chat_models import init_chat_model from langgraph.store.memory import InMemoryStore # Convert math functions math_tools = [] for func in [math.acos, math.asin, math.cos, math.sin]: if tool := convert_positional_only_function_to_tool(func): math_tools.append(tool) tool_registry = {tool.name: tool for tool in math_tools} # Index in store from langchain.embeddings import init_embeddings embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={"embed": embeddings, "dims": 1536, "fields": ["description"]} ) for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}) # Create and compile agent llm = init_chat_model("openai:gpt-4o-mini") builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) ``` -------------------------------- ### Create Agent with Many Tools Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/00-START-HERE.md Demonstrates creating an agent with a large number of tools, showing that it efficiently retrieves only the necessary ones. Requires `llm`, `store`, and `create_agent` to be defined. ```python import math from langgraph_bigtool.utils import convert_positional_only_function_to_tool # Convert ~50 math functions to tools all_tools = [convert_positional_only_function_to_tool(func) for func in [...]] tool_registry = {tool.name: tool for tool in all_tools if tool} # Even with 50 tools, agent only retrieves what's needed builder = create_agent(llm, tool_registry, limit=3) agent = builder.compile(store=store) # This will only retrieve "acos" even though 50 tools are available result = agent.invoke({"messages": "Calculate arc cosine of 0.5"}) ``` -------------------------------- ### Create Agent with Tool Registry Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/create_agent.md This snippet shows the basic process of creating an agent using a tool registry and then compiling and invoking it. It assumes `llm`, `tool_registry`, and `store` are pre-defined. ```python # Index tool descriptions for tool_id, tool_obj in tool_registry.items(): store.put( ("tools",), tool_id, {"description": f"{tool_obj.name}: {tool_obj.description}"}, ) # Create agent builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Execute result = agent.invoke({"messages": "What is the weather in New York?"}) ``` -------------------------------- ### Initialize and Use InMemoryStore Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Demonstrates how to initialize an InMemoryStore with embeddings for semantic search and how to put and search for items. ```python from langgraph.store.memory import InMemoryStore from langchain.embeddings import init_embeddings embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={ "embed": embeddings, "dims": 1536, "fields": ["description"], } ) # Index tools store.put(("tools",), "tool_1", {"description": "Calculate sum"}) # Search tools results = store.search(("tools",), query="addition math", limit=3) ``` -------------------------------- ### Use RunnableConfig in Node Function Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Example of a node function accepting RunnableConfig to access execution context. ```python def call_model(state: State, config: RunnableConfig, *, store: BaseStore) -> State: # config contains execution context pass ``` -------------------------------- ### Agent Initialization with String Message Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/State.md Illustrates initializing an agent with a string input for messages, which is automatically converted to a HumanMessage. ```python # String gets converted to HumanMessage internally result = agent.invoke({"messages": "Help me with something"}) ``` -------------------------------- ### Import PostgresStore from langgraph.store.postgres Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Import the PostgreSQL-backed storage implementation. ```python from langgraph.store.postgres import PostgresStore ``` -------------------------------- ### Get Default Retrieval Tool Signature Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Signature for get_default_retrieval_tool, returning sync and async retrieval functions. ```python def get_default_retrieval_tool( namespace_prefix: tuple[str, ...], *, limit: int = 2, filter: dict[str, Any] | None = None, ) -> tuple[Callable, Callable] ``` -------------------------------- ### Final State Example Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/graph-architecture.md Represents the terminal state of the graph when no more tool calls are made and a final response is generated. ```python { "messages": [ ..., AIMessage(content="Final response to user", tool_calls=[]), ], "selected_tool_ids": ["tool_1", "tool_2"], } ``` -------------------------------- ### Index Tools into Store Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Iterate through the tool registry and index each tool's metadata (name and description) into the store for retrieval. ```python for tool_id, tool in tool_registry.items(): # Get tool description if hasattr(tool, 'description'): description = tool.description elif hasattr(tool, '__doc__'): description = tool.__doc__ or "No description" else: description = "Tool" # Also include name for better search full_description = f"{tool.name if hasattr(tool, 'name') else tool_id}: {description}" store.put( ("tools",), # namespace tool_id, {"description": full_description}, ) ``` -------------------------------- ### Create Agent with Default Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/create_agent.md Demonstrates basic usage of `create_agent` with default semantic retrieval. Requires initializing an LLM, embeddings, a tool registry, and an in-memory store. ```python from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langchain_core.tools import tool from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent # Define tools @tool def get_weather(location: str) -> str: """Get weather for a location.""" return f"Sunny in {location}" @tool def get_time(timezone: str) -> str: """Get current time in timezone.""" return f"3:45 PM in {timezone}" # Create tool registry tool_registry = { "weather_tool": get_weather, "time_tool": get_time, } # Initialize LLM and embeddings llm = init_chat_model("openai:gpt-4o-mini") embeddings = init_embeddings("openai:text-embedding-3-small") # Create and configure store store = InMemoryStore( index={ "embed": embeddings, "dims": 1536, "fields": ["description"], } ) ``` -------------------------------- ### Custom Tool Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Use this for custom retrieval logic where you need to specify the store and query to get tool IDs. ```python tool_ids = retrieve_fn(query="your query", store=store) print(f"Retrieved IDs: {tool_ids}") ``` -------------------------------- ### Import prebuilt components from langgraph.prebuilt Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Import ToolNode, InjectedStore, and InjectedState for prebuilt graph components. ```python from langgraph.prebuilt import ToolNode, InjectedStore, InjectedState ``` -------------------------------- ### Get Default Retrieval Tool Functions Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Retrieves default synchronous and asynchronous functions for tool retrieval, allowing configuration of namespace, limit, and filters. ```python from langgraph_bigtool.tools import get_default_retrieval_tool retrieve_fn, aretrieve_fn = get_default_retrieval_tool( ("tools",), limit=3, filter={"category": "math"}, ) ``` -------------------------------- ### Agent Invocation with Initial State Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/State.md Shows how to invoke an agent with an initial state, including messages, and how the agent returns an output state with accumulated messages and tool IDs. ```python from langgraph_bigtool import create_agent builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Input state (initial messages) input_state = {"messages": "What is the weather?"} # Agent returns output state with accumulated messages and tool IDs output_state = agent.invoke(input_state) # Access the state print(output_state["messages"]) print(output_state["selected_tool_ids"]) ``` -------------------------------- ### Graph Execution (Sync, Async, Streaming) Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md Shows how to compile and execute the agent graph synchronously, asynchronously, and with streaming updates. The agent is compiled with an optional store. ```python builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) # Synchronous result = agent.invoke({"messages": "your query"}) # Asynchronous result = await agent.ainvoke({"messages": "your query"}) # Streaming for step in agent.stream({"messages": "your query"}, stream_mode="updates"): print(step) ``` -------------------------------- ### Custom Retrieval Function Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/00-START-HERE.md Example of defining a custom retrieval function that categorizes tools instead of using semantic search. This function should be passed to `create_agent` via `retrieve_tools_function`. Requires `llm` and `tool_registry` to be defined. ```python from typing import Literal from langgraph.prebuilt import InjectedStore from typing_extensions import Annotated # Instead of semantic search, use categories def retrieve_by_category( category: Literal["math", "string", "io"], ) -> list[str]: return { "math": ["add", "multiply", "sin"], "string": ["uppercase", "split"], "io": ["read_file", "write_file"], }.get(category, []) builder = create_agent( llm, tool_registry, retrieve_tools_function=retrieve_by_category, ) ``` -------------------------------- ### Agent Initialization with Explicit Message Objects Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/State.md Shows how to initialize an agent by providing explicit message objects, such as HumanMessage, for the messages field. ```python # Or provide explicit message objects from langchain_core.messages import HumanMessage result = agent.invoke({ "messages": [HumanMessage(content="Help me with something")] }) ``` -------------------------------- ### Import BaseStore from langgraph.store.base Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Import the abstract persistence layer for storage. ```python from langgraph.store.base import BaseStore ``` -------------------------------- ### Configure PostgreSQL Store for Production Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md For production environments, use `PostgresStore` for persistent storage of tool metadata. Provide the connection string and index tools. ```python from langgraph.store.postgres import PostgresStore connection_string = "postgresql://user:password@localhost/langgraph" store = PostgresStore(connection_string=connection_string) # Index tools (one-time setup) for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}, ) ``` -------------------------------- ### Register Tools as BaseTool or Callable Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/types.md Demonstrates registering tools in a registry using LangChain's BaseTool or raw Python callables. ```python from langchain_core.tools import BaseTool, StructuredTool, tool from typing import Callable @tool def my_tool(query: str) -> str: """Describe the tool.""" return f"Result for {query}" def another_tool(x: int, y: int) -> int: """Add two numbers.""" return x + y tool_registry: dict[str, BaseTool | Callable] = { "decorated": my_tool, # BaseTool from @tool "structured": StructuredTool.from_function(another_tool), # StructuredTool "raw": another_tool, # Raw callable } ``` -------------------------------- ### Custom Tool Retrieval without LangGraph Store Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/README.md Implement custom tool retrieval logic that does not rely on the LangGraph Store. This example shows how to return tool IDs based on predefined categories using a `Literal` type hint for categorical arguments. ```python tool_registry = { "id_1": get_balance, "id_2": get_history, "id_3": create_ticket, } def retrieve_tools( category: Literal["billing", "service"], ) -> list[str]: """Get tools for a category.""" if category == "billing": return ["id_1", "id_2"] else: return ["id_3"] ``` -------------------------------- ### Create an Agent with Math Tools Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/README.md This Python script demonstrates how to equip a LangGraph agent with all functions from Python's built-in math library. It collects functions, converts them to tools, registers them, and indexes their descriptions in a LangGraph Store for retrieval. ```python import math import types import uuid from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent from langgraph_bigtool.utils import ( convert_positional_only_function_to_tool ) # Collect functions from `math` built-in all_tools = [] for function_name in dir(math): function = getattr(math, function_name) if not isinstance( function, types.BuiltinFunctionType ): continue # This is an idiosyncrasy of the `math` library if tool := convert_positional_only_function_to_tool( function ): all_tools.append(tool) # Create registry of tools. This is a dict mapping # identifiers to tool instances. tool_registry = { str(uuid.uuid4()): tool for tool in all_tools } # Index tool names and descriptions in the LangGraph # Store. Here we use a simple in-memory store. embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={ "embed": embeddings, "dims": 1536, "fields": ["description"], } ) for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, { "description": f"{tool.name}: {tool.description}", }, ) # Initialize agent llm = init_chat_model("openai:gpt-4o-mini") builder = create_agent(llm, tool_registry) agent = builder.compile(store=store) agent ``` -------------------------------- ### Custom Retrieval with Store Search and Filtering Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/usage-patterns.md This snippet illustrates using the Store for tool retrieval but with added custom filtering logic. Tools are indexed with metadata like category and authentication requirements, allowing for more granular control over which tools are made available to the agent. ```python from langgraph.prebuilt import InjectedStore from langgraph.store.base import BaseStore from typing_extensions import Annotated # Index tools with metadata for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, { "description": f"{tool.name}: {tool.description}", "category": tool_metadata[tool_id]["category"], "requires_auth": tool_metadata[tool_id]["requires_auth"], }, ) ``` -------------------------------- ### Import PostgresSaver Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Import the PostgresSaver class for PostgreSQL state persistence. ```python from langgraph.checkpoint.postgres import PostgresSaver ``` -------------------------------- ### Create Agent with Custom Synchronous Retrieval Function Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/api-reference/create_agent.md Demonstrates creating an agent that uses a custom synchronous function to retrieve tools based on a category. The `retrieve_by_category` function is injected using `retrieve_tools_function`. ```python from typing import Literal from langgraph.prebuilt import InjectedStore from langgraph.store.base import BaseStore from typing_extensions import Annotated def retrieve_by_category( category: Literal["weather", "time"], *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: """Retrieve tools by category.""" if category == "weather": return ["weather_tool"] else: return ["time_tool"] builder = create_agent( llm, tool_registry, retrieve_tools_function=retrieve_by_category, ) agent = builder.compile(store=store) ``` -------------------------------- ### Import Chat Model Initialization Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/API-SYMBOLS.md Import functions and classes for initializing chat models, including ChatOpenAI. ```python from langchain.chat_models import init_chat_model from langchain_openai import ChatOpenAI ``` -------------------------------- ### Sync and Async Node Support Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/graph-architecture.md Illustrates how to handle both synchronous and asynchronous node implementations, allowing for mixed execution within the graph. ```python if retrieve_tools_function is not None and retrieve_tools_coroutine is not None: select_tools_node = RunnableCallable(select_tools, aselect_tools) elif retrieve_tools_function is not None and retrieve_tools_coroutine is None: select_tools_node = select_tools elif retrieve_tools_coroutine is not None and retrieve_tools_function is None: select_tools_node = aselect_tools ``` -------------------------------- ### Converting Math Functions to Tools for Large Scale Use Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/README.md Illustrates converting multiple Python math functions into tools using `convert_positional_only_function_to_tool`. These tools are then registered and indexed in the store for agent use. ```python import math from langgraph_bigtool.utils import convert_positional_only_function_to_tool # Convert ~50 math functions to tools all_tools = [] for func in [math.acos, math.sin, math.cos, ...]: if tool := convert_positional_only_function_to_tool(func): all_tools.append(tool) tool_registry = {tool.name: tool for tool in all_tools} # Index all in store for tool_id, tool in tool_registry.items(): store.put(("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}) ``` -------------------------------- ### Agent with Custom Retrieval by Category Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Sets up an agent to use a custom function for retrieving tools based on a specified category. No store is needed for this retrieval method. ```python from typing import Literal from langgraph.prebuilt import InjectedStore from typing_extensions import Annotated def retrieve_by_category( category: Literal["billing", "support"], ) -> list[str]: return {"billing": ["t1", "t2"], "support": ["t3"]}.get(category, []) builder = create_agent(llm, tool_registry, retrieve_tools_function=retrieve_by_category) agent = builder.compile() # No store needed for this retrieval ``` -------------------------------- ### Configure Tool Namespace Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/configuration.md Sets the hierarchical path in the LangGraph Store for tool metadata storage and retrieval. ```python # Flat namespace builder = create_agent(llm, tool_registry) # Uses ("tools",) # Nested namespace builder = create_agent( llm, tool_registry, namespace_prefix=("services", "billing", "tools"), ) # Custom organization builder = create_agent( llm, tool_registry, namespace_prefix=("domain", "api_v2", "actions"), ) ``` -------------------------------- ### Organizing Tools by Namespace Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/usage-patterns.md Structure tools hierarchically using namespace prefixes for better organization, especially when dealing with multiple services or domains. Tools are indexed under these namespaces in the store. ```python # Service-based organization builder = create_agent( llm, tool_registry, namespace_prefix=("services", "billing", "tools"), ) # Index accordingly for tool_id, tool in billing_tools.items(): store.put( ("services", "billing", "tools"), tool_id, {"description": f"{tool.name}: {tool.description}"}, ) # Multiple service namespaces for tool_id, tool in support_tools.items(): store.put( ("services", "support", "tools"), tool_id, {"description": f"{tool.name}: {tool.description}"}, ) ``` -------------------------------- ### Check Tool Availability and Details Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/integration-guide.md Iterate through the tool registry to verify that all tools are accessible and to inspect their names and descriptions. ```python # Verify tools are accessible for tool_id, tool in tool_registry.items(): print(f"Tool {tool_id}:") print(f" Name: {getattr(tool, 'name', 'N/A')}") print(f" Description: {getattr(tool, 'description', 'N/A')}") ``` -------------------------------- ### Index Tool Descriptions in Store Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/configuration.md Populates the store with tool descriptions for semantic search. Use this for basic indexing or with rich metadata. ```python # Index with description only for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}, ) # Index with rich metadata for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, { "description": f"{tool.name}: {tool.description}", "category": "math", "tags": ["calculation", "scientific"], }, ) ``` -------------------------------- ### Agent with Async Custom Retrieval Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Sets up an agent to use an asynchronous custom retrieval function that interacts with an injected store. The agent can then be invoked asynchronously. ```python async def aretrieve_tools( query: str, *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: results = await store.asearch(("tools",), query=query, limit=3) return [r.key for r in results] builder = create_agent(llm, tool_registry, retrieve_tools_coroutine=aretrieve_tools) agent = builder.compile(store=store) result = await agent.ainvoke({"messages": "query"}) ``` -------------------------------- ### Type Definitions for Agent Creation Source: https://github.com/langchain-ai/langgraph-bigtool/blob/main/_autodocs/quick-reference.md Illustrates the expected type signatures for various components used in creating an agent, including tool registries, retrieval functions, LLMs, stores, and the graph builder. ```python # Tool registry value types from langchain_core.tools import BaseTool, StructuredTool from typing import Callable tool_registry: dict[str, BaseTool | Callable] = { "tool1": my_tool, # BaseTool from @tool "tool2": my_function, # Raw callable } # Retrieval function signature from langgraph.prebuilt import InjectedStore from langgraph.store.base import BaseStore from typing_extensions import Annotated def retrieve_tools( query: str, *, store: Annotated[BaseStore, InjectedStore], ) -> list[str]: return [...] # Placeholder for actual return # LLM type from langchain_core.language_models import LanguageModelLike llm: LanguageModelLike = ... # Store type from langgraph.store.base import BaseStore store: BaseStore = ... # Graph return type from langgraph.graph import StateGraph builder: StateGraph = create_agent(...) ```