Skip to content

Commit

Permalink
updated streaming tokens without langchain doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent 2398f9c commit 867496c
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions examples/how-tos/streaming-tokens-without-langchain.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,29 @@
"source": [
"import { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\n",
"import { wrapOpenAI } from \"langsmith/wrappers/openai\";\n",
"import { Annotation } from \"@langchain/langgraph\";\n",
"\n",
"type GraphState = {\n",
" messages: OpenAI.ChatCompletionMessageParam[];\n",
"};\n",
"const GraphState = Annotation.Root({\n",
" messages: Annotation<OpenAI.ChatCompletionMessageParam[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" }),\n",
"});\n",
"\n",
"// If using LangSmith, use \"wrapOpenAI\" on the whole client or\n",
"// \"traceable\" to wrap a single method for nicer tracing:\n",
"// https://docs.smith.langchain.com/how_to_guides/tracing/annotate_code\n",
"const wrappedClient = wrapOpenAI(openaiClient);\n",
"\n",
"const callModel = async ({ messages }: GraphState) => {\n",
"const callModel = async (state: typeof GraphState.State): Promise<Partial<typeof GraphState.State>> => {\n",
" const { messages } = state;\n",
" const stream = await wrappedClient.chat.completions.create({\n",
" messages,\n",
" model: \"gpt-4o-mini\",\n",
" tools: [toolSchema],\n",
" stream: true,\n",
" });\n",
" let responseContent = \"\";\n",
" let role = \"assistant\";\n",
" let role: string = \"assistant\";\n",
" let toolCallId: string | undefined;\n",
" let toolCallName: string | undefined;\n",
" let toolCallArgs = \"\";\n",
Expand Down Expand Up @@ -133,11 +137,12 @@
" name: toolCallName,\n",
" arguments: toolCallArgs\n",
" },\n",
" type: \"function\",\n",
" type: \"function\" as const,\n",
" }];\n",
" }\n",
"\n",
" const responseMessage = {\n",
" role,\n",
" role: role as any,\n",
" content: responseContent,\n",
" tool_calls: finalToolCalls,\n",
" };\n",
Expand Down Expand Up @@ -172,7 +177,8 @@
" }\n",
"};\n",
"\n",
"const callTools = async ({ messages }: GraphState) => {\n",
"const callTools = async (state: typeof GraphState.State): Promise<Partial<typeof GraphState.State>> => {\n",
" const { messages } = state;\n",
" const mostRecentMessage = messages[messages.length - 1];\n",
" const toolCalls = (mostRecentMessage as OpenAI.ChatCompletionAssistantMessageParam).tool_calls;\n",
" if (toolCalls === undefined || toolCalls.length === 0) {\n",
Expand All @@ -186,7 +192,7 @@
" const response = await toolNameMap[functionName](functionArguments);\n",
" const toolMessage = {\n",
" tool_call_id: toolCalls[0].id,\n",
" role: \"tool\",\n",
" role: \"tool\" as const,\n",
" name: functionName,\n",
" content: response,\n",
" }\n",
Expand All @@ -210,13 +216,11 @@
"outputs": [],
"source": [
"import { StateGraph } from \"@langchain/langgraph\";\n",
"\n",
"import OpenAI from \"openai\";\n",
"type GraphState = {\n",
" messages: OpenAI.ChatCompletionMessageParam[];\n",
"};\n",
"\n",
"const shouldContinue = ({ messages }: GraphState) => {\n",
"// We can reuse the same `GraphState` from above as it has not changed.\n",
"const shouldContinue = (state: typeof GraphState.State) => {\n",
" const { messages } = state;\n",
" const lastMessage =\n",
" messages[messages.length - 1] as OpenAI.ChatCompletionAssistantMessageParam;\n",
" if (lastMessage?.tool_calls !== undefined && lastMessage?.tool_calls.length > 0) {\n",
Expand All @@ -225,15 +229,7 @@
" return \"__end__\";\n",
"}\n",
"\n",
"const workflow = new StateGraph<GraphState>({\n",
" channels: {\n",
" messages: {\n",
" reducer: (a: any, b: any) => a.concat(b),\n",
" }\n",
" }\n",
"});\n",
"\n",
"const graph = workflow\n",
"const graph = new StateGraph(GraphState)\n",
" .addNode(\"model\", callModel)\n",
" .addNode(\"tools\", callTools)\n",
" .addEdge(\"__start__\", \"model\")\n",
Expand All @@ -242,7 +238,8 @@
" __end__: \"__end__\",\n",
" })\n",
" .addEdge(\"tools\", \"model\")\n",
" .compile();"
" .compile();\n",
" "
]
},
{
Expand Down Expand Up @@ -288,7 +285,7 @@
"text": [
"streamed_tool_call_chunk {\n",
" index: 0,\n",
" id: 'call_1Lt33wOgPfjXwvM9bcWoe1yZ',\n",
" id: 'call_v99reml4gZvvUypPgOpLgxM2',\n",
" type: 'function',\n",
" function: { name: 'get_items', arguments: '' }\n",
"}\n",
Expand All @@ -302,7 +299,8 @@
"streamed_token { content: ' the' }\n",
"streamed_token { content: ' bedroom' }\n",
"streamed_token { content: ',' }\n",
"streamed_token { content: \" you'll\" }\n",
"streamed_token { content: ' you' }\n",
"streamed_token { content: ' can' }\n",
"streamed_token { content: ' find' }\n",
"streamed_token { content: ' socks' }\n",
"streamed_token { content: ',' }\n",
Expand Down

0 comments on commit 867496c

Please sign in to comment.