langchain-ai / langchainjs

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

model.bindTools not a function: Can No Longer bindTools to ChatOpenAI model #5773

Closed radiantone closed 3 months ago

radiantone commented 3 months ago

Checked other resources

Example Code

        let model = new ChatOpenAI({ openAIApiKey: apiKey, temperature: temp });
...
...
          const llmWithTools = model.bindTools(tools);

Error Message and Stack Trace (if applicable)

TypeError: model.bindTools is not a function
    at chatModel (ChatModelTemplate.vue:2411:38)
    at Proxy.executeChatModelWithTools (ChatModelTemplate.vue:2457:7)
    at onClick (ChatModelTemplate.vue:586:21)
    at callWithErrorHandling (chunk-SSEX44ZS.js?v=a33dcfdd:1824:18)
    at callWithAsyncErrorHandling (chunk-SSEX44ZS.js?v=a33dcfdd:1832:17)
    at emit (chunk-SSEX44ZS.js?v=a33dcfdd:2280:5)
    at navigateOnClick (quasar_dist_quasar_client_js.js?v=a33dcfdd:2234:7)
    at onClick (quasar_dist_quasar_client_js.js?v=a33dcfdd:2777:7)
    at callWithErrorHandling (chunk-SSEX44ZS.js?v=a33dcfdd:1824:18)
    at callWithAsyncErrorHandling (chunk-SSEX44ZS.js?v=a33dcfdd:1832:17)

Description

This worked before recent update

System Info

langchain@0.2.5 | MIT | deps: 16 | versions: 277 Typescript bindings for langchain https://github.com/langchain-ai/langchainjs/tree/main/langchain/

keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores

dist .tarball: https://registry.npmjs.org/langchain/-/langchain-0.2.5.tgz .shasum: fc23848c20244a1d0a3dd5dd4663af9a41017ccb .integrity: sha512-H5WL0NanCdQ+tzoeEt7Fyz9YGdR3wbfDvfQrJvxAO95istKo5JraRh24dzyvqxM9439xwRMNaMIpMwsyqtWDtQ== .unpackedSize: 4.0 MB

dependencies: @langchain/core: ~0.2.0 @langchain/openai: ~0.1.0 @langchain/textsplitters: ~0.0.0 binary-extensions: ^2.2.0 js-tiktoken: ^1.0.12 js-yaml: ^4.1.0 jsonpointer: ^5.0.1 langchainhub: ~0.0.8 langsmith: ~0.1.30 ml-distance: ^4.0.0 openapi-types: ^12.1.3 p-retry: 4 uuid: ^9.0.0 yaml: ^2.2.1 zod-to-json-schema: ^3.22.3 zod: ^3.22.4

maintainers:

dist-tags: latest: 0.2.5 next: 0.2.3-rc.0

published a week ago by jacoblee93 jacoblee93@gmail.com

dosubot[bot] commented 3 months ago

The TypeError: model.bindTools is not a function issue occurs because the ChatOpenAI class you are using does not implement the bindTools method. This method is required for binding tools to the model.

To resolve this issue, you can use a model that supports the bindTools method, such as ChatOpenAI from the @langchain/openai package. Here is an example of how to use it:

import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";

const model = new ChatOpenAI({
  temperature: 0,
  model: "gpt-4o",
});

// Define your tools
const calculatorSchema = z
  .object({
    operation: z.enum(["add", "subtract", "multiply", "divide"]),
    number1: z.number(),
    number2: z.number(),
  })
  .describe("A tool to perform basic arithmetic operations");
const weatherSchema = z
  .object({
    city: z.string(),
  })
  .describe("A tool to get the weather in a city");

// Bind tools to the model
const modelWithTools = model.bindTools([
  {
    type: "function",
    function: {
      name: "calculator",
      description: calculatorSchema.description,
      parameters: zodToJsonSchema(calculatorSchema),
    },
  },
  {
    type: "function",
    function: {
      name: "weather",
      description: weatherSchema.description,
      parameters: zodToJsonSchema(weatherSchema),
    },
  },
]);

// Invoke the model with `parallel_tool_calls` set to `true`
const response = await modelWithTools.invoke(
  ["What is the weather in san francisco and what is 23716 times 27342?"],
  {
    parallel_tool_calls: true,
  }
);
console.log(response.tool_calls);

Ensure that you are using the correct version of the @langchain/openai package that includes the bindTools method [1][2].

To continue talking to Dosu, mention @dosu.