Skip to content

Commit

Permalink
anthropic[patch]: Force tool call by name in withStructuredOutput (#5933
Browse files Browse the repository at this point in the history
)

* anthropic[patch]: Force tool call by name in withStructuredOutput

* chore: lint files
  • Loading branch information
bracesproul authored Jun 28, 2024
1 parent 807bcbc commit 0a9090d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,10 @@ export class ChatAnthropicMessages<
}
const llm = this.bind({
tools,
tool_choice: "any",
tool_choice: {
type: "tool",
name: functionName,
},
} as Partial<CallOptions>);

if (!includeRaw) {
Expand Down
24 changes: 24 additions & 0 deletions libs/langchain-anthropic/src/tests/chat_models-tools.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,27 @@ test("bindTools accepts openai formatted tool", async () => {
}
expect(tool_calls[0].name).toBe("get_weather");
});

test("withStructuredOutput will always force tool usage", async () => {
const weatherTool = z
.object({
location: z.string().describe("The name of city to get the weather for."),
})
.describe(
"Get the weather of a specific location and return the temperature in Celsius."
);
const modelWithTools = model.withStructuredOutput(weatherTool, {
name: "get_weather",
includeRaw: true,
});
const response = await modelWithTools.invoke(
"What is the sum of 271623 and 281623? It is VERY important you use a calculator tool to give me the answer."
);

if (!("tool_calls" in response.raw)) {
throw new Error("Tool call not found in response");
}
const castMessage = response.raw as AIMessage;
expect(castMessage.tool_calls).toHaveLength(1);
expect(castMessage.tool_calls?.[0].name).toBe("get_weather");
});

0 comments on commit 0a9090d

Please sign in to comment.