聊天模型 是使用一系列 消息 作为输入并返回消息作为输出 (与纯文本相反).
安装和使用
OpenAI
Install:
npm install @langchain/openai @langchain/core
yarn add @langchain/openai @langchain/core
pnpm add @langchain/openai @langchain/core
添加环境变量:
OPENAI_API_KEY=your-api-key
实例化模型:
const model = new ChatOpenAI({ model: "gpt-5.4-mini" });
await model.invoke("Hello, world!")
Anthropic
Install:
npm i @langchain/anthropic @langchain/core
yarn add @langchain/anthropic @langchain/core
pnpm add @langchain/anthropic @langchain/core
添加环境变量:
ANTHROPIC_API_KEY=your-api-key
实例化模型:
const model = new ChatAnthropic({
model: "claude-3-sonnet-20240620",
temperature: 0
});
await model.invoke("Hello, world!")
Google Gemini
Install:
npm install @langchain/google @langchain/core
yarn add @langchain/google @langchain/core
pnpm add @langchain/google @langchain/core
添加环境变量:
GOOGLE_API_KEY=your-api-key
实例化模型:
const model = new ChatGoogle("gemini-2.5-flash");
await model.invoke("Hello, world!")
MistralAI
Install:
npm install @langchain/mistralai @langchain/core
yarn add @langchain/mistralai @langchain/core
pnpm add @langchain/mistralai @langchain/core
添加环境变量:
MISTRAL_API_KEY=your-api-key
实例化模型:
const model = new ChatMistralAI({
model: "mistral-large-latest",
temperature: 0
});
await model.invoke("Hello, world!")
Groq
Install:
npm install @langchain/groq @langchain/core
yarn add @langchain/groq @langchain/core
pnpm add @langchain/groq @langchain/core
添加环境变量:
GROQ_API_KEY=your-api-key
实例化模型:
const model = new ChatGroq({
model: "openai/gpt-oss-120b",
temperature: 0
});
await model.invoke("Hello, world!")
精选模型
| 模型 | 流式 | 工具调用 | withStructuredOutput() | Multimodal | |-|-|-|-|-| | ChatOpenAI | ✅ | ✅ | ✅ | ✅ | | ChatAnthropic | ✅ | ✅ | ✅ | ✅ | | ChatGoogle | ✅ | ✅ | ✅ | ✅ | | ChatBedrockConverse | ✅ | ✅ | ✅ | ✅ | | ChatCloudflareWorkersAI | ✅ | ❌ | ❌ | ❌ | | ChatCohere | ✅ | ✅ | ✅ | ✅ | | ChatFireworks | ✅ | ✅ | ✅ | ✅ | | ChatGroq | ✅ | ✅ | ✅ | ✅ | | ChatMistralAI | ✅ | ✅ | ✅ | ✅ | | ChatOllama | ✅ | ✅ | ✅ | ✅ | | ChatPerplexity | ✅ | ❌ | ✅ | ❌ | | ChatTogetherAI | ✅ | ✅ | ✅ | ✅ | | ChatXAI | ✅ | ✅ | ✅ | ❌ |
请参阅 聊天模型集成的完整列表 更多选项,请参阅下文。
路由器和代理
路由器和代理让您通过单一 API 和凭证访问来自多个提供商的模型。它们可以简化计费,让您在不更改集成的情况下切换模型,并提供自动故障转移等功能。
| 提供商 | 集成 | 描述 | |-|-|-| | OpenRouter | ChatOpenRouter | 通过统一接口访问 OpenAI、Anthropic、Google、Meta 等模型 |
聊天补全 API
某些模型提供商提供与 OpenAI(遗留版本)的 聊天补全 API兼容的端点。在这种情况下,您可以使用 ChatOpenAI 和自定义 base_url 连接到这些端点。请注意,基于聊天补全 API 构建的功能可能无法被 ChatOpenAI完全支持;在这种情况下,请考虑使用提供商特定的类(如果有)。