diff --git a/examples/how-tos/force-calling-a-tool-first.ipynb b/examples/how-tos/force-calling-a-tool-first.ipynb index 269d06c1..c7a97065 100644 --- a/examples/how-tos/force-calling-a-tool-first.ipynb +++ b/examples/how-tos/force-calling-a-tool-first.ipynb @@ -81,17 +81,7 @@ "metadata": { "lines_to_next_cell": 2 }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[WARN]: You have enabled LangSmith tracing without backgrounding callbacks.\n", - "[WARN]: If you are not using a serverless environment where you must wait for tracing calls to finish,\n", - "[WARN]: we suggest setting \"process.env.LANGCHAIN_CALLBACKS_BACKGROUND=true\" to avoid additional latency.\n" - ] - } - ], + "outputs": [], "source": [ "import { DynamicStructuredTool } from \"@langchain/core/tools\";\n", "import { z } from \"zod\";\n", @@ -216,25 +206,21 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "3db3dd6e", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ - "import { StateGraphArgs } from \"@langchain/langgraph\";\n", - "\n", - "interface IState {\n", - " messages: BaseMessage[];\n", - "}\n", + "import { Annotation } from \"@langchain/langgraph\";\n", + "import { BaseMessage } from \"@langchain/core/messages\";\n", "\n", - "const agentState: StateGraphArgs[\"channels\"] = {\n", - " messages: {\n", - " value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n", - " default: () => [],\n", - " },\n", - "};" + "const AgentState = Annotation.Root({\n", + " messages: Annotation({\n", + " reducer: (x, y) => x.concat(y),\n", + " }),\n", + "});" ] }, { @@ -271,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "eee5adc0", "metadata": {}, "outputs": [], @@ -281,7 +267,7 @@ "import { concat } from \"@langchain/core/utils/stream\";\n", "\n", "// Define logic that will be used to determine which conditional edge to go down\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", @@ -294,7 +280,7 @@ "\n", "// Define the function that calls the model\n", "const callModel = async (\n", - " state: IState,\n", + " state: typeof AgentState.State,\n", " config?: RunnableConfig,\n", ") => {\n", " const { messages } = state;\n", @@ -326,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "e9104fc7", "metadata": { "lines_to_next_cell": 2 @@ -334,7 +320,7 @@ "outputs": [], "source": [ "// This is the new first - the first call of the model we want to explicitly hard-code some action\n", - "const firstModel = async (state: IState) => {\n", + "const firstModel = async (state: typeof AgentState.State) => {\n", " const humanInput = state.messages[state.messages.length - 1].content || \"\";\n", " return {\n", " messages: [\n", @@ -371,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "de6da918", "metadata": { "lines_to_next_cell": 2 @@ -381,7 +367,7 @@ "import { END, START, StateGraph } from \"@langchain/langgraph\";\n", "\n", "// Define a new graph\n", - "const workflow = new StateGraph({ channels: agentState })\n", + "const workflow = new StateGraph(AgentState)\n", " // Define the new entrypoint\n", " .addNode(\"first_agent\", firstModel)\n", " // Define the two nodes we will cycle between\n", @@ -436,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "id": "acaade41", "metadata": { "lines_to_next_cell": 2 @@ -446,13 +432,69 @@ "name": "stdout", "output_type": "stream", "text": [ - "{ first_agent: { messages: [ \u001b[36m[AIMessage]\u001b[39m ] } }\n", + "{\n", + " first_agent: {\n", + " messages: [\n", + " AIMessage {\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {},\n", + " \"tool_calls\": [\n", + " {\n", + " \"name\": \"search\",\n", + " \"args\": {\n", + " \"query\": \"what is the weather in sf\"\n", + " },\n", + " \"id\": \"tool_abcd123\"\n", + " }\n", + " ],\n", + " \"invalid_tool_calls\": []\n", + " }\n", + " ]\n", + " }\n", + "}\n", "-----\n", "\n", - "{ action: { messages: [ \u001b[36m[ToolMessage]\u001b[39m ] } }\n", + "{\n", + " action: {\n", + " messages: [\n", + " ToolMessage {\n", + " \"content\": \"Cold, with a low of 13 ℃\",\n", + " \"name\": \"search\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {},\n", + " \"tool_call_id\": \"tool_abcd123\"\n", + " }\n", + " ]\n", + " }\n", + "}\n", "-----\n", "\n", - "{ agent: { messages: [ \u001b[36m[AIMessageChunk]\u001b[39m ] } }\n", + "{\n", + " agent: {\n", + " messages: [\n", + " AIMessageChunk {\n", + " \"id\": \"chatcmpl-9y562g16z0MUNBJcS6nKMsDuFMRsS\",\n", + " \"content\": \"The current weather in San Francisco is cold, with a low of 13°C.\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {\n", + " \"prompt\": 0,\n", + " \"completion\": 0,\n", + " \"finish_reason\": \"stop\",\n", + " \"system_fingerprint\": \"fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27fp_3aa7262c27\"\n", + " },\n", + " \"tool_calls\": [],\n", + " \"tool_call_chunks\": [],\n", + " \"invalid_tool_calls\": [],\n", + " \"usage_metadata\": {\n", + " \"input_tokens\": 104,\n", + " \"output_tokens\": 18,\n", + " \"total_tokens\": 122\n", + " }\n", + " }\n", + " ]\n", + " }\n", + "}\n", "-----\n", "\n" ] @@ -470,14 +512,6 @@ " console.log(\"-----\\n\");\n", "}" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "241e2fc9-9bee-4f2c-a077-09472a7b5613", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -485,9 +519,9 @@ "encoding": "# -*- coding: utf-8 -*-" }, "kernelspec": { - "display_name": "Deno", + "display_name": "TypeScript", "language": "typescript", - "name": "deno" + "name": "tslab" }, "language_info": { "codemirror_mode": {