Skip to content
CrewAI 文档版本 跟随上游 源快照 fec58dbd 更新于 2026-10-18

LangChain 工具

LangChainTool 是 LangChain 工具和查询引擎的封装。

LangChainTool

INFO

CrewAI 可与 LangChain 全面的工具列表无缝集成,所有这些工具都可以与 CrewAI 一起使用。

python
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
from crewai.tools import BaseTool
from pydantic import Field
from langchain_community.utilities import GoogleSerperAPIWrapper

# Set up your SERPER_API_KEY key in an .env file, eg:
# SERPER_API_KEY=<your api key>
load_dotenv()

search = GoogleSerperAPIWrapper()

class SearchTool(BaseTool):
    name: str = "Search"
    description: str = "Useful for search-based queries. Use this to find current information about markets, companies, and trends."
    search: GoogleSerperAPIWrapper = Field(default_factory=GoogleSerperAPIWrapper)

    def _run(self, query: str) -> str:
        """Execute the search query and return results"""
        try:
            return self.search.run(query)
        except Exception as e:
            return f"Error performing search: {str(e)}"

# Create Agents
researcher = Agent(
    role='Research Analyst',
    goal='Gather current market data and trends',
    backstory="""You are an expert research analyst with years of experience in
    gathering market intelligence. You're known for your ability to find
    relevant and up-to-date market information and present it in a clear,
    actionable format.""",
    tools=[SearchTool()],
    verbose=True
)

# rest of the code ...

结论

工具对于扩展 CrewAI 智能体的能力至关重要,使其能够承担广泛的任务并有效协作。 使用 CrewAI 构建解决方案时,请同时利用自定义工具和现有工具来增强你的智能体并提升 AI 生态。考虑利用错误处理、缓存机制, 以及工具参数的灵活性来优化智能体的性能和能力。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。