Skip to main content

如何用一行代码初始化任意模型

许多大型语言模型(LLM)应用允许最终用户指定他们希望应用使用哪个模型提供商和模型。 这需要编写一些逻辑,根据用户的配置来初始化不同的聊天模型。 initChatModel() 辅助方法使得无需担心导入路径和类名,即可轻松初始化多种不同的模型集成。 请注意,此功能仅适用于聊天模型。

前提条件

本指南假定您熟悉以下概念:

兼容性

此功能仅适用于 Node 环境。在非 Node 环境中或与打包工具一起使用时,功能可能无法正常工作,且不在官方支持范围内。

initChatModel 要求 langchain>=0.2.11。有关升级时的注意事项,请参见本指南

支持的集成列表,请参见 initChatModel() API 文档。

请确保安装了您希望支持的模型提供商的集成包。例如,如果您要初始化 OpenAI 模型,则应安装 @langchain/openai

基本用法

import { initChatModel } from "langchain/chat_models/universal";

// Returns a @langchain/openai ChatOpenAI instance.
const gpt4o = await initChatModel("gpt-4o", {
modelProvider: "openai",
temperature: 0,
});

// You can also specify the model provider in the model name like this in
// langchain>=0.3.18:

// Returns a @langchain/anthropic ChatAnthropic instance.
const claudeOpus = await initChatModel("anthropic:claude-3-opus-20240229", {
temperature: 0,
});
// Returns a @langchain/google-vertexai ChatVertexAI instance.
const gemini15 = await initChatModel("google-vertexai:gemini-1.5-pro", {
temperature: 0,
});

// Since all model integrations implement the ChatModel interface, you can use them in the same way.
console.log(`GPT-4o: ${(await gpt4o.invoke("what's your name")).content}\n`);
console.log(
`Claude Opus: ${(await claudeOpus.invoke("what's your name")).content}\n`
);
console.log(
`Gemini 1.5: ${(await gemini15.invoke("what's your name")).content}\n`
);

/*
GPT-4o: I'm an AI language model created by OpenAI, and I don't have a personal name. You can call me Assistant or any other name you prefer! How can I help you today?

Claude Opus: My name is Claude. It's nice to meet you!

Gemini 1.5: I don't have a name. I am a large language model, and I am not a person. I am a computer program that can generate text, translate languages, write different kinds of creative content, and answer your questions in an informative way.
*/

API Reference:

推断模型提供商

对于常见且具有辨识度的模型名称,initChatModel() 会尝试推断模型提供商。 完整的推断行为列表,请参见API 文档。 例如,任何以 gpt-3...gpt-4... 开头的模型都会被推断为使用模型提供商 openai

import { initChatModel } from "langchain/chat_models/universal";

const gpt4o = await initChatModel("gpt-4o", {
temperature: 0,
});
const claudeOpus = await initChatModel("claude-3-opus-20240229", {
temperature: 0,
});
const gemini15 = await initChatModel("gemini-1.5-pro", {
temperature: 0,
});

API Reference:

创建可配置模型

您还可以通过指定 configurableFields 来创建运行时可配置的模型。 如果您未指定 model 值,则 "model" 和 "modelProvider" 将默认为可配置项。

import { initChatModel } from "langchain/chat_models/universal";

const configurableModel = await initChatModel(undefined, { temperature: 0 });

const gpt4Res = await configurableModel.invoke("what's your name", {
configurable: { model: "gpt-4o-mini" },
});
console.log("gpt4Res: ", gpt4Res.content);
/*
gpt4Res: I'm an AI language model created by OpenAI, and I don't have a personal name. You can call me Assistant or any other name you prefer! How can I assist you today?
*/

const claudeRes = await configurableModel.invoke("what's your name", {
configurable: { model: "claude-3-5-sonnet-20240620" },
});
console.log("claudeRes: ", claudeRes.content);
/*
claudeRes: My name is Claude. It's nice to meet you!
*/

API Reference:

带默认值的可配置模型

我们可以创建带有默认模型值的可配置模型,指定哪些参数是可配置的,并为可配置参数添加前缀:

import { initChatModel } from "langchain/chat_models/universal";

const firstLlm = await initChatModel("gpt-4o", {
temperature: 0,
configurableFields: ["model", "modelProvider", "temperature", "maxTokens"],
configPrefix: "first", // useful when you have a chain with multiple models
});

const openaiRes = await firstLlm.invoke("what's your name");
console.log("openaiRes: ", openaiRes.content);
/*
openaiRes: I'm an AI language model created by OpenAI, and I don't have a personal name. You can call me Assistant or any other name you prefer! How can I assist you today?
*/

const claudeRes = await firstLlm.invoke("what's your name", {
configurable: {
first_model: "claude-3-5-sonnet-20240620",
first_temperature: 0.5,
first_maxTokens: 100,
},
});
console.log("claudeRes: ", claudeRes.content);
/*
claudeRes: My name is Claude. It's nice to meet you!
*/

API Reference:

以声明方式使用可配置模型

我们可以在可配置模型上调用如 bindToolswithStructuredOutputwithConfig 等声明式操作,并以与常规实例化的聊天模型对象相同的方式对可配置模型进行链式调用。

import { z } from "zod";
import { tool } from "@langchain/core/tools";
import { initChatModel } from "langchain/chat_models/universal";

const GetWeather = z
.object({
location: z.string().describe("The city and state, e.g. San Francisco, CA"),
})
.describe("Get the current weather in a given location");
const weatherTool = tool(
(_) => {
// do something
return "138 degrees";
},
{
name: "GetWeather",
schema: GetWeather,
}
);

const GetPopulation = z
.object({
location: z.string().describe("The city and state, e.g. San Francisco, CA"),
})
.describe("Get the current population in a given location");
const populationTool = tool(
(_) => {
// do something
return "one hundred billion";
},
{
name: "GetPopulation",
schema: GetPopulation,
}
);

const llm = await initChatModel(undefined, { temperature: 0 });
const llmWithTools = llm.bindTools([weatherTool, populationTool]);

const toolCalls1 = (
await llmWithTools.invoke("what's bigger in 2024 LA or NYC", {
configurable: { model: "gpt-4o-mini" },
})
).tool_calls;
console.log("toolCalls1: ", JSON.stringify(toolCalls1, null, 2));
/*
toolCalls1: [
{
"name": "GetPopulation",
"args": {
"location": "Los Angeles, CA"
},
"type": "tool_call",
"id": "call_DXRBVE4xfLYZfhZOsW1qRbr5"
},
{
"name": "GetPopulation",
"args": {
"location": "New York, NY"
},
"type": "tool_call",
"id": "call_6ec3m4eWhwGz97sCbNt7kOvC"
}
]
*/

const toolCalls2 = (
await llmWithTools.invoke("what's bigger in 2024 LA or NYC", {
configurable: { model: "claude-3-5-sonnet-20240620" },
})
).tool_calls;
console.log("toolCalls2: ", JSON.stringify(toolCalls2, null, 2));
/*
toolCalls2: [
{
"name": "GetPopulation",
"args": {
"location": "Los Angeles, CA"
},
"id": "toolu_01K3jNU8jx18sJ9Y6Q9SooJ7",
"type": "tool_call"
},
{
"name": "GetPopulation",
"args": {
"location": "New York City, NY"
},
"id": "toolu_01UiANKaSwYykuF4hi3t5oNB",
"type": "tool_call"
}
]
*/

API Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.