Skip to content

Commit

Permalink
Only populate tool params if sensible
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Apr 10, 2024
1 parent ddcd286 commit 11b7b82
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
45 changes: 26 additions & 19 deletions libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,35 @@ function _formatMessagesForAnthropic(messages: BaseMessage[]): {
throw new Error(`Message type "${message._getType()}" is not supported.`);
}
if (isAIMessage(message) && !!message.tool_calls?.length) {
const rawContent = _formatContent(message.content);
if (typeof rawContent === "string") {
if (message.content === "") {
return {
role,
content: [
{ type: "text", text: rawContent },
...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
],
content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
};
} else if (typeof message.content === "string") {
console.warn(
`The "tool_calls" field on a message is only respected if content is an empty string.`
);
return {
role,
content: _formatContent(message.content),
};
} else {
const missingToolCallBlocks = message.tool_calls.filter(
(toolCall) =>
!!rawContent.find(
(contentPart) =>
contentPart === "tool_use" && toolCall.id === contentPart.id
)
const { content } = message;
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>
content.find(
(contentPart) =>
contentPart.type === "tool_use" && contentPart.id === toolCall.id
)
);
if (hasMismatchedToolCalls) {
console.warn(
`The "tool_calls" field on a message is only respected if content is an empty string.`
);
}
return {
role,
content: [
...rawContent,
...missingToolCallBlocks.map(_convertLangChainToolCallToAnthropic),
],
content: _formatContent(message.content),
};
}
} else {
Expand Down Expand Up @@ -601,11 +607,12 @@ export class ChatAnthropicMessages<
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
const params = this.invocationParams(options);
const formattedMessages = _formatMessagesForAnthropic(messages);
const requestOptions = this.invocationOptions(
{
...params,
stream: false,
..._formatMessagesForAnthropic(messages),
...formattedMessages,
},
options
);
Expand All @@ -614,7 +621,7 @@ export class ChatAnthropicMessages<
{
...params,
stream: false,
..._formatMessagesForAnthropic(messages),
...formattedMessages,
},
options
);
Expand Down Expand Up @@ -644,7 +651,7 @@ export class ChatAnthropicMessages<
const stream = await this.createStreamWithRetry(
{
...params,
..._formatMessagesForAnthropic(messages),
...formattedMessages,
stream: true,
},
requestOptions
Expand Down
20 changes: 12 additions & 8 deletions libs/langchain-anthropic/src/tests/chat_models-tools.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ test("Few shotting with tool calls", async () => {
const res = await chat.invoke([
new HumanMessage("What is the weather in SF?"),
new AIMessage({
content: "Let me look that up!",
content: "",
tool_calls: [
{
id: "toolu_feiwjf9u98r389u498",
name: "get_current_weather",
name: "get_weather",
args: {
location: "SF",
},
Expand All @@ -88,7 +88,7 @@ test("Can bind & invoke StructuredTools", async () => {
const modelWithTools = model.bindTools(tools);

const result = await modelWithTools.invoke(
"What is the weather in London today?"
"What is the weather in SF today?"
);
console.log(
{
Expand Down Expand Up @@ -116,17 +116,21 @@ test("Can bind & invoke StructuredTools", async () => {
expect(input).toBeTruthy();
expect(input.location).toBeTruthy();
const result2 = await modelWithTools.invoke([
new HumanMessage("What is the weather in London today?"),
new HumanMessage("What is the weather in SF today?"),
result,
new ToolMessage({
tool_call_id: result.tool_calls?.[0].id ?? "",
content: "The weather in London is currently 98 degrees and sunny.",
content:
"The weather in San Francisco is currently 59 degrees and sunny.",
}),
new AIMessage("The weather in London is currently 98 degrees and sunny."),
new HumanMessage("What did you just say the weather was?"),
new AIMessage(
"The weather in San Francisco is currently 59 degrees and sunny."
),
new HumanMessage("What did you say the weather was?"),
]);
console.log(result2);
expect(result2.content).toContain("98");
// This should work, but Anthorpic is too skeptical
expect(result2.content).toContain("59");
});

test("Can bind & invoke AnthropicTools", async () => {
Expand Down

0 comments on commit 11b7b82

Please sign in to comment.