LangChain v1.0 现已发布!有关更改的完整列表和如何升级代码的说明,请参阅发布说明迁移指南如果您遇到任何问题或有反馈,请提交问题,以便我们改进。要查看 v0.x 文档,请访问存档内容
LangChain 是开始构建由 LLM 驱动的智能体和应用程序的最简单方法。只需不到 10 行代码,您就可以连接到 OpenAI、Anthropic、Google 和更多。LangChain 提供预构建的智能体架构和模型集成,帮助您快速入门并无缝地将 LLM 集成到您的智能体和应用程序中。 如果您想快速构建智能体和自主应用程序,我们建议您使用 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: "claude-sonnet-4-5-20250929",
  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.