首页 > 编程问答 >关键错误:“工具”。 CrewAI 在制作自定义工具时出错?

关键错误:“工具”。 CrewAI 在制作自定义工具时出错?

时间:2024-08-01 07:34:34浏览次数:14  
标签:python automation artificial-intelligence crewai

我开发了一个团队来从不同的 URL 获取一些信息。到目前为止总共有大约 3 个 URL,所以我创建了 5 个代理。 1 是编辑器(经理),1 是其他 3 个带到表中的所有数据的编译器。

如果这有帮助的话,这是我的文件夹结构

university_scraper/
│
├── src/
│   ├── __init__.py
│   ├── config.py
│   ├── main.py
│   ├── scraper/
│   │   ├── __init__.py
│   │   ├── agent.py
│   │   ├── task.py
│   │   ├── crew.py
│   ├── tools/
│   │   ├── __init__.py
│   │   ├── web_scrape_tool.py
│   ├── utils/
│   │   ├── __init__.py
│   │   ├── load_json.py
│   │   ├── format.py
│   │   ├── file_operations.py  
│
├── data/
│   ├── format.json
│   ├── urls.json              
│   ├── extracted_data.json
│
├── tests/                      
│   ├── __init__.py
│   ├── test_task.py            
│
├── .env
├── .gitignore
└── README.md

问题是我在“中有一个非常简单的类” search_tools.py”,这里是代码:

from crewai_tools import ScrapeWebsiteTool
from langchain.tools import tool

class SearchTools():
    @tool("Search for the details")
    def details_scraper_tool(url=''):
        """Search for the details"""
        return ScrapeWebsiteTool(website_url=url)
    
    @tool("Search for the programs")
    def programs_scraper_tool(url=''):
        """Search for the programs"""
        return ScrapeWebsiteTool(website_url=url)
    
    @tool("Search for the costs")
    def costs_scraper_tool(url = ''):
        """Search for the costs"""
        return ScrapeWebsiteTool(website_url=url)

这是我的代理,它利用这些函数之一(agents.py):

 def details_scraper_agent(self, details_scraper):
        agent = Agent(
            role='DetailsScraper',
            goal="""Get all the details of the University from the give URL',
            backstory='As an expert of knowing all the fields related to deatils, you are responsible for                   getting all the details of the University from the given URL, ensuring that nothing lefts behind""",
            tools=[details_scraper], 
            verbose=True,
        )

        return agent 

最后,main.py:

agents = AIScrapingAgents()
tasks = AIScrapingTasks()

site = 'https;//www.google.com'
details_scraper = ScrapeWebsiteTool(website_url=site)

openAIGPT4 = ChatOpenAI(
    model='gpt-4'
)

details_scraper_agent = agents.details_scraper_agent(details_scraper)

这是我的错误始终获取:

File "C:\Users\pvt\AppData\Local\pypoetry\Cache\virtualenvs\crewai-test-eDiMwRLS-py3.11\Lib\site-packages\langchain\agents\agent.py", line 980, in validate_tools
tools = values["tools"]
        ~~~~~~^^^^^^^^^
KeyError: 'tools'

单个 main 的示例:

site = "https://www.google.com/"
costs_scraper_tool = ScrapeWebsiteTool(website_url=site)

# initialize an agent

costs_scraper_agent = Agent(
                            role='CostsScraper',
                            goal='Scrape all the sorts of costs of the University from the given URL',
                            backstory='',
                            tools=[costs_scraper_tool], 
                            allow_delegation=True,
                            verbose=True,
                            llm=llm)

当然,任务也是在同一个文件和所有内容中创建的,它的工作方式就像一个魅力。有人可以帮我吗?

尽管我也尝试过不同的选择。我什至尝试在代理类本身内部完成所有函数的工作,但我仍然遇到相同的错误。这是我尝试过的示例:

class AIScrapingAgents():

    
    def details_scraper_agent(self):
        site = 'https://www.google.com/'
        details_scraper = ScrapeWebsiteTool(website_url=site)
        agent = Agent(
            role='DetailsScraper',
            goal='Get all the details of the University from the give URL',
            backstory=''
            tools=[details_scraper], 
            verbose=True,
        )

        return agent

仍然是相同的错误


看来问题出在如何访问和传递工具,而不是 CrewAI 工具本身。错误消息“KeyError: 'tools'”表明试图访问字典中不存在的“tools”键,该字典在 LangChain 的 Agent 类中使用。

我注意到的代码中有几处不一致,这些不一致可能会导致此错误:

  1. details_scraper_agent 方法中传递工具: main.py 中将 details_scraper 作为参数传递给 details_scraper_agent 方法,但该方法不使用此参数。应该在 Agent 初始化中使用传递的工具。

  2. 工具定义: 虽然定义了 SearchTools 类,但并没有实际使用它。应该创建一个 SearchTools 类的实例,然后将想要使用的工具方法作为列表传递给代理。

让我们修改的代码来解决这些问题:

# agents.py
from .tools.search_tools import SearchTools

class AIScrapingAgents():
    def __init__(self):
        self.tools = SearchTools()

    def details_scraper_agent(self):
        agent = Agent(
            role='DetailsScraper',
            goal='Get all the details of the University from the give URL',
            backstory='',
            tools=[self.tools.details_scraper_tool], 
            verbose=True,
        )
        return agent

# main.py
from .src.scraper.agent import AIScrapingAgents
# ... other imports

agents = AIScrapingAgents()
# ... other code

site = 'https://www.example.com' # Replace with your actual URL

details_scraper_agent = agents.details_scraper_agent()
details_scraper_agent.run(details_scraper_tool=site) 

说明:

  • 我们在 AIScrapingAgents 类中创建了 SearchTools 的实例。
  • details_scraper_agent 方法现在使用 self.tools 访问 SearchTools 实例,并传递 details_scraper_tool 方法作为代理的工具。
  • main.py 中,我们在调用 details_scraper_agent.run() 时传递了网站 URL 作为参数。

通过这些更改,的代码应该能够正确访问和使用定义的 CrewAI 工具,而不会出现“KeyError”。请注意,需要为其他代理和工具重复此模式,以确保它们都正确设置。

标签:python,automation,artificial-intelligence,crewai
From: 78816614

相关文章