From 79cfa87341b1074946b5b8ee2560d06eda94831c Mon Sep 17 00:00:00 2001 From: Ola Hungerford Date: Tue, 3 Dec 2024 08:13:06 -0800 Subject: [PATCH] Update using langchain tools docs (#1664) * Update example of how to use LangChain tools with correct syntax * Use .env * Add Code back --------- Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- docs/concepts/langchain-tools.mdx | 53 +++++++++++++++++++------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/docs/concepts/langchain-tools.mdx b/docs/concepts/langchain-tools.mdx index 538581aee..68a7998a9 100644 --- a/docs/concepts/langchain-tools.mdx +++ b/docs/concepts/langchain-tools.mdx @@ -7,32 +7,45 @@ icon: link ## Using LangChain Tools - CrewAI seamlessly integrates with LangChain’s comprehensive [list of tools](https://python.langchain.com/docs/integrations/tools/), all of which can be used with CrewAI. + CrewAI seamlessly integrates with LangChain's comprehensive [list of tools](https://python.langchain.com/docs/integrations/tools/), all of which can be used with CrewAI. ```python Code import os -from crewai import Agent -from langchain.agents import Tool -from langchain.utilities import GoogleSerperAPIWrapper +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 -# Setup API keys -os.environ["SERPER_API_KEY"] = "Your Key" +# Set up your SERPER_API_KEY key in an .env file, eg: +# SERPER_API_KEY= +load_dotenv() search = GoogleSerperAPIWrapper() -# Create and assign the search tool to an agent -serper_tool = Tool( - name="Intermediate Answer", - func=search.run, - description="Useful for search-based queries", -) +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) -agent = Agent( - role='Research Analyst', - goal='Provide up-to-date market analysis', - backstory='An expert analyst with a keen eye for market trends.', - tools=[serper_tool] + 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 ... @@ -40,6 +53,6 @@ agent = Agent( ## Conclusion -Tools are pivotal in extending the capabilities of CrewAI agents, enabling them to undertake a broad spectrum of tasks and collaborate effectively. -When building solutions with CrewAI, leverage both custom and existing tools to empower your agents and enhance the AI ecosystem. Consider utilizing error handling, caching mechanisms, -and the flexibility of tool arguments to optimize your agents' performance and capabilities. \ No newline at end of file +Tools are pivotal in extending the capabilities of CrewAI agents, enabling them to undertake a broad spectrum of tasks and collaborate effectively. +When building solutions with CrewAI, leverage both custom and existing tools to empower your agents and enhance the AI ecosystem. Consider utilizing error handling, caching mechanisms, +and the flexibility of tool arguments to optimize your agents' performance and capabilities.