Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anthropic[patch]: ensure content exists when parsing tool tokens #6183

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ function extractToolCallChunk(
function extractToken(chunk: AIMessageChunk): string | undefined {
if (typeof chunk.content === "string") {
return chunk.content;
} else if (Array.isArray(chunk.content) && "input" in chunk.content[0]) {
} else if (
Array.isArray(chunk.content) &&
chunk.content.length >= 1 &&
"input" in chunk.content[0]
) {
return typeof chunk.content[0].input === "string"
? chunk.content[0].input
: JSON.stringify(chunk.content[0].input);
Expand Down
38 changes: 38 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 @@ -402,3 +402,41 @@ test("Can stream tool calls", async () => {
expect(finalChunk?.tool_calls?.[0].args.location).toBeDefined();
expect(realToolCallChunkStreams).toBeGreaterThan(1);
});

test("llm token callbacks can handle tool calls", async () => {
const weatherTool = tool((_) => "no-op", {
name: "get_weather",
description: zodSchema.description,
schema: zodSchema,
});

const modelWithTools = model.bindTools([weatherTool], {
tool_choice: {
type: "tool",
name: "get_weather",
},
});

let tokens = "";
const stream = await modelWithTools.stream("What is the weather in SF?", {
callbacks: [
{
handleLLMNewToken: (tok) => {
tokens += tok;
},
},
],
});

let finalChunk: AIMessageChunk | undefined;
for await (const chunk of stream) {
finalChunk = !finalChunk ? chunk : concat(finalChunk, chunk);
}

expect(finalChunk?.tool_calls?.[0]).toBeDefined();
expect(finalChunk?.tool_calls?.[0].name).toBe("get_weather");
expect(finalChunk?.tool_calls?.[0].args).toBeDefined();
const args = finalChunk?.tool_calls?.[0].args;
if (!args) return;
expect(args).toEqual(JSON.parse(tokens));
});
Loading