主题
聊天模型 是一种语言模型,它以一系列消息作为输入,并返回消息作为输出 (与纯文本相对)。
安装与使用
关于安装 LangChain 包的通用说明,请参阅此章节。
OpenAI
安装:
bash
npm i @langchain/openaibash
yarn add @langchain/openaibash
pnpm add @langchain/openai添加环境变量:
bash
OPENAI_API_KEY=your-api-key实例化模型:
typescript
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4o-mini" });javascript
await model.invoke("Hello, world!")Anthropic
安装:
bash
npm i @langchain/anthropicbash
yarn add @langchain/anthropicbash
pnpm add @langchain/anthropic添加环境变量:
bash
ANTHROPIC_API_KEY=your-api-key实例化模型:
typescript
import { ChatAnthropic } from "@langchain/anthropic";
const model = new ChatAnthropic({
model: "claude-3-sonnet-20240620",
temperature: 0
});javascript
await model.invoke("Hello, world!")Google Gemini
安装:
bash
npm i @langchain/google-genaibash
yarn add @langchain/google-genaibash
pnpm add @langchain/google-genai添加环境变量:
bash
GOOGLE_API_KEY=your-api-key实例化模型:
typescript
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const model = new ChatGoogleGenerativeAI({
modelName: "gemini-2.5-flash-lite",
temperature: 0
});javascript
await model.invoke("Hello, world!")Google VertexAI
安装:
bash
npm i @langchain/google-vertexaibash
yarn add @langchain/google-vertexaibash
pnpm add @langchain/google-vertexai添加环境变量:
bash
GOOGLE_APPLICATION_CREDENTIALS=credentials.json实例化模型:
typescript
import { ChatVertexAI } from "@langchain/google-vertexai";
const model = new ChatVertexAI({
model: "gemini-2.5-flash",
temperature: 0
});javascript
await model.invoke("Hello, world!")MistralAI
安装:
bash
npm i @langchain/mistralaibash
yarn add @langchain/mistralaibash
pnpm add @langchain/mistralai添加环境变量:
bash
MISTRAL_API_KEY=your-api-key实例化模型:
typescript
import { ChatMistralAI } from "@langchain/mistralai";
const model = new ChatMistralAI({
model: "mistral-large-latest",
temperature: 0
});javascript
await model.invoke("Hello, world!")FireworksAI
安装:
bash
npm i @langchain/communitybash
yarn add @langchain/communitybash
pnpm add @langchain/community添加环境变量:
bash
FIREWORKS_API_KEY=your-api-key实例化模型:
typescript
import { ChatFireworks } from "@langchain/community/chat_models/fireworks";
const model = new ChatFireworks({
model: "accounts/fireworks/models/llama-v3p1-70b-instruct",
temperature: 0
});javascript
await model.invoke("Hello, world!")Groq
安装:
bash
npm i @langchain/groqbash
yarn add @langchain/groqbash
pnpm add @langchain/groq添加环境变量:
bash
GROQ_API_KEY=your-api-key实例化模型:
typescript
import { ChatGroq } from "@langchain/groq";
const model = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0
});javascript
await model.invoke("Hello, world!")特色模型
虽然这些 LangChain 类支持所指示的高级功能,但你可能需要查阅特定提供商的文档,以了解哪些托管模型或后端支持该功能。
| 模型 | 流式传输 | 工具调用 | withStructuredOutput() | 多模态 |
|---|---|---|---|---|
ChatOpenAI | ✅ | ✅ | ✅ | ✅ |
ChatAnthropic | ✅ | ✅ | ✅ | ✅ |
ChatGoogleGenerativeAI | ✅ | ✅ | ✅ | ✅ |
ChatVertexAI | ✅ | ✅ | ✅ | ✅ |
BedrockChat | ✅ | 🟡 (仅限 Bedrock Anthropic) | 🟡 (仅限 Bedrock Anthropic) | 🟡 (仅限 Bedrock Anthropic) |
ChatBedrockConverse | ✅ | ✅ | ✅ | ✅ |
ChatCloudflareWorkersAI | ✅ | ❌ | ❌ | ❌ |
ChatCohere | ✅ | ✅ | ✅ | ✅ |
ChatFireworks | ✅ | ✅ | ✅ | ✅ |
ChatGroq | ✅ | ✅ | ✅ | ✅ |
ChatMistralAI | ✅ | ✅ | ✅ | ✅ |
ChatOllama | ✅ | ✅ | ✅ | ✅ |
ChatTogetherAI | ✅ | ✅ | ✅ | ✅ |
ChatXAI | ✅ | ✅ | ✅ | ❌ |
聊天补全 API
某些模型提供商提供与 OpenAI(旧版)聊天补全 API 兼容的端点。在这种情况下,你可以使用带有自定义 base_url 的 ChatOpenAI 来连接到这些端点。请注意,基于聊天补全 API 构建的功能可能不被 ChatOpenAI 完全支持;在这种情况下,请考虑使用特定于提供商的类(如果可用)。
示例:OpenRouter
要使用 OpenRouter,你需要注册一个账户并获取一个 API 密钥。
typescript
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "...", // 指定 OpenRouter 上可用的模型
configuration: {
apiKey: "OPENROUTER_API_KEY",
baseURL: "https://openrouter.ai/api/v1",
}
});更多详情请参阅 OpenRouter 文档。
所有聊天模型
Alibaba Tongyi
查看指南 →
Anthropic
查看指南 →
Arcjet Redact
查看指南 →
Azure OpenAI
查看指南 →
Baidu Qianfan
查看指南 →
Amazon Bedrock
查看指南 →
Amazon Bedrock Converse
查看指南 →
Cerebras
查看指南 →
Cloudflare Workers AI
查看指南 →
Cohere
查看指南 →
Deep Infra
查看指南 →
DeepSeek
查看指南 →
Fake LLM
查看指南 →
Fireworks
查看指南 →
Friendli
查看指南 →
Google GenAI
查看指南 →
Google Vertex AI
查看指南 →
Groq
查看指南 →
IBM watsonx.ai
查看指南 →
Llama CPP
查看指南 →
Minimax
查看指南 →
MistralAI
查看指南 →
Moonshot
查看指南 →
Novita AI
查看指南 →
Ollama
查看指南 →
OpenAI
查看指南 →
Perplexity
查看指南 →
PremAI
查看指南 →
Tencent Hunyuan
查看指南 →
Together
查看指南 →
WebLLM
查看指南 →
xAI
查看指南 →
YandexGPT
查看指南 →
ZhipuAI
查看指南 →
如果你想贡献一个集成,请参阅贡献集成。