Skip to main content
LangChain v1.0 现已发布!有关变更的完整列表以及升级代码的说明,请参阅发布说明迁移指南如果您遇到任何问题或有反馈,请提交问题以便我们改进。要查看 v0.x 文档,请访问归档网站
LangChain 是开始构建由大语言模型驱动的智能体和应用程序的最简单方式。通过不到 10 行代码,您就可以连接到 OpenAI、Anthropic、Google 以及更多模型。LangChain 提供了预构建的智能体架构和模型集成,帮助您快速入门并无缝地将大语言模型融入您的智能体和应用程序中。 如果您希望快速构建智能体和自主应用程序,我们推荐您使用 LangChain。当您有更高级的需求,需要结合确定性和智能工作流、深度定制以及精细控制的延迟时,请使用我们的底层智能体编排框架和运行时 LangGraph LangChain 智能体构建在 LangGraph 之上,以提供持久化执行、流式传输、人在回路、持久化等特性。对于基本的 LangChain 智能体使用,您不需要了解 LangGraph。

安装

npm install langchain @langchain/core

创建一个智能体

import * as z from "zod";
// npm install @langchain/anthropic to call the model
import { createAgent, tool } from "langchain";

const getWeather = tool(
  ({ city }) => `It's always sunny in ${city}!`,
  {
    name: "get_weather",
    description: "Get the weather for a given city",
    schema: z.object({
      city: z.string(),
    }),
  },
);

const agent = createAgent({
  model: "anthropic:claude-sonnet-4-5",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
  })
);

核心优势


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.