mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-17 02:03:44 +00:00
cli: update integration doc template for tools (#30188)
Chain example -> langgraph agent
This commit is contained in:
parent
0d7cdf290b
commit
3823daa0b9
@ -181,11 +181,11 @@
|
|||||||
"id": "659f9fbd-6fcf-445f-aa8c-72d8e60154bd",
|
"id": "659f9fbd-6fcf-445f-aa8c-72d8e60154bd",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Chaining\n",
|
"## Use within an agent\n",
|
||||||
"\n",
|
"\n",
|
||||||
"- TODO: Add user question and run cells\n",
|
"- TODO: Add user question and run cells\n",
|
||||||
"\n",
|
"\n",
|
||||||
"We can use our tool in a chain by first binding it to a [tool-calling model](/docs/how_to/tool_calling/) and then calling it:\n",
|
"We can use our tool in an [agent](/docs/concepts/agents/). For this we will need a LLM with [tool-calling](/docs/how_to/tool_calling/) capabilities:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
|
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -208,6 +208,19 @@
|
|||||||
"llm = init_chat_model(model=\"gpt-4o\", model_provider=\"openai\")"
|
"llm = init_chat_model(model=\"gpt-4o\", model_provider=\"openai\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "bea35fa1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langgraph.prebuilt import create_react_agent\n",
|
||||||
|
"\n",
|
||||||
|
"tools = [tool]\n",
|
||||||
|
"agent = create_react_agent(llm, tools)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@ -215,32 +228,14 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
"example_query = \"...\"\n",
|
||||||
"from langchain_core.runnables import RunnableConfig, chain\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"prompt = ChatPromptTemplate(\n",
|
"events = agent.stream(\n",
|
||||||
" [\n",
|
" {\"messages\": [(\"user\", example_query)]},\n",
|
||||||
" (\"system\", \"You are a helpful assistant.\"),\n",
|
" stream_mode=\"values\",\n",
|
||||||
" (\"human\", \"{user_input}\"),\n",
|
|
||||||
" (\"placeholder\", \"{messages}\"),\n",
|
|
||||||
" ]\n",
|
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"for event in events:\n",
|
||||||
"# specifying tool_choice will force the model to call this tool.\n",
|
" event[\"messages\"][-1].pretty_print()"
|
||||||
"llm_with_tools = llm.bind_tools([tool], tool_choice=tool.name)\n",
|
|
||||||
"\n",
|
|
||||||
"llm_chain = prompt | llm_with_tools\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"@chain\n",
|
|
||||||
"def tool_chain(user_input: str, config: RunnableConfig):\n",
|
|
||||||
" input_ = {\"user_input\": user_input}\n",
|
|
||||||
" ai_msg = llm_chain.invoke(input_, config=config)\n",
|
|
||||||
" tool_msgs = tool.batch(ai_msg.tool_calls, config=config)\n",
|
|
||||||
" return llm_chain.invoke({**input_, \"messages\": [ai_msg, *tool_msgs]}, config=config)\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"tool_chain.invoke(\"...\")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user