使用crewAI 创建游戏构建器AI 代理。我的代码没有按预期启动代理,而是返回一个对象 <crewai.project.crew_base.CrewBase..WrappedClass object at 0x1012252d0>。
我期待这样的输出:
[DEBUG]: == Working Agent: Financial Researcher
[INFO]: == Starting Task: You will create a game using python, these are the instructions:
Instructions
------------
{game}
Your Final answer must be the full python code, only the python code and nothing else.
> Entering new CrewAgentExecutor chain...
在 main.py 中我已尝试使用 str() 转换结果并添加打印语句来识别停止工作的位置:
import os
from dotenv import load_dotenv
load_dotenv()
from crewai.project import CrewBase, crew
from crewai import Crew, Process
from .tasks import GameTasks
from .agents import GameAgents
@CrewBase
class GameBuildCrew():
tasks = GameTasks()
agents = GameAgents()
def __init__(self):
print("## Welcome to the Game Crew")
print('-------------------------------')
self.game = input("What is the game you would like to build? What will be the mechanics?\n")
@crew
def crew(self) -> Crew:
print("crew called")
return Crew(
agents=[
self.agents.senior_engineer_agent(),
self.agents.qa_engineer_agent(),
self.agents.chief_qa_engineer_agent()
],
tasks=[
self.tasks.code_task(self.senior_engineer_agent),
self.tasks.review_task(self.qa_engineer_agent),
self.tasks.evaluate_task(self.chief_qa_engineer_agent)
],
process=Process.sequential,
verbose=2,
)
def run(self):
print("run called")
crew_instance = self.crew()
result = crew_instance.kickoff()
print("\n\n########################")
print("## Here is the result")
print("########################\n")
print("final code for the game:")
print(result)
if __name__ == '__main__':
game_build_crew = GameBuildCrew()
game_build_crew.run()
The issue stems from how you're interacting with the
crew_instance.kickoff()
method and the structure of your
CrewBase
subclass. Let's break it down:
-
crew_instance.kickoff()
is likely asynchronous: CrewAI likely leverages asynchronous operations for task execution, especially when dealing with potentially time-consuming agent interactions. This meanskickoff()
might not return the final result immediately. Instead, it could return a representation of the ongoing process (which is why you're seeing theWrappedClass
object). -
You're attempting to print the result immediately: Your
print(result)
line executes right afterkickoff()
, without waiting for the asynchronous operations to complete.
Solution Approach:
To get the intended output, you need to adapt your code to work with CrewAI's asynchronous nature. Without knowing the specifics of
crewai
, here's a conceptual approach using Python's
asyncio
:
import asyncio
import os
from dotenv import load_dotenv
load_dotenv()
from crewai.project import CrewBase, crew
from crewai import Crew, Process
from .tasks import GameTasks
from .agents import GameAgents
@CrewBase
class GameBuildCrew:
# ... (rest of your class code)
async def run(self): # Make 'run' asynchronous
print("run called")
crew_instance = self.crew()
result = await crew_instance.kickoff() # Await the result
print("\n\n########################")
print("## Here is the result")
print("########################\n")
print("final code for the game:")
print(result)
if __name__ == "__main__":
game_build_crew = GameBuildCrew()
asyncio.run(game_build_crew.run()) # Run within an event loop
Explanation:
-
async def run()
: We define therun
method as an asynchronous coroutine usingasync def
. This allows us to useawait
inside it. -
await crew_instance.kickoff()
: We useawait
to pause execution until thekickoff()
method completes and provides the actual result. -
asyncio.run(game_build_crew.run())
: We utilizeasyncio.run()
to create an event loop, which is necessary for running asynchronous code.
Important Considerations:
- CrewAI Documentation: Refer to CrewAI's documentation for the most accurate guidance on handling asynchronous operations and retrieving results.
-
Error Handling:
Implement robust error handling (e.g., using
try...except
blocks) within your asynchronous code to catch potential exceptions during agent execution.
Let me know if you have any questions or need further assistance!
标签:python,agent,crewai From: 78780910