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

DOC: How to define bindTools method in chatmodel? #7220

Open
2 tasks done
keyiis opened this issue Nov 17, 2024 · 3 comments
Open
2 tasks done

DOC: How to define bindTools method in chatmodel? #7220

keyiis opened this issue Nov 17, 2024 · 3 comments
Labels
auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder auto:question A specific question about the codebase, product, project, or how to use a feature

Comments

@keyiis
Copy link

keyiis commented Nov 17, 2024

Checklist

  • I added a very descriptive title to this issue.
  • I included a link to the documentation page I am referring to (if applicable).

Issue with current documentation:

I noticed that none of the models in the langchain/community/chat_models directory implement the bindTools method. What is the reason for this? If I want to add the bindTools method to a specific model, how should I proceed?

Idea or request for content:

I hope to provide guidelines for implementing the bindTools method for models in the chat_models directory.

Copy link

dosubot bot commented Nov 17, 2024

Hello @keyiis! I'm here to help you with any bugs, questions, or contributions you have. Let's work together to solve this issue.

To implement the bindTools method in a chat model within the langchain/community/chat_models directory, you can follow these steps:

  1. Define the Tools: Create tool objects using LangChain's tool interfaces. For example, you can define a tool using a Zod schema:

    import { tool } from "@langchain/core/tools";
    import { z } from "zod";
    
    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 calculatorTool = tool(async ({ operation, number1, number2 }) => {
      if (operation === "add") {
        return `${number1 + number2}`;
      } else if (operation === "subtract") {
        return `${number1 - number2}`;
      } else if (operation === "multiply") {
        return `${number1 * number2}`;
      } else if (operation === "divide") {
        return `${number1 / number2}`;
      } else {
        throw new Error("Invalid operation.");
      }
    }, {
      name: "calculator",
      description: "Can perform mathematical operations.",
      schema: calculatorSchema,
    });
  2. Bind Tools to the Model: Use the .bindTools() method to bind the defined tools to your chat model. This method takes a list of tool objects and binds them to the model in the expected format. Ensure that your chat model implements the bindTools method, as it is required for using structured outputs.

    const llmWithTools = llm.bindTools([calculatorTool]);
  3. Invoke the Model: Once the tools are bound, you can invoke the model, and it will include the tool schemas in its calls to the LLM.

    const res = await llmWithTools.invoke("What is 3 * 12");
    console.log(res);

These steps will allow you to implement the bindTools method in a new chat model, enabling it to generate arguments for tool calls and include tool schemas in its interactions [1][2].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder auto:question A specific question about the codebase, product, project, or how to use a feature labels Nov 17, 2024
@keyiis
Copy link
Author

keyiis commented Nov 17, 2024

@dosu I used @langchain/community/chat_models/alibaba_tongyi to creat llm,but it dosen't define bindTools method.

Copy link

dosubot bot commented Nov 17, 2024

The ChatAlibabaTongyi model does not currently implement the bindTools method. If you want to add this functionality, you can look at how other models, like ChatDeepInfra, implement the bindTools method. Here's a brief example of how it might be done:

override bindTools(
  tools: BindToolsInput[],
  kwargs?: Partial<DeepInfraCallOptions>
): Runnable<BaseLanguageModelInput, AIMessageChunk, DeepInfraCallOptions> {
  return this.bind({
    tools: tools.map((tool) => convertToOpenAITool(tool)),
    ...kwargs,
  } as DeepInfraCallOptions);
}

This method allows the model to bind tools by converting them to the appropriate format and passing them along with any additional options [1]. You can adapt this approach to implement bindTools in the ChatAlibabaTongyi model.

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder auto:question A specific question about the codebase, product, project, or how to use a feature
Projects
None yet
Development

No branches or pull requests

1 participant