Skip to content

Commit

Permalink
fixed ntbk
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 26, 2024
1 parent 6b17896 commit c59ed88
Showing 1 changed file with 92 additions and 23 deletions.
115 changes: 92 additions & 23 deletions examples/how-tos/streaming-events-from-within-tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 11,
"id": "2cb38dd9-74d8-456d-9e39-4655f2bf3f37",
"metadata": {},
"outputs": [],
Expand All @@ -63,16 +63,16 @@
"const model = new ChatAnthropic({\n",
" model: \"claude-3-5-sonnet-20240620\",\n",
" temperature: 0,\n",
"})\n",
"});\n",
"\n",
"const getItems = tool(\n",
" async (input, config) => {\n",
" const template = ChatPromptTemplate.fromMessages([\n",
" [\n",
" \"human\",\n",
" \"Can you tell me what kind of items i might find in the following place: '{place}'. \" +\n",
" \"List at least 3 such items separating them by a comma. And include a brief description of each item..\",\n",
" ]\n",
" \"List at least 3 such items separating them by a comma. And include a brief description of each item..\",\n",
" ],\n",
" ]);\n",
"\n",
" const modelWithConfig = model.withConfig({\n",
Expand All @@ -88,10 +88,10 @@
" name: \"get_items\",\n",
" description: \"Use this tool to look up which items are in the given place.\",\n",
" schema: z.object({\n",
" place: z.string(),\n",
" place: z.string().describe(\"The place to look up items for. E.g 'shelf'\"),\n",
" }),\n",
" }\n",
")"
");"
]
},
{
Expand All @@ -104,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 12,
"id": "7254310e-7016-45f7-9795-6d52a1160086",
"metadata": {},
"outputs": [],
Expand All @@ -114,7 +114,7 @@
"const agent = createReactAgent({\n",
" llm: model,\n",
" tools: [getItems],\n",
"})\n"
"});"
]
},
{
Expand All @@ -127,25 +127,73 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "4399d9e9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ type: 'ai', content: 'Here' }\n",
"{ type: 'ai', content: ' are three items you might' }\n",
"{ type: 'ai', content: ' find on a shelf,' }\n",
"{ type: 'ai', content: ' along with brief' }\n",
"{ type: 'ai', content: ' descriptions:\\n\\n1.' }\n",
"{ type: 'ai', content: ' Books' }\n",
"{ type: 'ai', content: ': Boun' }\n",
"{ type: 'ai', content: 'd collections of printe' }\n",
"{ type: 'ai', content: 'd pages' }\n",
"{ type: 'ai', content: ' containing' }\n",
"{ type: 'ai', content: ' various' }\n",
"{ type: 'ai', content: ' forms' }\n",
"{ type: 'ai', content: ' of literature, information' }\n",
"{ type: 'ai', content: ', or reference' }\n",
"{ type: 'ai', content: ' material.\\n\\n2.' }\n",
"{ type: 'ai', content: ' Picture' }\n",
"{ type: 'ai', content: ' frames: Decorative' }\n",
"{ type: 'ai', content: ' borders' }\n",
"{ type: 'ai', content: ' used to display an' }\n",
"{ type: 'ai', content: 'd protect photographs, artwork' }\n",
"{ type: 'ai', content: ', or other visual memor' }\n",
"{ type: 'ai', content: 'abilia.\\n\\n3' }\n",
"{ type: 'ai', content: '. Pot' }\n",
"{ type: 'ai', content: 'ted plants: Small' }\n",
"{ type: 'ai', content: ' indoor' }\n",
"{ type: 'ai', content: ' plants in' }\n",
"{ type: 'ai', content: ' containers, often used for' }\n",
"{ type: 'ai', content: ' decoration or to add a' }\n",
"{ type: 'ai', content: ' touch of nature to indoor' }\n",
"{ type: 'ai', content: ' spaces.' }\n"
]
}
],
"source": [
"let finalEvent\n",
"let finalEvent;\n",
"\n",
"for await (const event of agent.streamEvents(\n",
" {\n",
" messages: [[\"human\", \"what items are on the shelf?\"]],\n",
" messages: [\n",
" [\n",
" \"human\",\n",
" \"what items are on the shelf? You should call the get_items tool.\",\n",
" ],\n",
" ],\n",
" },\n",
" {\n",
" version: \"v2\"\n",
" version: \"v2\",\n",
" },\n",
" {\n",
" includeTags: [\"tool_llm\"],\n",
" }\n",
")) {\n",
" console.log(event.data);\n",
" finalEvent = event\n",
" if (\"chunk\" in event.data) {\n",
" console.dir({\n",
" type: event.data.chunk._getType(),\n",
" content: event.data.chunk.content,\n",
" })\n",
" }\n",
" finalEvent = event;\n",
"}"
]
},
Expand All @@ -159,17 +207,38 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 21,
"id": "ca382c1f-b1c7-4c8a-bd9b-7a873b891b3e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" type: 'ai',\n",
" content: 'Here are three items you might find on a shelf, along with brief descriptions:\\n' +\n",
" '\\n' +\n",
" '1. Books: Bound collections of printed pages containing various forms of literature, information, or reference material.\\n' +\n",
" '\\n' +\n",
" '2. Picture frames: Decorative borders used to display and protect photographs, artwork, or other visual memorabilia.\\n' +\n",
" '\\n' +\n",
" '3. Potted plants: Small indoor plants in containers, often used for decoration or to add a touch of nature to indoor spaces.',\n",
" tool_calls: []\n",
"}\n"
]
}
],
"source": [
"const finalMessages = finalEvent.data.output.messages;\n",
"console.dir(finalMessages.map((msg) => ({\n",
" type: msg._getType(),\n",
" content: msg.content,\n",
" tool_calls: msg.tool_calls,\n",
"})), { depth: null });"
"const finalMessage = finalEvent?.data.output;\n",
"console.dir(\n",
" {\n",
" type: finalMessage._getType(),\n",
" content: finalMessage.content,\n",
" tool_calls: finalMessage.tool_calls,\n",
" },\n",
" { depth: null }\n",
");\n"
]
},
{
Expand Down

0 comments on commit c59ed88

Please sign in to comment.