一键导入
langchain-tools
Guide to using tool integrations in LangChain including pre-built toolkits, Tavily, Wikipedia, and custom tools
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guide to using tool integrations in LangChain including pre-built toolkits, Tavily, Wikipedia, and custom tools
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Understanding Deep Agents framework - what they are, how to create them with createDeepAgent, and the agent harness architecture with built-in middleware for planning, filesystems, and subagents.
Creating and using custom skills with progressive disclosure, SKILL.md format, and the Agent Skills protocol in Deep Agents.
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.
| name | langchain-tools |
| description | Guide to using tool integrations in LangChain including pre-built toolkits, Tavily, Wikipedia, and custom tools |
| language | js |
Tools enable LLMs to interact with external systems, perform calculations, search the web, query databases, and more. They extend model capabilities beyond text generation, making agents truly actionable.
| Tool/Toolkit | Best For | Package | Key Features |
|---|---|---|---|
| Tavily Search | Web search | @langchain/community | AI-optimized search API |
| Wikipedia | Encyclopedia queries | @langchain/community | Wikipedia API access |
| Calculator | Math operations | @langchain/community | Expression evaluation |
| DuckDuckGo Search | Privacy-focused search | @langchain/community | No API key needed |
| Browser Tools | Web automation | @langchain/community | Headless browsing |
| Vector Store Tools | Semantic search | Based on vector store | Query your data |
| Custom Tools | Your specific needs | @langchain/core/tools | Define any function |
Choose Tavily if:
Choose Wikipedia if:
Choose Custom Tools if:
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
// Initialize Tavily (requires API key)
const searchTool = new TavilySearchResults({
maxResults: 3,
apiKey: process.env.TAVILY_API_KEY,
});
// Use directly
const results = await searchTool.invoke("Latest AI news");
console.log(results);
// Use with agent
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const model = new ChatOpenAI({ modelName: "gpt-4" });
const agent = createReactAgent({
llm: model,
tools: [searchTool],
});
const response = await agent.invoke({
messages: [{ role: "user", content: "What's new in AI today?" }]
});
import { WikipediaQueryRun } from "@langchain/community/tools/wikipedia_query_run";
const wikipediaTool = new WikipediaQueryRun({
topKResults: 3,
maxDocContentLength: 4000,
});
// Query Wikipedia
const result = await wikipediaTool.invoke("Artificial Intelligence");
console.log(result);
import { Calculator } from "@langchain/community/tools/calculator";
const calculator = new Calculator();
// Perform calculations
const result = await calculator.invoke("sqrt(144) + 5 * 3");
console.log(result); // "27"
// Use in agent for math problems
const mathAgent = createReactAgent({
llm: model,
tools: [calculator],
});
import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search";
const searchTool = new DuckDuckGoSearch({
maxResults: 5,
});
const results = await searchTool.invoke("LangChain framework");
import { tool } from "@langchain/core/tools";
import { z } from "zod";
// Define custom tool
const weatherTool = tool(
async ({ location, unit = "celsius" }) => {
// Your implementation
const data = await fetchWeather(location, unit);
return `The weather in ${location} is ${data.temp}°${unit === "celsius" ? "C" : "F"}`;
},
{
name: "get_weather",
description: "Get the current weather for a location. Use this when users ask about weather.",
schema: z.object({
location: z.string().describe("The city name, e.g., 'San Francisco'"),
unit: z.enum(["celsius", "fahrenheit"]).optional().describe("Temperature unit"),
}),
}
);
// Use with agent
const agent = createReactAgent({
llm: model,
tools: [weatherTool],
});
const response = await agent.invoke({
messages: [{ role: "user", content: "What's the weather in London?" }]
});
import { StructuredTool } from "@langchain/core/tools";
import { z } from "zod";
class DatabaseQueryTool extends StructuredTool {
name = "database_query";
description = "Query the customer database for information";
schema = z.object({
customerId: z.string().describe("Customer ID to look up"),
});
async _call({ customerId }: { customerId: string }): Promise<string> {
// Your database logic
const customer = await db.getCustomer(customerId);
return JSON.stringify(customer);
}
}
const dbTool = new DatabaseQueryTool();
import { createRetrieverTool } from "langchain/tools/retriever";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create vector store
const vectorStore = await MemoryVectorStore.fromTexts(
["LangChain is a framework...", "Agents use tools..."],
[{}, {}],
new OpenAIEmbeddings()
);
// Convert to tool
const retrieverTool = createRetrieverTool(
vectorStore.asRetriever(),
{
name: "knowledge_base",
description: "Search the knowledge base for information about LangChain",
}
);
// Use in agent
const agent = createReactAgent({
llm: model,
tools: [retrieverTool],
});
import { ChatOpenAI } from "@langchain/openai";
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
import { Calculator } from "@langchain/community/tools/calculator";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
// Define tools
const searchTool = new TavilySearchResults({ maxResults: 3 });
const calculator = new Calculator();
const customTool = tool(
async ({ query }) => {
// Your custom logic
return `Custom result for: ${query}`;
},
{
name: "custom_lookup",
description: "Look up custom information",
schema: z.object({
query: z.string().describe("The query to look up"),
}),
}
);
// Create agent with multiple tools
const agent = createReactAgent({
llm: new ChatOpenAI({ modelName: "gpt-4" }),
tools: [searchTool, calculator, customTool],
});
// Agent will choose appropriate tool(s)
const response = await agent.invoke({
messages: [{
role: "user",
content: "Search for the population of Tokyo and calculate if it doubled"
}]
});
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const apiTool = tool(
async ({ endpoint }) => {
try {
const response = await fetch(`https://api.example.com/${endpoint}`);
if (!response.ok) {
return `API error: ${response.statusText}`;
}
const data = await response.json();
return JSON.stringify(data);
} catch (error) {
return `Failed to call API: ${error.message}`;
}
},
{
name: "api_call",
description: "Call external API",
schema: z.object({
endpoint: z.string().describe("API endpoint to call"),
}),
}
);
✅ Use pre-built tools
✅ Create custom tools
✅ Combine multiple tools
✅ Handle tool responses
❌ Execute arbitrary code safely
❌ Bypass authentication
❌ Guarantee tool selection
❌ Use tools model doesn't support
// ❌ Missing API key
const tool = new TavilySearchResults();
await tool.invoke("query"); // Error!
// ✅ Provide API key
const tool = new TavilySearchResults({
apiKey: process.env.TAVILY_API_KEY,
});
Fix: Set required API keys in environment variables.
// ❌ Model doesn't support tool calling
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo-instruct" });
// This model doesn't support tools!
// ✅ Use tool-capable model
const model = new ChatOpenAI({ modelName: "gpt-4" });
const modelWithTools = model.bindTools([myTool]);
Fix: Use models that support function calling (GPT-4, Claude 3, etc.).
// ❌ Poor description
const tool = tool(
async ({ x }) => x * 2,
{
name: "tool1",
description: "A tool", // Too vague!
schema: z.object({ x: z.number() }),
}
);
// ✅ Clear, specific description
const tool = tool(
async ({ number }) => number * 2,
{
name: "double_number",
description: "Multiply a number by 2. Use this when the user wants to double a value.",
schema: z.object({
number: z.number().describe("The number to double"),
}),
}
);
Fix: Write clear descriptions that help the model know when to use the tool.
// ❌ No schema validation
const tool = tool(
async ({ location }) => {
// Assumes location is a string, but no validation
return location.toUpperCase(); // Could crash!
},
{
name: "format_location",
description: "Format location",
schema: z.object({ location: z.any() }), // Too permissive
}
);
// ✅ Proper schema
const tool = tool(
async ({ location }) => {
return location.toUpperCase();
},
{
name: "format_location",
description: "Format location name to uppercase",
schema: z.object({
location: z.string().describe("Location name"),
}),
}
);
Fix: Use specific Zod schemas for type safety.
# Community tools
npm install @langchain/community
# Core tools
npm install @langchain/core
# Specific integrations
npm install @langchain/openai # For OpenAI-based tools