Skip to content

Commit

Permalink
start porting chat agent executor w func calling doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent 725d008 commit 231339e
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions examples/chat_agent_executor_with_function_calling/base.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,39 @@
"object you construct the graph with.\n",
"\n",
"For this example, the state we will track will just be a list of messages. We\n",
"want each node to just add messages to that list. Therefore, we will use an\n",
"object with one key (`messages`) with the value as an object:\n",
"`{ value: Function, default?: () => any }`\n",
"want each node to just add messages to that list. To do this, we define our state via the `Annotation` function, with a single attribute `messages` that is a list of `BaseMessage`s. The value of our attribute `messages` is another `Annotation` which has two sub-attributes: `reducer` and `default`. The `reducer` key must be a factory that returns a function that takes the current value of the attribute and the new value to add, and returns the new value of the attribute.\n",
"\n",
"The `default` key must be a factory that returns the default value for that\n",
"attribute."
"attribute (this key is optional)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ea793afa-2eab-4901-910d-6eed90cd6564",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "Cannot read properties of undefined (reading 'Root')",
"output_type": "error",
"traceback": [
"Stack trace:",
"TypeError: Cannot read properties of undefined (reading 'Root')",
" at <anonymous>:3:31"
]
}
],
"source": [
"import { BaseMessage } from \"@langchain/core/messages\";\n",
"import { Annotation } from \"@langchain/langgraph\";\n",
"\n",
"const agentState = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
"const AgentState = Annotation.Root({\n",
" messages: Annotation<BaseMessage[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
"};"
" })\n",
"})"
]
},
{
Expand Down Expand Up @@ -262,17 +272,16 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "3b541bb9-900c-40d0-964d-7b5dfee30667",
"metadata": {},
"outputs": [],
"source": [
"import { FunctionMessage } from \"@langchain/core/messages\";\n",
"import { AgentAction } from \"@langchain/core/agents\";\n",
"import type { RunnableConfig } from \"@langchain/core/runnables\";\n",
"\n",
"// Define the function that determines whether to continue or not\n",
"const shouldContinue = (state: { messages: Array<BaseMessage> }) => {\n",
"const shouldContinue = (state: typeof AgentState.State) => {\n",
" const { messages } = state;\n",
" const lastMessage = messages[messages.length - 1];\n",
" // If there is no function call, then we finish\n",
Expand All @@ -287,7 +296,7 @@
"};\n",
"\n",
"// Define the function to execute tools\n",
"const _getAction = (state: { messages: Array<BaseMessage> }): AgentAction => {\n",
"const _getAction = (state: typeof AgentState.State): AgentAction => {\n",
" const { messages } = state;\n",
" // Based on the continue condition\n",
" // we know the last message involves a function call\n",
Expand All @@ -309,9 +318,7 @@
"};\n",
"\n",
"// Define the function that calls the model\n",
"const callModel = async (\n",
" state: { messages: Array<BaseMessage> },\n",
") => {\n",
"const callModel = async (state: typeof AgentState.State) => {\n",
" const { messages } = state;\n",
" const response = await newModel.invoke(messages);\n",
" // We return a list, because this will get added to the existing list\n",
Expand All @@ -320,9 +327,7 @@
" };\n",
"};\n",
"\n",
"const callTool = async (\n",
" state: { messages: Array<BaseMessage> },\n",
") => {\n",
"const callTool = async (state: typeof AgentState.State) => {\n",
" const action = _getAction(state);\n",
" // We call the tool_executor and get back a response\n",
" const response = await toolExecutor.invoke(action);\n",
Expand All @@ -348,17 +353,15 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "813ae66c-3b58-4283-a02a-36da72a2ab90",
"metadata": {},
"outputs": [],
"source": [
"import { END, START, StateGraph } from \"@langchain/langgraph\";\n",
"\n",
"// Define a new graph\n",
"const workflow = new StateGraph({\n",
" channels: agentState,\n",
"});\n",
"const workflow = new StateGraph(AgentState);\n",
"\n",
"// Define the two nodes we will cycle between\n",
"workflow.addNode(\"agent\", callModel);\n",
Expand Down Expand Up @@ -413,7 +416,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "8edb04b9-40b6-46f1-a7a8-4b2d8aba7752",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -505,7 +508,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb",
"metadata": {},
"outputs": [
Expand Down

0 comments on commit 231339e

Please sign in to comment.