Мне удалось сделать эту функцию возможной, но, возможно, она просто делает слишком много, поскольку инструмент может очищать только один сайт. одновременно?
Мне просто нужно немного понять этот код и понять, как я могу устранить ошибку, если не существует более эффективных методов решения моих задач.
Код:
Код: Выделить всё
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
from crewai_tools import ScrapeWebsiteTool
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('OPENAI_KEY')
Model = 'gpt-3.5-turbo'
llm = ChatOpenAI(model=Model,api_key=api_key)
# Define a list of URLs to scrape (or load them from a file as needed)
urls = [
'https://www.ibm.com/topics/artificial-intelligence',
'https://www.ibm.com/topics/machine-learning'
]
# Instantiate tools
web_scrape_tool = ScrapeWebsiteTool() # No need to specify URL initially
# Create agents (remains the same)
web_scraper_agent = Agent(
role='Web Scraper',
goal='Effectively Scrape data on the websites for your company',
backstory='''You are expert web scraper, your job is to scrape all the data for
your company from a given website.
''',
tools=[web_scrape_tool],
verbose=True,
llm = llm
)
def sanitize_filename(url):
invalid_chars = ':/?\\|*\"'
for char in invalid_chars:
url = url.replace(char, "_")
return url
# Inside the loop, sanitize the URL before using it as part of the filename
for url in urls:
# Assuming the tool requires a config dict that includes the website_url
config = {'website_url': f'{url}'} # Construct the config with the required website_url
sanitized_url = sanitize_filename(url) # Sanitize the URL
web_scraper_task = Task(
description=f'Scrape data from {url}',
expected_output='Content from the website',
agent=web_scraper_agent,
output_file=f'data_{sanitized_url}.txt', # Use sanitized URL for filename
config=config # Include the config when setting up the task
)
crew = Crew(
agents=[web_scraper_agent],
tasks=[web_scraper_task],
verbose=2
)
# Execute tasks for all URLs
result = crew.kickoff()
print(result)
with open('results.txt', 'w') as f:
f.write(result)
Код: Выделить всё
> Entering new CrewAgentExecutor chain...
I need to read the content from the website https://www.ibm.com/topics/machine-learning to scrape the data for my company.
Action: Read website content
Action Input: {'website_url': 'https://www.ibm.com/topics/machine-learning'}
I encountered an error while trying to use the tool. This was the error: StructuredTool._run() missing 1 required keyword-only argument: 'config'.
Tool Read website content accepts these inputs: Read website content(website_url: 'string') - A tool that can be used to read a website content.
Thought: I need to read the content from the website https://www.ibm.com/topics/machine-learning to scrape the data for my company.
Action: Read website content
Action Input: {'website_url': 'https://www.ibm.com/topics/machine-learning'}
I encountered an error while trying to use the tool. This was the error: StructuredTool._run() missing 1 required keyword-only argument: 'config'.
Tool Read website content accepts these inputs: Read website content(website_url: 'string') - A tool that can be used to read a website content.
Я уже добавил эту «конфигурацию», так как она запрашивала ее в ошибка.
Подробнее здесь: https://stackoverflow.com/questions/787 ... ent-config