mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
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>
This commit is contained in:
@@ -7,32 +7,45 @@ icon: link
|
|||||||
## Using LangChain Tools
|
## Using LangChain Tools
|
||||||
|
|
||||||
<Info>
|
<Info>
|
||||||
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.
|
||||||
</Info>
|
</Info>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import os
|
import os
|
||||||
from crewai import Agent
|
from dotenv import load_dotenv
|
||||||
from langchain.agents import Tool
|
from crewai import Agent, Task, Crew
|
||||||
from langchain.utilities import GoogleSerperAPIWrapper
|
from crewai.tools import BaseTool
|
||||||
|
from pydantic import Field
|
||||||
|
from langchain_community.utilities import GoogleSerperAPIWrapper
|
||||||
|
|
||||||
# Setup API keys
|
# Set up your SERPER_API_KEY key in an .env file, eg:
|
||||||
os.environ["SERPER_API_KEY"] = "Your Key"
|
# SERPER_API_KEY=<your api key>
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
search = GoogleSerperAPIWrapper()
|
search = GoogleSerperAPIWrapper()
|
||||||
|
|
||||||
# Create and assign the search tool to an agent
|
class SearchTool(BaseTool):
|
||||||
serper_tool = Tool(
|
name: str = "Search"
|
||||||
name="Intermediate Answer",
|
description: str = "Useful for search-based queries. Use this to find current information about markets, companies, and trends."
|
||||||
func=search.run,
|
search: GoogleSerperAPIWrapper = Field(default_factory=GoogleSerperAPIWrapper)
|
||||||
description="Useful for search-based queries",
|
|
||||||
)
|
|
||||||
|
|
||||||
agent = Agent(
|
def _run(self, query: str) -> str:
|
||||||
role='Research Analyst',
|
"""Execute the search query and return results"""
|
||||||
goal='Provide up-to-date market analysis',
|
try:
|
||||||
backstory='An expert analyst with a keen eye for market trends.',
|
return self.search.run(query)
|
||||||
tools=[serper_tool]
|
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 ...
|
# rest of the code ...
|
||||||
|
|||||||
Reference in New Issue
Block a user