From 9b54af5ef418e5cdb7c60cd5b172b1e20ecaef60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Tue, 14 Nov 2023 12:45:15 -0300 Subject: [PATCH] Updated Agent Tools (markdown) --- Agent-Tools.md | 70 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/Agent-Tools.md b/Agent-Tools.md index a655edc..40daf22 100644 --- a/Agent-Tools.md +++ b/Agent-Tools.md @@ -9,26 +9,72 @@ These tools can be as straightforward as a search function or as sophisticated a - **Integration**: Tools can be integrated into agents to extend their capabilities beyond their basic functions. - **Customizability**: Developers can create custom tools tailored to the specific needs of their agents or use pre-built LangChain ones available in the ecosystem. -## Implementing Tools - -Implementing a tool involves integrating it with an agent so it can be used to carry out tasks. For example, you might integrate SerpAPI as a tool for an agent that needs to perform web searches. - # Creating and Assigning Tools -To assign a tool to an agent, you'd provide it as part of the agent's properties during initialization. Below is a conceptual example: +## Creating your own Tools + +You can easily create your own tool using [LangChain Tool Custom Tool Creation](https://python.langchain.com/docs/modules/agents/tools/custom_tools). + +Example: +```python +import json +import requests +from langchain.tools import tool +from unstructured.partition.html import partition_html + +class BrowserTools(): + @tool("Scrape website content") + def scrape_website(website): + """Useful to scrape a website content""" + url = f"https://chrome.browserless.io/content?token={config('BROWSERLESS_API_KEY')}" + payload = json.dumps({"url": website}) + headers = { + 'cache-control': 'no-cache', + 'content-type': 'application/json' + } + response = requests.request("POST", url, headers=headers, data=payload) + elements = partition_html(text=response.text) + content = "\n\n".join([str(el) for el in elements]) + + # Return only the first 5k characters + return content[:5000] + + +# Create an agent and assign the search tool +agent = Agent( + role='Research Analyst', + goal='Provide up-to-date market analysis', + backstory='An expert analyst with a keen eye for market trends.', + tools=[BrowserTools().scrape_website] +) +``` + +## Using Existing Tools + +Check [LangChain Integration](https://python.langchain.com/docs/integrations/tools/) for a set of useful existing tools. +To assign a tool to an agent, you'd provide it as part of the agent's properties during initialization. ```python from crewai import Agent -from langchain.tools import SerpAPI +from langchain.agents import Tool +from langchain.utilities import GoogleSerperAPIWrapper -# Instantiate the SerpAPI tool -serp_api_tool = SerpAPI(...) +# Initialize SerpAPI tool with your API key +os.environ["OPENAI_API_KEY"] = "" -# Create an agent and assign the SerpAPI tool +# Create tool to be used by agent +serper_tool = Tool( + name="Intermediate Answer", + func=search.run, + description="useful for when you need to ask with search", +) + +# Create an agent and assign the search tool agent = Agent( - role='Researcher', - goal='Gather market data', - tools=[serp_api_tool] + role='Research Analyst', + goal='Provide up-to-date market analysis', + backstory='An expert analyst with a keen eye for market trends.', + tools=[serper_tool] ) ```