From 2398f9cecbe2af1f454032b51b82cfa952f3e6d9 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 19 Aug 2024 16:29:08 -0700 Subject: [PATCH] updated stream values doc --- examples/how-tos/stream-values.ipynb | 269 +++++++++++++++------------ 1 file changed, 150 insertions(+), 119 deletions(-) diff --git a/examples/how-tos/stream-values.ipynb b/examples/how-tos/stream-values.ipynb index 9eab6852c..c92642f6b 100644 --- a/examples/how-tos/stream-values.ipynb +++ b/examples/how-tos/stream-values.ipynb @@ -19,14 +19,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "8e76833b", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ - "process.env.OPENAI_API_KEY = \"sk-...\";" + "// process.env.OPENAI_API_KEY = \"sk-...\";" ] }, { @@ -48,20 +48,14 @@ }, "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", - "// This defines the agent state\n", - "const graphState: StateGraphArgs[\"channels\"] = {\n", - " messages: {\n", - " value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n", - " default: () => [],\n", - " },\n", - "};" + "const GraphState = Annotation.Root({\n", + " messages: Annotation({\n", + " reducer: (x, y) => x.concat(y),\n", + " }),\n", + "});" ] }, { @@ -130,7 +124,7 @@ "source": [ "import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n", "\n", - "const toolNode = new ToolNode<{ messages: BaseMessage[] }>(tools);" + "const toolNode = new ToolNode(tools);" ] }, { @@ -214,7 +208,7 @@ "import { AIMessage } from \"@langchain/core/messages\";\n", "import { RunnableConfig } from \"@langchain/core/runnables\";\n", "\n", - "const routeMessage = (state: IState) => {\n", + "const routeMessage = (state: typeof GraphState.State) => {\n", " const { messages } = state;\n", " const lastMessage = messages[messages.length - 1] as AIMessage;\n", " // If no tools are called, we can finish (respond to the user)\n", @@ -226,7 +220,7 @@ "};\n", "\n", "const callModel = async (\n", - " state: IState,\n", + " state: typeof GraphState.State,\n", " config?: RunnableConfig,\n", ") => {\n", " // For versions of @langchain/core < 0.2.3, you must call `.stream()`\n", @@ -236,9 +230,7 @@ " return { messages: [responseMessage] };\n", "};\n", "\n", - "const workflow = new StateGraph({\n", - " channels: graphState,\n", - "})\n", + "const workflow = new StateGraph(GraphState)\n", " .addNode(\"agent\", callModel)\n", " .addNode(\"tools\", toolNode)\n", " .addEdge(START, \"agent\")\n", @@ -261,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "cbcf7c39", "metadata": {}, "outputs": [ @@ -269,133 +261,172 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ [ \u001b[32m'user'\u001b[39m, \u001b[32m\"what's the weather in sf\"\u001b[39m ] ]\n", + "[ [ 'user', \"what's the weather in sf\" ] ]\n", "\n", "====\n", "\n", "[\n", - " [ \u001b[32m'user'\u001b[39m, \u001b[32m\"what's the weather in sf\"\u001b[39m ],\n", + " [ 'user', \"what's the weather in sf\" ],\n", " AIMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " content: \u001b[32m''\u001b[39m,\n", - " tool_calls: \u001b[36m[Array]\u001b[39m,\n", - " invalid_tool_calls: [],\n", - " additional_kwargs: \u001b[36m[Object]\u001b[39m,\n", - " response_metadata: {}\n", + " \"id\": \"chatcmpl-9y660d49eLzT7DZeBk2ZmX8C5f0LU\",\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {\n", + " \"tool_calls\": [\n", + " {\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\",\n", + " \"type\": \"function\",\n", + " \"function\": \"[Object]\"\n", + " }\n", + " ]\n", + " },\n", + " \"response_metadata\": {\n", + " \"tokenUsage\": {\n", + " \"completionTokens\": 17,\n", + " \"promptTokens\": 70,\n", + " \"totalTokens\": 87\n", + " },\n", + " \"finish_reason\": \"tool_calls\",\n", + " \"system_fingerprint\": \"fp_3aa7262c27\"\n", " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m''\u001b[39m,\n", - " name: \u001b[90mundefined\u001b[39m,\n", - " additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[36m[Array]\u001b[39m },\n", - " response_metadata: { tokenUsage: \u001b[36m[Object]\u001b[39m, finish_reason: \u001b[32m'tool_calls'\u001b[39m },\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_calls: [ \u001b[36m[Object]\u001b[39m ],\n", - " invalid_tool_calls: [],\n", - " usage_metadata: { input_tokens: \u001b[33m70\u001b[39m, output_tokens: \u001b[33m17\u001b[39m, total_tokens: \u001b[33m87\u001b[39m }\n", + " \"tool_calls\": [\n", + " {\n", + " \"name\": \"search\",\n", + " \"args\": {\n", + " \"query\": \"current weather in San Francisco\"\n", + " },\n", + " \"type\": \"tool_call\",\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\"\n", + " }\n", + " ],\n", + " \"invalid_tool_calls\": [],\n", + " \"usage_metadata\": {\n", + " \"input_tokens\": 70,\n", + " \"output_tokens\": 17,\n", + " \"total_tokens\": 87\n", + " }\n", " }\n", "]\n", "\n", "====\n", "\n", "[\n", - " [ \u001b[32m'user'\u001b[39m, \u001b[32m\"what's the weather in sf\"\u001b[39m ],\n", + " [ 'user', \"what's the weather in sf\" ],\n", " AIMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " content: \u001b[32m''\u001b[39m,\n", - " tool_calls: \u001b[36m[Array]\u001b[39m,\n", - " invalid_tool_calls: [],\n", - " additional_kwargs: \u001b[36m[Object]\u001b[39m,\n", - " response_metadata: {}\n", + " \"id\": \"chatcmpl-9y660d49eLzT7DZeBk2ZmX8C5f0LU\",\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {\n", + " \"tool_calls\": [\n", + " {\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\",\n", + " \"type\": \"function\",\n", + " \"function\": \"[Object]\"\n", + " }\n", + " ]\n", " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m''\u001b[39m,\n", - " name: \u001b[90mundefined\u001b[39m,\n", - " additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[36m[Array]\u001b[39m },\n", - " response_metadata: { tokenUsage: \u001b[36m[Object]\u001b[39m, finish_reason: \u001b[32m'tool_calls'\u001b[39m },\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_calls: [ \u001b[36m[Object]\u001b[39m ],\n", - " invalid_tool_calls: [],\n", - " usage_metadata: { input_tokens: \u001b[33m70\u001b[39m, output_tokens: \u001b[33m17\u001b[39m, total_tokens: \u001b[33m87\u001b[39m }\n", + " \"response_metadata\": {\n", + " \"tokenUsage\": {\n", + " \"completionTokens\": 17,\n", + " \"promptTokens\": 70,\n", + " \"totalTokens\": 87\n", + " },\n", + " \"finish_reason\": \"tool_calls\",\n", + " \"system_fingerprint\": \"fp_3aa7262c27\"\n", + " },\n", + " \"tool_calls\": [\n", + " {\n", + " \"name\": \"search\",\n", + " \"args\": {\n", + " \"query\": \"current weather in San Francisco\"\n", + " },\n", + " \"type\": \"tool_call\",\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\"\n", + " }\n", + " ],\n", + " \"invalid_tool_calls\": [],\n", + " \"usage_metadata\": {\n", + " \"input_tokens\": 70,\n", + " \"output_tokens\": 17,\n", + " \"total_tokens\": 87\n", + " }\n", " },\n", " ToolMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " name: \u001b[32m'search'\u001b[39m,\n", - " content: \u001b[32m'Cold, with a low of 3℃'\u001b[39m,\n", - " tool_call_id: \u001b[32m'call_cQR8iZwXhIwkTuAsFhWjbPCf'\u001b[39m,\n", - " additional_kwargs: {},\n", - " response_metadata: {}\n", - " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m'Cold, with a low of 3℃'\u001b[39m,\n", - " name: \u001b[32m'search'\u001b[39m,\n", - " additional_kwargs: {},\n", - " response_metadata: {},\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_call_id: \u001b[32m'call_cQR8iZwXhIwkTuAsFhWjbPCf'\u001b[39m\n", + " \"content\": \"Cold, with a low of 3℃\",\n", + " \"name\": \"search\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {},\n", + " \"tool_call_id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\"\n", " }\n", "]\n", "\n", "====\n", "\n", "[\n", - " [ \u001b[32m'user'\u001b[39m, \u001b[32m\"what's the weather in sf\"\u001b[39m ],\n", + " [ 'user', \"what's the weather in sf\" ],\n", " AIMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " content: \u001b[32m''\u001b[39m,\n", - " tool_calls: \u001b[36m[Array]\u001b[39m,\n", - " invalid_tool_calls: [],\n", - " additional_kwargs: \u001b[36m[Object]\u001b[39m,\n", - " response_metadata: {}\n", + " \"id\": \"chatcmpl-9y660d49eLzT7DZeBk2ZmX8C5f0LU\",\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {\n", + " \"tool_calls\": [\n", + " {\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\",\n", + " \"type\": \"function\",\n", + " \"function\": \"[Object]\"\n", + " }\n", + " ]\n", " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m''\u001b[39m,\n", - " name: \u001b[90mundefined\u001b[39m,\n", - " additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[36m[Array]\u001b[39m },\n", - " response_metadata: { tokenUsage: \u001b[36m[Object]\u001b[39m, finish_reason: \u001b[32m'tool_calls'\u001b[39m },\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_calls: [ \u001b[36m[Object]\u001b[39m ],\n", - " invalid_tool_calls: [],\n", - " usage_metadata: { input_tokens: \u001b[33m70\u001b[39m, output_tokens: \u001b[33m17\u001b[39m, total_tokens: \u001b[33m87\u001b[39m }\n", + " \"response_metadata\": {\n", + " \"tokenUsage\": {\n", + " \"completionTokens\": 17,\n", + " \"promptTokens\": 70,\n", + " \"totalTokens\": 87\n", + " },\n", + " \"finish_reason\": \"tool_calls\",\n", + " \"system_fingerprint\": \"fp_3aa7262c27\"\n", + " },\n", + " \"tool_calls\": [\n", + " {\n", + " \"name\": \"search\",\n", + " \"args\": {\n", + " \"query\": \"current weather in San Francisco\"\n", + " },\n", + " \"type\": \"tool_call\",\n", + " \"id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\"\n", + " }\n", + " ],\n", + " \"invalid_tool_calls\": [],\n", + " \"usage_metadata\": {\n", + " \"input_tokens\": 70,\n", + " \"output_tokens\": 17,\n", + " \"total_tokens\": 87\n", + " }\n", " },\n", " ToolMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " name: \u001b[32m'search'\u001b[39m,\n", - " content: \u001b[32m'Cold, with a low of 3℃'\u001b[39m,\n", - " tool_call_id: \u001b[32m'call_cQR8iZwXhIwkTuAsFhWjbPCf'\u001b[39m,\n", - " additional_kwargs: {},\n", - " response_metadata: {}\n", - " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m'Cold, with a low of 3℃'\u001b[39m,\n", - " name: \u001b[32m'search'\u001b[39m,\n", - " additional_kwargs: {},\n", - " response_metadata: {},\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_call_id: \u001b[32m'call_cQR8iZwXhIwkTuAsFhWjbPCf'\u001b[39m\n", + " \"content\": \"Cold, with a low of 3℃\",\n", + " \"name\": \"search\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {},\n", + " \"tool_call_id\": \"call_iD5Wk4vPsTckffDKJpEQaMkg\"\n", " },\n", " AIMessage {\n", - " lc_serializable: \u001b[33mtrue\u001b[39m,\n", - " lc_kwargs: {\n", - " content: \u001b[32m'The current weather in San Francisco is cold, with a low of 3°C.'\u001b[39m,\n", - " tool_calls: [],\n", - " invalid_tool_calls: [],\n", - " additional_kwargs: \u001b[36m[Object]\u001b[39m,\n", - " response_metadata: {}\n", + " \"id\": \"chatcmpl-9y660ZKNXvziVJze0X5aTlZ5IoN35\",\n", + " \"content\": \"Currently, in San Francisco, it's cold with a temperature of around 3℃ (37.4°F).\",\n", + " \"additional_kwargs\": {},\n", + " \"response_metadata\": {\n", + " \"tokenUsage\": {\n", + " \"completionTokens\": 23,\n", + " \"promptTokens\": 103,\n", + " \"totalTokens\": 126\n", + " },\n", + " \"finish_reason\": \"stop\",\n", + " \"system_fingerprint\": \"fp_3aa7262c27\"\n", " },\n", - " lc_namespace: [ \u001b[32m'langchain_core'\u001b[39m, \u001b[32m'messages'\u001b[39m ],\n", - " content: \u001b[32m'The current weather in San Francisco is cold, with a low of 3°C.'\u001b[39m,\n", - " name: \u001b[90mundefined\u001b[39m,\n", - " additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[90mundefined\u001b[39m },\n", - " response_metadata: { tokenUsage: \u001b[36m[Object]\u001b[39m, finish_reason: \u001b[32m'stop'\u001b[39m },\n", - " id: \u001b[90mundefined\u001b[39m,\n", - " tool_calls: [],\n", - " invalid_tool_calls: [],\n", - " usage_metadata: { input_tokens: \u001b[33m103\u001b[39m, output_tokens: \u001b[33m18\u001b[39m, total_tokens: \u001b[33m121\u001b[39m }\n", + " \"tool_calls\": [],\n", + " \"invalid_tool_calls\": [],\n", + " \"usage_metadata\": {\n", + " \"input_tokens\": 103,\n", + " \"output_tokens\": 23,\n", + " \"total_tokens\": 126\n", + " }\n", " }\n", "]\n", "\n",