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.4", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in San Francisco?" }], }));
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-genai:gemini-2.5-flash-lite", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in San Francisco?" }], }));
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?" }], }));
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?" }], }));
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?" }], }));
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", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in San Francisco?" }], }));
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?" }], }));
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.4", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in San Francisco?" }], }));
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.4", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in San Francisco?" }], }));
const SYSTEM_PROMPT = `You are a literary data assistant.## Capabilities- \`fetch_text_from_url\`: loads document text from a URL into the conversation.Do not guess line counts or positions—ground them in tool results from the saved file.`;
现在,使用所有组件组装你的智能体并运行它。创建智能体有两种不同的框架:LangChain 智能体和 deep agents。
LangChain 和 deep agents 都为你提供了对工具、记忆等的细粒度控制。
两者的主要区别在于 deep agents 内置了一系列常用功能,例如规划、文件系统工具和子智能体。当你希望以最少的设置获得最大功能时,请使用 deep agents;当你需要细粒度控制时,请选择 LangChain 智能体。
async function main() { const agent = createAgent({ model, tools: [fetchTextFromUrl], systemPrompt: SYSTEM_PROMPT, checkpointer, }); const deepAgent = createDeepAgent({ model, tools: [fetchTextFromUrl], systemPrompt: SYSTEM_PROMPT, checkpointer, }); const content = `Project Gutenberg hosts a full plain-text copy of F. Scott Fitzgerald's The Great Gatsby. URL: https://www.gutenberg.org/files/64317/64317-0.txt Answer as much as you can: 1) How many lines in the complete Gutenberg file contain the substring \`Gatsby\` (count lines, not occurrences within a line, each line ends with a line break). 2) The 1-based line number of the first line in the file that contains \`Daisy\`. 3) A two-sentence neutral synopsis. Do your best on (1) and (2). If at any point you realize you cannot **verify** an exact answer with your available tools and reasoning, do not fabricate numbers: use \`null\` for that field and spell out the limitation in \`how_you_computed_counts\`. If you encounter any errors please report what the error was and what the error message was.`; const agentResult = await agent.invoke( { messages: [{ role: "user", content }] }, { configurable: { thread_id: "great-gatsby-lc" } }, ); const deepAgentResult = await deepAgent.invoke( { messages: [{ role: "user", content }] }, { configurable: { thread_id: "great-gatsby-da" } }, ); const agentMessages = agentResult.messages; const deepMessages = deepAgentResult.messages; console.log(agentMessages[agentMessages.length - 1]!.content_blocks); console.log("\n"); console.log(deepMessages[deepMessages.length - 1]!.content_blocks);}main().catch((err) => { console.error(err); process.exitCode = 1;});
Show 完整示例代码
import { MemorySaver } from "@langchain/langgraph";import { createDeepAgent } from "deepagents";import { tool } from "@langchain/core/tools";import { createAgent, initChatModel } from "langchain";import { z } from "zod";const SYSTEM_PROMPT = `You are a literary data assistant.## Capabilities- \`fetch_text_from_url\`: loads document text from a URL into the conversation.Do not guess line counts or positions—ground them in tool results from the saved file.`;const fetchTextFromUrl = tool( async ({ url }: { url: string }): Promise<string> => { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 120_000); try { const resp = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 (compatible; quickstart-research/1.0)", }, signal: controller.signal, }); if (!resp.ok) { return `Fetch failed: HTTP ${resp.status} ${resp.statusText}`; } return await resp.text(); } catch (e) { const msg = e instanceof Error ? e.message : String(e); return `Fetch failed: ${msg}`; } finally { clearTimeout(timeoutId); } }, { name: "fetch_text_from_url", description: "Fetch the document from a URL.", schema: z.object({ url: z.string().url() }), },);const model = await initChatModel("gemini-3.1-pro-preview", { modelProvider: "google-genai", temperature: 0.5, timeout: 600_000, maxTokens: 25000, streaming: true,});const checkpointer = new MemorySaver();async function main() { const agent = createAgent({ model, tools: [fetchTextFromUrl], systemPrompt: SYSTEM_PROMPT, checkpointer, }); const deepAgent = createDeepAgent({ model, tools: [fetchTextFromUrl], systemPrompt: SYSTEM_PROMPT, checkpointer, }); const content = `Project Gutenberg hosts a full plain-text copy of F. Scott Fitzgerald's The Great Gatsby. URL: https://www.gutenberg.org/files/64317/64317-0.txt Answer as much as you can: 1) How many lines in the complete Gutenberg file contain the substring \`Gatsby\` (count lines, not occurrences within a line, each line ends with a line break). 2) The 1-based line number of the first line in the file that contains \`Daisy\`. 3) A two-sentence neutral synopsis. Do your best on (1) and (2). If at any point you realize you cannot **verify** an exact answer with your available tools and reasoning, do not fabricate numbers: use \`null\` for that field and spell out the limitation in \`how_you_computed_counts\`. If you encounter any errors please report what the error was and what the error message was.`; const agentResult = await agent.invoke( { messages: [{ role: "user", content }] }, { configurable: { thread_id: "great-gatsby-lc" } }, ); const deepAgentResult = await deepAgent.invoke( { messages: [{ role: "user", content }] }, { configurable: { thread_id: "great-gatsby-da" } }, ); const agentMessages = agentResult.messages; const deepMessages = deepAgentResult.messages; console.log(agentMessages[agentMessages.length - 1]!.content_blocks); console.log("\n"); console.log(deepMessages[deepMessages.length - 1]!.content_blocks);}main().catch((err) => { console.error(err); process.exitCode = 1;});