Skip to content

Commit

Permalink
Updated managing agent steps doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent ce0596f commit 9968c93
Showing 1 changed file with 21 additions and 47 deletions.
68 changes: 21 additions & 47 deletions examples/how-tos/managing-agent-steps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,14 @@
"metadata": {},
"outputs": [],
"source": [
"import { Annotation } from \"@langchain/langgraph\";\n",
"import { BaseMessage } from \"@langchain/core/messages\";\n",
"import { StateGraphArgs } from \"@langchain/langgraph\";\n",
"\n",
"interface IState {\n",
" messages: BaseMessage[];\n",
"}\n",
"\n",
"const graphState: StateGraphArgs<IState>[\"channels\"] = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
"};"
"const AgentState = Annotation.Root({\n",
" messages: Annotation<BaseMessage[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" }),\n",
"});"
]
},
{
Expand All @@ -126,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"id": "ec9f73a5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -164,14 +159,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "7f4829c3",
"metadata": {},
"outputs": [],
"source": [
"import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n",
"\n",
"const toolNode = new ToolNode<{ messages: BaseMessage[] }>(tools);"
"const toolNode = new ToolNode<typeof AgentState.State>(tools);"
]
},
{
Expand All @@ -195,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"id": "cf1fcc3f",
"metadata": {},
"outputs": [],
Expand All @@ -210,7 +205,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"id": "a0903bb8",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -254,7 +249,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 8,
"id": "1249b1b3",
"metadata": {},
"outputs": [],
Expand All @@ -264,7 +259,7 @@
"import { RunnableConfig } from \"@langchain/core/runnables\";\n",
"\n",
"// Define the function that determines whether to continue or not\n",
"const shouldContinue = (state: IState) => {\n",
"const shouldContinue = (state: typeof AgentState.State) => {\n",
" const { messages } = state;\n",
" const lastMessage = messages[messages.length - 1] as AIMessage;\n",
" // If there is no function call, then we finish\n",
Expand All @@ -279,7 +274,7 @@
"//\n",
"// Here we don't pass all messages to the model but rather only pass the `N` most recent. Note that this is a terribly simplistic way to handle messages meant as an illustration, and there may be other methods you may want to look into depending on your use case. We also have to make sure we don't truncate the chat history to include the tool message first, as this would cause an API error.\n",
"const callModel = async (\n",
" state: IState,\n",
" state: typeof AgentState.State,\n",
" config?: RunnableConfig,\n",
") => {\n",
" let modelMessages = [];\n",
Expand Down Expand Up @@ -311,17 +306,15 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 9,
"id": "ff5f7b65",
"metadata": {},
"outputs": [],
"source": [
"import { START, StateGraph } from \"@langchain/langgraph\";\n",
"\n",
"// Define a new graph\n",
"const workflow = new StateGraph<IState>({\n",
" channels: graphState,\n",
"})\n",
"const workflow = new StateGraph(AgentState)\n",
" .addNode(\"agent\", callModel)\n",
" .addNode(\"tools\", toolNode)\n",
" .addEdge(START, \"agent\")\n",
Expand Down Expand Up @@ -351,7 +344,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 10,
"id": "7bd7315e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -398,19 +391,8 @@
"Tools: \n",
"- search({\"query\":\"current weather in San Francisco\"})\n",
"-----\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GraphRecursionError: Recursion limit of 10 reached without hitting a stop condition. You can increase the limit by setting the \"recursionLimit\" config key.\n",
" at CompiledStateGraph._transform (/Users/wfh/code/lc/langgraphjs/langgraph/dist/pregel/index.cjs:432:27)\n",
"\u001b[90m at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\u001b[39m\n",
" at async CompiledStateGraph._transformStreamWithConfig (/Users/wfh/code/lc/langgraphjs/node_modules/\u001b[4m@langchain\u001b[24m/core/dist/runnables/base.cjs:290:30)\n",
" at async CompiledStateGraph.transform (/Users/wfh/code/lc/langgraphjs/langgraph/dist/pregel/index.cjs:527:26)\n",
" at async Object.pull (/Users/wfh/code/lc/langgraphjs/node_modules/\u001b[4m@langchain\u001b[24m/core/dist/utils/stream.cjs:96:41)\n"
"\n",
"As expected, maximum steps reached. Exiting.\n"
]
}
],
Expand Down Expand Up @@ -462,21 +444,13 @@
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "763c79c8-71b8-441f-a52a-d03708170d12",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"display_name": "TypeScript",
"language": "typescript",
"name": "deno"
"name": "tslab"
},
"language_info": {
"codemirror_mode": {
Expand Down

0 comments on commit 9968c93

Please sign in to comment.