Skip to content

Commit

Permalink
Adds more advanced test
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Aug 28, 2024
1 parent 4fc252e commit 93997ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"// Define the tools for the agent to use\n",
"const tools = [new TavilySearchResults({ maxResults: 3 })];\n",
"\n",
"const toolNode = new ToolNode<typeof GraphAnnotation.State>(tools);\n",
"const toolNode = new ToolNode(tools);\n",
"\n",
"const model = new ChatOpenAI({ temperature: 0 }).bindTools(tools);\n",
"\n",
Expand Down Expand Up @@ -282,4 +282,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
41 changes: 35 additions & 6 deletions libs/langgraph/src/tests/prebuilt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,6 @@ describe("ToolNode", () => {
}
);

const graph = new StateGraph(AgentAnnotation)
.addNode("tools", new ToolNode([weatherTool]))
.addEdge("__start__", "tools")
.addEdge("tools", "__end__")
.compile();
const aiMessage = new AIMessage({
content: "",
tool_calls: [
Expand All @@ -542,8 +537,41 @@ describe("ToolNode", () => {
},
],
});

const aiMessage2 = new AIMessage({
content: "FOO",
});

async function callModel(state: typeof AgentAnnotation.State) {
// We return a list, because this will get added to the existing list
if (state.messages.includes(aiMessage)) {
return { messages: [aiMessage2] };
}
return { messages: [aiMessage] };
}

function shouldContinue(
{ messages }: typeof AgentAnnotation.State
): "tools" | "__end__" {
const lastMessage: AIMessage = messages[messages.length - 1];

// If the LLM makes a tool call, then we route to the "tools" node
if ((lastMessage.tool_calls?.length ?? 0) > 0) {
return "tools";
}
// Otherwise, we stop (reply to the user)
return "__end__";
}

const graph = new StateGraph(AgentAnnotation)
.addNode("agent", callModel)
.addNode("tools", new ToolNode([weatherTool]))
.addEdge("__start__", "agent")
.addConditionalEdges("agent", shouldContinue)
.addEdge("tools", "agent")
.compile();
const res = await graph.invoke({
messages: [aiMessage],
messages: [],
});
const toolMessageId = res.messages[1].id;
expect(res).toEqual({
Expand All @@ -556,6 +584,7 @@ describe("ToolNode", () => {
content: "It's 60 degrees and foggy.",
tool_call_id: "call_1234",
}),
aiMessage2,
],
});
});
Expand Down

0 comments on commit 93997ac

Please sign in to comment.