Skip to content

Commit

Permalink
updated stream updates doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent f1da02f commit 5a02cd9
Showing 1 changed file with 72 additions and 59 deletions.
131 changes: 72 additions & 59 deletions examples/how-tos/stream-updates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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-...\";"
]
},
{
Expand All @@ -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<IState>[\"channels\"] = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
"};"
"const GraphState = Annotation.Root({\n",
" messages: Annotation<BaseMessage[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" }),\n",
"});"
]
},
{
Expand Down Expand Up @@ -130,7 +124,7 @@
"source": [
"import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n",
"\n",
"const toolNode = new ToolNode<{ messages: BaseMessage[] }>(tools);"
"const toolNode = new ToolNode<typeof GraphState.State>(tools);"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -236,9 +230,7 @@
" return { messages: [responseMessage] };\n",
"};\n",
"\n",
"const workflow = new StateGraph<IState>({\n",
" channels: graphState,\n",
"})\n",
"const workflow = new StateGraph(GraphState)\n",
" .addNode(\"agent\", callModel)\n",
" .addNode(\"tools\", toolNode)\n",
" .addEdge(START, \"agent\")\n",
Expand Down Expand Up @@ -272,17 +264,42 @@
"{\n",
" messages: [\n",
" AIMessage {\n",
" lc_serializable: \u001b[33mtrue\u001b[39m,\n",
" lc_kwargs: \u001b[36m[Object]\u001b[39m,\n",
" lc_namespace: \u001b[36m[Array]\u001b[39m,\n",
" content: \u001b[32m''\u001b[39m,\n",
" name: \u001b[90mundefined\u001b[39m,\n",
" additional_kwargs: \u001b[36m[Object]\u001b[39m,\n",
" response_metadata: \u001b[36m[Object]\u001b[39m,\n",
" id: \u001b[90mundefined\u001b[39m,\n",
" tool_calls: \u001b[36m[Array]\u001b[39m,\n",
" invalid_tool_calls: [],\n",
" usage_metadata: \u001b[36m[Object]\u001b[39m\n",
" \"id\": \"chatcmpl-9y654VypbD3kE1xM8v4xaAHzZEOXa\",\n",
" \"content\": \"\",\n",
" \"additional_kwargs\": {\n",
" \"tool_calls\": [\n",
" {\n",
" \"id\": \"call_OxlOhnROermwae2LPs9SanmD\",\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",
" \"tool_calls\": [\n",
" {\n",
" \"name\": \"search\",\n",
" \"args\": {\n",
" \"query\": \"current weather in San Francisco\"\n",
" },\n",
" \"type\": \"tool_call\",\n",
" \"id\": \"call_OxlOhnROermwae2LPs9SanmD\"\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",
Expand All @@ -293,15 +310,11 @@
"{\n",
" messages: [\n",
" ToolMessage {\n",
" lc_serializable: \u001b[33mtrue\u001b[39m,\n",
" lc_kwargs: \u001b[36m[Object]\u001b[39m,\n",
" lc_namespace: \u001b[36m[Array]\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_htjQLLIUKKaxLDAU26QqPlJn'\u001b[39m\n",
" \"content\": \"Cold, with a low of 3℃\",\n",
" \"name\": \"search\",\n",
" \"additional_kwargs\": {},\n",
" \"response_metadata\": {},\n",
" \"tool_call_id\": \"call_OxlOhnROermwae2LPs9SanmD\"\n",
" }\n",
" ]\n",
"}\n",
Expand All @@ -312,17 +325,25 @@
"{\n",
" messages: [\n",
" AIMessage {\n",
" lc_serializable: \u001b[33mtrue\u001b[39m,\n",
" lc_kwargs: \u001b[36m[Object]\u001b[39m,\n",
" lc_namespace: \u001b[36m[Array]\u001b[39m,\n",
" content: \u001b[32m'The current weather in San Francisco is cold, with a low of 3°C (37.4°F).'\u001b[39m,\n",
" name: \u001b[90mundefined\u001b[39m,\n",
" additional_kwargs: \u001b[36m[Object]\u001b[39m,\n",
" response_metadata: \u001b[36m[Object]\u001b[39m,\n",
" id: \u001b[90mundefined\u001b[39m,\n",
" tool_calls: [],\n",
" invalid_tool_calls: [],\n",
" usage_metadata: \u001b[36m[Object]\u001b[39m\n",
" \"id\": \"chatcmpl-9y654dZ0zzZhPYm6lb36FkG1Enr3p\",\n",
" \"content\": \"It looks like it's currently quite cold in San Francisco, with a low temperature of around 3°C. Make sure to dress warmly!\",\n",
" \"additional_kwargs\": {},\n",
" \"response_metadata\": {\n",
" \"tokenUsage\": {\n",
" \"completionTokens\": 28,\n",
" \"promptTokens\": 103,\n",
" \"totalTokens\": 131\n",
" },\n",
" \"finish_reason\": \"stop\",\n",
" \"system_fingerprint\": \"fp_3aa7262c27\"\n",
" },\n",
" \"tool_calls\": [],\n",
" \"invalid_tool_calls\": [],\n",
" \"usage_metadata\": {\n",
" \"input_tokens\": 103,\n",
" \"output_tokens\": 28,\n",
" \"total_tokens\": 131\n",
" }\n",
" }\n",
" ]\n",
"}\n",
Expand All @@ -347,14 +368,6 @@
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8992a85-fac1-4550-9965-05664cec65ea",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 5a02cd9

Please sign in to comment.