Skip to content

Commit

Permalink
update force calling a tool doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent 05c7f49 commit 8f10e8f
Showing 1 changed file with 78 additions and 44 deletions.
122 changes: 78 additions & 44 deletions examples/how-tos/force-calling-a-tool-first.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<IState>[\"channels\"] = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
"};"
"const AgentState = Annotation.Root({\n",
" messages: Annotation<BaseMessage[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" }),\n",
"});"
]
},
{
Expand Down Expand Up @@ -271,7 +257,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"id": "eee5adc0",
"metadata": {},
"outputs": [],
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -326,15 +312,15 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"id": "e9104fc7",
"metadata": {
"lines_to_next_cell": 2
},
"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",
Expand Down Expand Up @@ -371,7 +357,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"id": "de6da918",
"metadata": {
"lines_to_next_cell": 2
Expand All @@ -381,7 +367,7 @@
"import { END, START, StateGraph } from \"@langchain/langgraph\";\n",
"\n",
"// Define a new graph\n",
"const workflow = new StateGraph<IState>({ 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",
Expand Down Expand Up @@ -436,7 +422,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"id": "acaade41",
"metadata": {
"lines_to_next_cell": 2
Expand All @@ -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"
]
Expand All @@ -470,24 +512,16 @@
" console.log(\"-----\\n\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "241e2fc9-9bee-4f2c-a077-09472a7b5613",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"jupytext": {
"encoding": "# -*- coding: utf-8 -*-"
},
"kernelspec": {
"display_name": "Deno",
"display_name": "TypeScript",
"language": "typescript",
"name": "deno"
"name": "tslab"
},
"language_info": {
"codemirror_mode": {
Expand Down

0 comments on commit 8f10e8f

Please sign in to comment.