langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.57k stars 2.15k forks source link

Anthropic Tools doesn't make function call #4807

Closed elie222 closed 6 months ago

elie222 commented 7 months ago

Checked other resources

Example Code

The following code should make a function call but doesn't.

import { ChatAnthropicTools } from "@langchain/anthropic/experimental";
import { HumanMessage } from "@langchain/core/messages";
import { ToolDefinition } from "@langchain/core/language_models/base";

export async function anthropicChatCompletionTools(
  messages: Array<{
    role: "assistant" | "user";
    content: string;
  }>,
  tools: ToolDefinition[],
) {
  const anthropic = new ChatAnthropicTools({
    modelName: "claude-3-opus-20240229",
    anthropicApiKey: env.ANTHROPIC_API_KEY,
  });

  anthropic.bind({
    tools,
    // You can set the `function_call` arg to force the model to use a function
    tool_choice: {
            type: "function",
            function: {
              name: tools[0].function.name,
            },
          }
  });

  const response = await anthropic.invoke([
    new HumanMessage({
      content: messages.map((m) => m.content).join("\n\n"),
    }),
  ]);

// bug: responds with content instead of function call
  console.log(response);

  return response;
}

Error Message and Stack Trace (if applicable)

import { ChatAnthropicTools } from "@langchain/anthropic/experimental"; import { HumanMessage } from "@langchain/core/messages"; import { ToolDefinition } from "@langchain/core/language_models/base";

Description

System Info

node v18.19.0 pnpm 8.15.4

"@langchain/anthropic": "^0.1.8", "@langchain/core": "^0.1.48",

dosubot[bot] commented 7 months ago

Based on the context provided, it seems like there might be a misunderstanding about the bind method in the ChatAnthropicTools class. According to the LangChain JS codebase, there is no bind method in the ChatAnthropicTools class. This could be the reason why the function call isn't working as expected.

The invoke method in the ChatAnthropicTools class is designed to process a message input and generate a response. However, it doesn't inherently support making function calls. Instead, it returns a ChatResult object that contains plain text content. This could explain why you're seeing plain text content instead of a function call.

The correct way to make function calls using the ChatAnthropicTools class involves using the invoke method with a HumanMessage object that prompts the use of the bound tool. However, in your code, it seems like the tool_choice argument is being set to force the model to use a specific function. This might not be the correct approach, as the invoke method doesn't support this functionality.

I recommend revisiting the usage of the ChatAnthropicTools class and the invoke method. You might need to adjust your code to align with the expected usage of these components. You can refer to the examples/src/models/chat/anthropic_tools/tool_calling.ts file in the LangChain JS repository for an example of how to use the ChatAnthropicTools class correctly.

Regarding the issue you found in the LangChain JS repository, it seems to be a different issue that doesn't directly relate to your problem. The issue is about a TypeError when reading properties of undefined in a different part of the codebase.

I hope this helps clarify the situation. If you have any more questions or need further clarification, feel free to ask.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
jacoblee93 commented 7 months ago

What is the actual logged response?

And could you share the tool schemas?

sessycode commented 6 months ago

ChatAnthropicTools is deprecated. Use ChatAnthropic instead. see. https://js.langchain.com/docs/integrations/chat/anthropic/

elie222 commented 6 months ago

ChatAnthropicTools is deprecated. Use ChatAnthropic instead. see. js.langchain.com/docs/integrations/chat/anthropic

Thanks. Seems like this also has issues:

Object literal may only specify known properties, and 'tools' does not exist in type 'Partial<BaseLanguageModelCallOptions>'.ts(2353)

Code:

const calculatorSchema = z.object({
  operation: z
    .enum(["add", "subtract", "multiply", "divide"])
    .describe("The type of operation to execute."),
  number1: z.number().describe("The first number to operate on."),
  number2: z.number().describe("The second number to operate on."),
});

const tool = {
  name: "calculator",
  description: "A simple calculator tool",
  input_schema: zodToJsonSchema(calculatorSchema),
};

const model = new ChatAnthropic({
  anthropicApiKey: '',
  modelName: "claude-3-haiku-20240307",
}).bind({
  tools: [tool],
});

CleanShot 2024-04-09 at 00 49 15@2x

May just be a types issues.

sessycode commented 6 months ago

I updated all the dependencies for using new released ChatAnthropic.

Here is mine.

"dependencies": { "@langchain/anthropic": "^0.1.10", "@langchain/community": "^0.0.44", "@langchain/core": "^0.1.54", "@langchain/google-vertexai": "^0.0.2", "langchain": "^0.1.31", "ts-node": "^10.9.2", "zod": "^3.22.4", "zod-to-json-schema": "^3.22.5" }

elie222 commented 6 months ago

Working now! Thanks! Think I needed to do a TS restart. Weird because almost certain I did yesterday.