codekingpro/portable-devtools
114k
1Metadata-Version: 2.42Name: langgraph-prebuilt3Version: 1.1.04Summary: Library with high-level APIs for creating and executing LangGraph agents and tools.5Project-URL: Source, https://github.com/langchain-ai/langgraph/tree/main/libs/prebuilt6Project-URL: Twitter, https://x.com/langchain_oss7Project-URL: Slack, https://www.langchain.com/join-community8Project-URL: Reddit, https://www.reddit.com/r/LangChain/9License-Expression: MIT10License-File: LICENSE11Classifier: Development Status :: 5 - Production/Stable12Classifier: Programming Language :: Python13Classifier: Programming Language :: Python :: 314Classifier: Programming Language :: Python :: 3 :: Only15Classifier: Programming Language :: Python :: 3.1016Classifier: Programming Language :: Python :: 3.1117Classifier: Programming Language :: Python :: 3.1218Classifier: Programming Language :: Python :: 3.1319Classifier: Programming Language :: Python :: Implementation :: CPython20Classifier: Programming Language :: Python :: Implementation :: PyPy21Requires-Python: >=3.1022Requires-Dist: langchain-core>=1.3.123Requires-Dist: langgraph-checkpoint<5.0.0,>=2.1.024Description-Content-Type: text/markdown25 26# LangGraph Prebuilt27 28This library defines high-level APIs for creating and executing LangGraph agents and tools.29 30> [!IMPORTANT]31> This library is meant to be bundled with `langgraph`, don't install it directly32 33## Agents34 35`langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.chat_agent_executor.create_react_agent) of a tool-calling [ReAct-style](https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#react-implementation) agent - `create_react_agent`:36 37```bash38pip install langchain-anthropic39```40 41```python42from langchain_anthropic import ChatAnthropic43from langgraph.prebuilt import create_react_agent44 45# Define the tools for the agent to use46def search(query: str):47 """Call to surf the web."""48 # This is a placeholder, but don't tell the LLM that...49 if "sf" in query.lower() or "san francisco" in query.lower():50 return "It's 60 degrees and foggy."51 return "It's 90 degrees and sunny."52 53tools = [search]54model = ChatAnthropic(model="claude-3-7-sonnet-latest")55 56app = create_react_agent(model, tools)57# run the agent58app.invoke(59 {"messages": [{"role": "user", "content": "what is the weather in sf"}]},60)61```62 63## Tools64 65### ToolNode66 67`langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.tool_node.ToolNode) of a node that executes tool calls - `ToolNode`:68 69```python70from langgraph.prebuilt import ToolNode71from langchain_core.messages import AIMessage72 73def search(query: str):74 """Call to surf the web."""75 # This is a placeholder, but don't tell the LLM that...76 if "sf" in query.lower() or "san francisco" in query.lower():77 return "It's 60 degrees and foggy."78 return "It's 90 degrees and sunny."79 80tool_node = ToolNode([search])81tool_calls = [{"name": "search", "args": {"query": "what is the weather in sf"}, "id": "1"}]82ai_message = AIMessage(content="", tool_calls=tool_calls)83# execute tool call84tool_node.invoke({"messages": [ai_message]})85```86 87### ValidationNode88 89`langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.tool_validator.ValidationNode) of a node that validates tool calls against a pydantic schema - `ValidationNode`:90 91```python92from pydantic import BaseModel, field_validator93from langgraph.prebuilt import ValidationNode94from langchain_core.messages import AIMessage95 96 97class SelectNumber(BaseModel):98 a: int99 100 @field_validator("a")101 def a_must_be_meaningful(cls, v):102 if v != 37:103 raise ValueError("Only 37 is allowed")104 return v105 106validation_node = ValidationNode([SelectNumber])107validation_node.invoke({108 "messages": [AIMessage("", tool_calls=[{"name": "SelectNumber", "args": {"a": 42}, "id": "1"}])]109})110```111 112## Agent Inbox113 114The library contains schemas for using the [Agent Inbox](https://github.com/langchain-ai/agent-inbox) with LangGraph agents. Learn more about how to use Agent Inbox [here](https://github.com/langchain-ai/agent-inbox#interrupts).115 116```python117from langgraph.types import interrupt118from langgraph.prebuilt.interrupt import HumanInterrupt, HumanResponse119 120def my_graph_function():121 # Extract the last tool call from the `messages` field in the state122 tool_call = state["messages"][-1].tool_calls[0]123 # Create an interrupt124 request: HumanInterrupt = {125 "action_request": {126 "action": tool_call['name'],127 "args": tool_call['args']128 },129 "config": {130 "allow_ignore": True,131 "allow_respond": True,132 "allow_edit": False,133 "allow_accept": False134 },135 "description": _generate_email_markdown(state) # Generate a detailed markdown description.136 }137 # Send the interrupt request inside a list, and extract the first response138 response = interrupt([request])[0]139 if response['type'] == "response":140 # Do something with the response141 ...142```