LangChain Tool
LangChainTool
Section titled “LangChainTool”import osfrom dotenv import load_dotenvfrom crewai import Agent, Task, Crewfrom crewai.tools import BaseToolfrom pydantic import Fieldfrom 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 Agentsresearcher = 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 agents 的能力至关重要,它们使 agents 能够承担更广泛的任务并高效协作。 在使用 CrewAI 构建解决方案时,优先结合自定义工具和现有工具来增强你的 agents,并提升整个 AI 生态系统。可以考虑利用错误处理、缓存机制, 以及工具参数的灵活性来优化 agents 的性能和能力。