一键导入
langchain-chat-models
Guide to using chat model integrations in LangChain including OpenAI, Anthropic, Google, Azure, and Bedrock
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guide to using chat model integrations in LangChain including OpenAI, Anthropic, Google, Azure, and Bedrock
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | langchain-chat-models |
| description | Guide to using chat model integrations in LangChain including OpenAI, Anthropic, Google, Azure, and Bedrock |
| language | js |
Chat models in LangChain provide a unified interface for interacting with various LLM providers. They take a sequence of messages as input and return AI-generated messages as output. Chat models support features like tool calling, structured output, and streaming.
| Provider | Best For | Model Examples | Package | Key Features |
|---|---|---|---|---|
| OpenAI | General purpose, function calling | gpt-4, gpt-4-turbo, gpt-3.5-turbo | @langchain/openai | Strong function calling, vision, fast |
| Anthropic | Long context, safety, analysis | claude-3-opus, claude-3-sonnet, claude-3-haiku | @langchain/anthropic | 200k context, tool use, prompt caching |
| Google GenAI | Multimodal, free tier | gemini-pro, gemini-pro-vision | @langchain/google-genai | Vision, free tier available |
| Azure OpenAI | Enterprise, compliance | gpt-4, gpt-35-turbo (Azure deployed) | @langchain/openai | Enterprise SLAs, data residency |
| AWS Bedrock | AWS ecosystem, variety | claude, llama, titan models | @langchain/aws | Multiple models, AWS integration |
| Google Vertex AI | GCP ecosystem, enterprise | gemini-pro, palm models | @langchain/google-vertexai | Enterprise features, GCP integration |
Choose OpenAI if:
Choose Anthropic if:
Choose Azure OpenAI if:
Choose AWS Bedrock if:
Choose Google (GenAI or Vertex) if:
import { ChatOpenAI } from "@langchain/openai";
// Basic initialization
const model = new ChatOpenAI({
modelName: "gpt-4",
temperature: 0.7,
openAIApiKey: process.env.OPENAI_API_KEY, // Optional if set in env
});
// Invoke the model
const response = await model.invoke([
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is LangChain?" }
]);
console.log(response.content);
// Streaming responses
const stream = await model.stream("Tell me a story");
for await (const chunk of stream) {
process.stdout.write(chunk.content);
}
import { ChatAnthropic } from "@langchain/anthropic";
const model = new ChatAnthropic({
modelName: "claude-3-opus-20240229",
temperature: 0.7,
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
maxTokens: 1024,
});
// Long context usage
const response = await model.invoke([
{ role: "user", content: "Analyze this long document..." }
]);
// With tool use
const modelWithTools = model.bindTools([
{
name: "get_weather",
description: "Get weather for a location",
input_schema: {
type: "object",
properties: {
location: { type: "string" }
},
required: ["location"]
}
}
]);
import { AzureChatOpenAI } from "@langchain/openai";
const model = new AzureChatOpenAI({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
azureOpenAIApiVersion: "2024-02-01",
temperature: 0.7,
});
const response = await model.invoke("Hello, how are you?");
import { ChatBedrockConverse } from "@langchain/aws";
const model = new ChatBedrockConverse({
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: "us-east-1",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
const response = await model.invoke("What is AWS Bedrock?");
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const model = new ChatGoogleGenerativeAI({
modelName: "gemini-pro",
apiKey: process.env.GOOGLE_API_KEY,
temperature: 0.7,
});
const response = await model.invoke("Explain quantum computing");
import { initChatModel } from "langchain/chat_models/universal";
// Automatically select model based on environment
const model = await initChatModel("gpt-4", {
modelProvider: "openai",
temperature: 0.7,
});
// Or with Bedrock
const bedrockModel = await initChatModel(
"anthropic.claude-3-sonnet-20240229-v1:0",
{ modelProvider: "bedrock" }
);
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
import { tool } from "@langchain/core/tools";
// Define a tool
const weatherTool = tool(
async ({ location }) => {
return `The weather in ${location} is sunny and 72°F`;
},
{
name: "get_weather",
description: "Get the current weather for a location",
schema: z.object({
location: z.string().describe("The city name"),
}),
}
);
// Bind tools to model
const model = new ChatOpenAI({
modelName: "gpt-4",
}).bindTools([weatherTool]);
const response = await model.invoke("What's the weather in San Francisco?");
console.log(response.tool_calls); // Model will suggest calling the weather tool
✅ Initialize any supported chat model provider
@langchain/openai, @langchain/anthropic, etc.)✅ Configure model parameters
✅ Use models for text generation
✅ Implement tool/function calling
✅ Switch between providers
❌ Create new model providers
❌ Bypass provider requirements
❌ Modify model capabilities
❌ Access models without proper setup
// ❌ BAD: Hardcoding API keys
const model = new ChatOpenAI({
openAIApiKey: "sk-..." // Never commit this!
});
// ✅ GOOD: Use environment variables
const model = new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY
});
Fix: Always use environment variables or secure key management systems.
// ❌ INCOMPLETE: Missing required fields
const model = new AzureChatOpenAI({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
});
// ✅ COMPLETE: All required fields
const model = new AzureChatOpenAI({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiInstanceName: "my-instance",
azureOpenAIApiDeploymentName: "gpt-4-deployment",
azureOpenAIApiVersion: "2024-02-01",
});
Fix: Azure requires instance name, deployment name, and API version.
// Different providers have different model naming conventions
// OpenAI
const openai = new ChatOpenAI({ modelName: "gpt-4" });
// Bedrock (includes provider prefix)
const bedrock = new ChatBedrockConverse({
model: "anthropic.claude-3-sonnet-20240229-v1:0"
});
// Anthropic (direct model name)
const anthropic = new ChatAnthropic({
modelName: "claude-3-opus-20240229"
});
Fix: Check provider documentation for correct model identifiers.
// ❌ Not all models support tool calling
const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo-instruct" });
// This older model doesn't support tools!
// ✅ Use models with tool support
const model = new ChatOpenAI({ modelName: "gpt-4" });
const withTools = model.bindTools([myTool]);
Fix: Verify model supports function/tool calling before binding tools. GPT-4, GPT-3.5-turbo, Claude 3, and Gemini Pro all support tools.
// ❌ No retry logic
const model = new ChatOpenAI();
const response = await model.invoke("Hello"); // May fail on rate limit
// ✅ Configure retries
const model = new ChatOpenAI({
maxRetries: 3,
timeout: 30000, // 30 seconds
});
Fix: Configure retry logic and handle rate limit errors gracefully.
// ❌ Exceeding context limits
const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo" }); // 4k context
const longText = "...".repeat(10000);
await model.invoke(longText); // Will fail!
// ✅ Use appropriate models for long context
const model = new ChatOpenAI({ modelName: "gpt-4-turbo" }); // 128k context
// OR
const model = new ChatAnthropic({
modelName: "claude-3-opus-20240229" // 200k context
});
Fix: Choose models with appropriate context windows for your use case.
// ❌ Mixing streaming and non-streaming incorrectly
const response = await model.stream("Hello");
console.log(response.content); // Won't work! response is an async iterable
// ✅ Handle streaming properly
const stream = await model.stream("Hello");
for await (const chunk of stream) {
console.log(chunk.content);
}
// OR use invoke for non-streaming
const response = await model.invoke("Hello");
console.log(response.content);
Fix: Use invoke() for complete responses, stream() for token-by-token.
# OpenAI
npm install @langchain/openai
# Anthropic
npm install @langchain/anthropic
# AWS (Bedrock)
npm install @langchain/aws
# Google
npm install @langchain/google-genai
npm install @langchain/google-vertexai
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Understanding Deep Agents framework - what they are, how to create them with createDeepAgent, and the agent harness architecture with built-in middleware for planning, filesystems, and subagents.
Creating and using custom skills with progressive disclosure, SKILL.md format, and the Agent Skills protocol in Deep Agents.
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.