This example demonstrates how to equip an agent with all functions from Python's math library by indexing them in an InMemoryStore.
To run this, you will need langgraph-bigtool, langchain[openai], and an OPENAI_API_KEY environment variable set.
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
)
# 1. 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
# Handle positional-only functions common in math library
if tool := convert_positional_only_function_to_tool(
function
):
all_tools.append(tool)
# 2. Create registry of tools (dict mapping identifiers to tool instances)
tool_registry = {
str(uuid.uuid4()): tool
for tool in all_tools
}
# 3. Index tool names and descriptions in the LangGraph 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}",
},
)
# 4. Initialize agent
llm = init_chat_model("openai:gpt-4o-mini")
builder = create_agent(llm, tool_registry)
agent = builder.compile(store=store)
# 5. Test it out
query = "Use available tools to calculate arc cosine of 0.5."
for step in agent.stream(
{"messages": query},
stream_mode="updates",
):
for _, update in step.items():
for message in update.get("messages", []):
message.pretty_print()
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
)
# 1. 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
# Handle positional-only functions common in math library
if tool := convert_positional_only_function_to_tool(
function
):
all_tools.append(tool)
# 2. Create registry of tools (dict mapping identifiers to tool instances)
tool_registry = {
str(uuid.uuid4()): tool
for tool in all_tools
}
# 3. Index tool names and descriptions in the LangGraph 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}",
},
)
# 4. Initialize agent
llm = init_chat_model("openai:gpt-4o-mini")
builder = create_agent(llm, tool_registry)
agent = builder.compile(store=store)
# 5. Test it out
query = "Use available tools to calculate arc cosine of 0.5."
for step in agent.stream(
{"messages": query},
stream_mode="updates",
):
for _, update in step.items():
for message in update.get("messages", []):
message.pretty_print()