许多 AI 应用程序通过自然语言与用户交互。然而,某些用例需要模型使用结构化输入直接与外部系统(如 API、数据库或文件系统)进行接口交互。工具是智能体调用以执行操作的组件。它们通过让模型通过明确定义的输入和输出与世界交互来扩展模型能力。工具封装了一个可调用的函数及其输入模式。这些可以传递给兼容的聊天模型,允许模型决定是否调用工具以及使用什么参数。在这些场景中,工具调用使模型能够生成符合指定输入模式的请求。
import * as z from "zod"import { tool } from "langchain"const searchDatabase = tool( ({ query, limit }) => `Found ${limit} results for '${query}'`, { name: "search_database", description: "Search the customer database for records matching the query.", schema: z.object({ query: z.string().describe("Search terms to look for"), limit: z.number().describe("Maximum number of results to return"), }), });
Why this matters: Tools are most powerful when they can access agent state, runtime context, and long-term memory. This enables tools to make context-aware decisions, personalize responses, and maintain information across conversations.
Access persistent data across conversations using the store. The store is accessed via config.store and allows you to save and retrieve user-specific or application-specific data.
Copy
import * as z from "zod";import { createAgent, tool } from "langchain";import { InMemoryStore } from "@langchain/langgraph";import { ChatOpenAI } from "@langchain/openai";const store = new InMemoryStore();// Access memoryconst getUserInfo = tool( async ({ user_id }) => { const value = await store.get(["users"], user_id); console.log("get_user_info", user_id, value); return value; }, { name: "get_user_info", description: "Look up user info.", schema: z.object({ user_id: z.string(), }), });// Update memoryconst saveUserInfo = tool( async ({ user_id, name, age, email }) => { console.log("save_user_info", user_id, name, age, email); await store.put(["users"], user_id, { name, age, email }); return "Successfully saved user info."; }, { name: "save_user_info", description: "Save user info.", schema: z.object({ user_id: z.string(), name: z.string(), age: z.number(), email: z.string(), }), });const agent = createAgent({ model: new ChatOpenAI({ model: "gpt-4o" }), tools: [getUserInfo, saveUserInfo], store,});// First session: save user infoawait agent.invoke({ messages: [ { role: "user", content: "Save the following user: userid: abc123, name: Foo, age: 25, email: foo@langchain.dev", }, ],});// Second session: get user infoconst result = await agent.invoke({ messages: [ { role: "user", content: "Get user info for user with id 'abc123'" }, ],});console.log(result);// Here is the user info for user with ID "abc123":// - Name: Foo// - Age: 25// - Email: foo@langchain.dev
Stream custom updates from tools as they execute using config.streamWriter. This is useful for providing real-time feedback to users about what a tool is doing.
Copy
import * as z from "zod";import { tool } from "langchain";const getWeather = tool( ({ city }, config) => { const writer = config.streamWriter; // Stream custom updates as the tool executes writer(`Looking up data for city: ${city}`); writer(`Acquired data for city: ${city}`); return `It's always sunny in ${city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string(), }), });