Skip to content

LangChain 概览 ​

LangChain 提供了 create_agent:一个极简且高度可配置的智能体运行框架(harness)。通过模型、工具、提示词和中间件,组合出完全契合你用例需求的智能体。

Agent = Model + Harness(智能体 = 模型 + 运行框架)。 LangChain 提供了 create_agent:一个极简且高度可配置的运行框架(harness)。运行框架涵盖了围绕模型循环的一切:提示词、工具以及任何塑造行为的中间件。从这些基础原语出发,组合出完全契合你用例需求的内容。支持 OpenAI、Anthropic、Google 等更多提供商。

TIP

LangChain vs. LangGraph vs. Deep Agents

从 Deep Agents 开始,体验“开箱即用”(batteries-included)的智能体,具备自动上下文压缩、虚拟文件系统以及子智能体衍生等功能。Deep Agents 构建于 LangChain 智能体之上,你也可以直接使用它们。

使用 LangChain(create_agent)获得高度可定制的运行框架,可轻松根据你的用例和数据进行量身定制。

使用 LangGraph(我们的底层编排框架)来满足结合确定性工作流与智能体工作流的高级需求。

使用 LangSmith 对基于上述任何框架构建的智能体进行链路追踪、调试和评估。请参考链路追踪快速入门来进行配置。我们还建议你配置 LangSmith Engine,它可以监控你的追踪记录、检测问题并提出修复建议。

创建智能体 ​

此示例演示如何使用自定义工具创建一个简单的 LangChain 智能体:

ts
// 首先安装:npm install langchain zod @langchain/openai
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "gpt-5.5",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/google
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "google:gemini-2.5-flash-lite",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/anthropic
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

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

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/openrouter
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

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

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "baseten:zai-org/GLM-5.2",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/ollama
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "ollama:devstral-2",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/openai
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "azure_openai:gpt-5.5",
  tools: [getWeather],
});

console.log(
  await agent.invoke({
    messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  })
);
ts
// 首先安装:npm install langchain zod @langchain/aws
import { createAgent, tool } from "langchain";
import * as z from "zod";

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

const agent = createAgent({
  model: "bedrock:gpt-5.5",
  tools: [getWeather],
});

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

请参阅安装说明和快速入门指南,开始使用 LangChain 构建你自己的智能体和应用程序。

TIP

使用 LangSmith 追踪请求、调试智能体行为并评估输出。设置 LANGSMITH_TRACING=true 以及你的 API Key 即可开始使用。

核心优势 ​

标准模型接口

跨不同提供商使用统一的接口来调用聊天模型、嵌入(embeddings)等能力。只需修改极少代码即可切换模型,并在需求演进过程中保持应用程序的可移植性。

高度可配置的运行框架

从作为极简运行框架(harness)的 create_agent 开始,通过中间件逐步增量添加能力。从安全护栏(guardrails)与重试机制,到路由和自定义工具策略,仅组合你用例所需的功能。

构建于 LangGraph 之上

LangChain 的智能体构建在 LangGraph 之上。这使得我们能够充分利用 LangGraph 的持久化执行、人机交互(human-in-the-loop)支持、持久化存储等特性。

使用 LangSmith 调试

在一个统一视图中检查链路追踪、工具调用、状态流转和延迟。发现故障模式,评估输出质量,并借助执行数据不断改进智能体行为。