crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
18.97k stars 2.61k forks source link

How to let an agent to read a json file #795

Closed dwk601 closed 2 weeks ago

dwk601 commented 2 months ago

I have a data engineer agent that needs to read json file and reformat to a csv file how to make the agent read file and do a task? I'm getting an error with this code right now.

Traceback (most recent call last): File "/home/dongwook/Project/auto_crawl/main.py", line 71, in result = crew.kickoff() File "/home/dongwook/Project/auto_crawl/venv/lib/python3.10/site-packages/crewai/crew.py", line 264, in kickoff result = self._run_sequential_process() File "/home/dongwook/Project/auto_crawl/venv/lib/python3.10/site-packages/crewai/crew.py", line 287, in _run_sequential_process if task.agent.allow_delegation: # type: ignore # Item "None" of "Agent | None" has no attribute "allow_delegation" AttributeError: 'NoneType' object has no attribute 'allow_delegation'

data_engineer = Agent(
   role="senior data engineer",
   goal="clean and refactor a given dataset to a cleaner version that can be used in database",
   backstory="You are a senior data engineer who manages data pipelines and data storage solutions"
   "about the json file"
   "You are tasked with cleaning and refactoring a given dataset to a cleaner version that can be used in a database"
   "The dataset is a CSV file that contains data about and it is not in a good shape"
   "You need to clean the data, remove any duplicates, and refactor it to a cleaner version that can be used in a database",
   tools=[file_read_tool],
)

task = Task(
   description=(
      "1. Read the given school admission dataset and understand the data, "
      "2. Clean the data by removing any duplicates, "
      "3. Refactor the data to a cleaner version that can be used in a database, "
      "4. Outline the dataset with the name of the university at the first row, "
      "5. Following by the university name the columns should be the each admission requirements for out of country students or international students, "
      "6. Save the final dataset in a CSV format"
   ),
   expected_output="A cleaned and refactored dataset that can be used in a database"
      "The dataset should have a column with the name of the university at the first row, "
      "Following by the university name the columns should be the each admission requirements for out of country students or international students, "
      "The final dataset should be in a CSV format",
   agents=data_engineer,
   tools=[file_read_tool],
)

crew = Crew(
   agents=[data_engineer],
   tasks=[task],
   verbose=True,
)

result = crew.kickoff()
yuripourre commented 2 months ago

@dwk601 it looks like the the process cannot assign the task to an agent, I suggest you to split your task into multiple tasks and assign to multiple agents.

saqib727 commented 2 months ago

Refer the documentation:

https://docs.crewai.com/tools/JSONSearchTool/

avri-schneider commented 2 months ago

@dwk601 shouldn't you set agent property of Task object, instead of agents?

snailfrying commented 2 months ago

from crewai_tools import tool

@tool("用户发布啤酒数据抽取工具") def load_peer_tool(file_path = '../data/weibo_peer.xlsx') -> np.array: """ excel文件读取工具,用于读取用户发布啤酒文本数据 参数: file_path (str): 存储数据的文件名。 返回: np.array: 用户对啤酒发布信息数据。 """ data = pd.read_excel(file_path) json_data = data[['id', '内容']].to_json(orient='records', force_ascii=False) return np.array(json_data)

import re import json @tool("存储json数据为文件") def save_json(data, filename='../data/beer_analysis.xlsx'): """ 将数据存储为JSON文件。 参数: data (dict): 要存储的数据字典,必须传入整体数据,不可传入变量。 filename (str): 存储数据的文件名。 """ filename = re.sub(r'"', '', filename) try: with open(filename, 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=False, indent=4) print(f"数据已成功存储到 {filename}") except Exception as e: print(f"存储数据时出错: {e}")

@tool("读取json文件") def load_json(filename): """ 从JSON文件中读取数据。 参数: filename (str): 要读取的文件名。 返回: dict: 读取的数据字典。 """ try: with open(filename, 'r', encoding='utf-8') as file: data = json.load(file) print(f"数据已成功从 {filename} 读取") return data except FileNotFoundError: print(f"文件 {filename} 未找到") except json.JSONDecodeError: print(f"文件 {filename} 不是有效的JSON格式") except Exception as e: print(f"读取数据时出错: {e}") return None

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 5 days with no activity.