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.55k stars 2.55k forks source link

Custom tools not working #949

Open yoursweater opened 1 month ago

yoursweater commented 1 month ago

I can't get CrewAI to use any custom tools whatsoever for some reason, as it even when it appears to invoke the tool (and doesn't time out) I get an error stating the tool isn't found, or alternatively it claims to have been invoked but doesn't do anything.

Here's an example of the errors I'm seeing:

Action 'FileWriterTool("seo_game_design_101", "")' don't exist, these are the only available Actions: FileWriterTool: FileWriterTool(filename: 'string', content: 'string') - Writes given content to a specified file.

Sometimes it also just times out. Any ideas?

edit, here's the code:

import os
from crewai import Agent, Task, Crew, Process
from langchain_community.llms import Ollama

from crewai_tools import tool

os.environ["OPENAI_API_KEY"] = "NA"

llm = Ollama(
    model = "llama3",
    base_url = "http://localhost:11434",
        temperature = 0.1)

@tool
def FileWriterTool(filename: str, content: str) -> str:
    """Writes given content to a specified file."""
    with open(filename, 'w') as file:
        file.write(content)
        return f"Content successfully written to {filename}"

researcher = Agent(
    role='Knowledge Article Writer',
    goal='Create content of professional domains longer than 1000 words',
    backstory="Write articles about Game Design.",
    verbose=True,
    allow_delegation=False,
    max_iter=10,
    llm = llm,
    tools=[FileWriterTool]  # Include the file writer tool
)

writer = Agent(
    role='SEO Writer',
    goal='Convert article into SEO version',
    backstory="Define keywords and titles for better SEO.",
    verbose=True,
    allow_delegation=False,
    max_iter=10,
    llm = llm,
    tools=[FileWriterTool]
)

task1 = Task(
    description="Write several articles.",
    expected_output="At least 4 topics saved to files",
    agent=researcher,
    tools=[FileWriterTool],
    function_args={'filename': '<articles>.md', 'content': 'Example article content'}  
)

task2 = Task(
    description="Make articles into SEO version",
    expected_output="Convert into 8 SEO pages saved to files. the files name start with seo_",
    agent=writer,
    tools=[FileWriterTool],
    function_args={'filename': 'seo_<articles>.md', 'content': 'SEO optimized article content'} \
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=2
)
yoursweater commented 1 month ago

Just adding that I tried virtually every example of custom tools I could find, including some of the official docs ones like the multiplication example and that seems to break as well.

matt-crewai commented 1 month ago

This might be causing issues - functionargs={'filename': 'seo.md', 'content': 'SEO optimized article content'} \

And you can remove the tools rom the tasks if assigned to agent

matt-crewai commented 1 month ago

Also I like to use class instead of @tool decorator

from crewai_tools import BaseTool

class FileWriterTool(BaseTool):
    name: str = "FileWriterTool"
    description: str = "Writes given content to a specified file."

    def _run(self, filename: str, content: str) -> str:
        try:
            with open(filename, 'w') as file:
                file.write(content)
            return f"Content successfully written to {filename}"
        except Exception as e:
            print(f"Error writing to file: {e}")
            return f"Failed to write to {filename}: {str(e)}"

Then create and instance

FileWriteTool = FileWriterTool()
yoursweater commented 1 month ago

@matt-crewai so I switched over to using a class and then removed the function args and tools from the tasks. Here's the error:

Action: FileWriter("seo_game_design_title", "What is Game Design? - The Fundamentals of Creating Engaging Gaming Experiences")

Action Input: {"title": "What is Game Design?", "description": "Game design is a crucial aspect of creating engaging and immersive gaming experiences. It involves crafting the overall vision, mechanics, and story of a game to ensure that players have a memorable time playing it." 

Action 'FileWriter("seo_game_design_title", "What is Game Design? - The Fundamentals of Creating Engaging Gaming Experiences")' don't exist, these are the only available Actions: FileWriter: FileWriter(filename: 'string', content: 'string') - Writes given content to a specified file.

As I'm looking at it a little more closely it seems like it really struggles to coerce the arguments into the proper format for the tool, but it DOES look like it is now seeing the tool and attempting to invoke it, so that's progress!

edit: actually on another run it looked like the arguments were correct and it still didn't invoke properly for some reason:

hought:
Action: FileWriter
Action Input: {"filename": "game_design_fundamentals.txt", "contentI'm excited to start working on this task. As a Knowledge Article Writer, my goal is to create high-quality content that meets the expectations of professionals in the game design domain.

Action: FileWriter
Action Input: {"filename": "storytelling_in_game_design.txt", "content": "The Role of Storytelling in Game Design\n\nStorytelling is a crucial aspect of game design. It's what draws players into the world, makes them care about the characters, and keeps them engaged throughout the game.\n\nA good story can make or break a game. It's what sets apart a mediocre game from a great one. A well-crafted narrative can evoke...<cut off by me>

I'm somewhat concerned about the lack of closing quotes on the "content" argument being referenced on the action input.

edit: sometimes the closing quotes appear. However, it seems the tool is never being invoked as I don't see the print statements being executed, even if the chain resolves successfully.

yoursweater commented 1 month ago

@matt-crewai actually after going through the source I think I found the problem, and it appears to be addressed by this PR that went in but isn't yet published to pip: https://github.com/crewAIInc/crewAI/pull/925

alphaveneno commented 1 month ago

Wondering if you might have the wrong tool decorator.

instead of: from crewai_tools import tool

try: from langchain.tools import tool

Also it may help if you give it a hint, e.g: @tool("name_of_your_tool")

goran-ristic-dev commented 1 month ago

@yoursweater Hello, I was having same issues with local ollama . From my perspective this is due poor reasoning of the model, and you can improve it with explicit statements in prompts. e.g.

task2 = Task( description="Make articles into SEO version. ", expected_output="""You should write articles to the file using FileWriterTool. Process:

  1. Invoke tool with key-value pairs "filename": "<name of the file. Should start with seo_ and end with .txt>", "content": ""
  2. make sure that key-value pairs are enclosed with { } example of args passed to the FileWriterTool: {"filename": "storytelling_in_game_design.txt", "content": "The Role of Storytelling..."} """, agent=writer, tools=[FileWriterTool,], functionargs={'filename': 'seo.md', 'content': 'SEO optimized article content'} )

when decorating function with "@tool" put @tool("FileWriterTool")

LuvKaranwal21 commented 3 weeks ago

unction_args

I've the same problem, it seems to struggle to take input in the correct format image


from crewai import Agent, Task, Crew
from langchain.llms import Ollama
import os
import subprocess
from langchain.tools import tool
from crewai_tools import BaseTool

os.environ["OPENAI_API_KEY"] = "NA"

class CommandExecutioner(BaseTool):
    name: str ="Mathematical evaluation calculator"
    description: str = (" Useful to execute mathematical expressions, so pass a mathematical expression in the form of string like for ex: '2+3' , '15*20/40' etc "
            " The type of input to this tool should be string. ")
    def _run(self, text) -> str:
        result = eval(text)
        print("Result from CommandExecutioner : ", result.stdout.decode('utf-8'))
        return "Ran the command : " + str(text) + ". The result is : " + str(result)

ce = CommandExecutioner()

llm = Ollama(
    model = "llama3.1",
    base_url = "http://localhost:11434")

maths_professor = Agent(
    role='Maths Professor',
    goal="Your aim is to evaluate the mathematical expressions given by the user by using Mathematical evaluation calculator tool available to you.",
    backstory=("You're a Math Professor with 10+ years of experience in teaching Mathematics. You're very brilliant and you can quickly solve any mathematical expressions "
               "given to you by using the Mathematical evaluation calculator tool."
    ),
    allow_delegation = False,
    verbose=True,
    llm=llm,
)

simple_expression = Task(
    description=("Could you please tell me what's '2*3' by using Mathematical evaluation calculator tool ? "),
    expected_output=("Provide the Mathematical evaluation calculator tool with the expression that user has given you to answer quickly."),
    agent=maths_professor,
    tools = [ce]
)

crew = Crew(
            agents=[maths_professor],
            tasks=[simple_expression],
            verbose=2
        )

result = crew.kickoff()

print(result)
mdhuzaifapatel commented 2 weeks ago

Also I like to use class instead of @tool decorator

from crewai_tools import BaseTool

class FileWriterTool(BaseTool):
    name: str = "FileWriterTool"
    description: str = "Writes given content to a specified file."

    def _run(self, filename: str, content: str) -> str:
        try:
            with open(filename, 'w') as file:
                file.write(content)
            return f"Content successfully written to {filename}"
        except Exception as e:
            print(f"Error writing to file: {e}")
            return f"Failed to write to {filename}: {str(e)}"

Then create and instance

FileWriteTool = FileWriterTool()

I used the same method but it's not working for me. This is the output:

Action:
Action Input:

I forgot the Action name, these are the only available Actions: Tool Name: FileWriterTool(filename: str, content: str) -> str
Tool Description: FileWriterTool(filename: 'string', content: 'string') - Writes given content to a specified file.
Tool Arguments: {'filename': {'title': 'Filename', 'type': 'string'}, 'content': {'title': 'Content', 'type': 'string'}}

Please let me know how to fix it.