So finally got around to playing with controlflow ...
# marvin-ai
c
So finally got around to playing with controlflow a bit today and am blown away at how much cleaner it is than other frameworks. I have a code agent that writes code but I want to basically have this code agent write the code and send the code (or whatever really) to an agent writer (creates an agent) which then goes to a orchestrator agent to run the created agent to run the task , do u guys have any examples of this
🙌 5
j
I'm not totally sure I follow your objective. Does something like this shed light though?
Copy code
from pydantic import BaseModel
import controlflow as cf

# Define our specialized agents
code_writer = cf.Agent(name="CodeWriter", instructions="Write Python code based on given requirements")
agent_creator = cf.Agent(name="AgentCreator", instructions="Create ControlFlow agents based on provided code")


class AgentParams(BaseModel):
    name: str
    instructions: str
    
def create_agent(params: AgentParams):
    return cf.Agent(name=params.name, instructions=params.instructions)


@cf.flow
def create_and_run_custom_agent(task_description: str):
    # Step 1: Write code for a custom agent
    code = cf.run(
        f"Write Python code for an agent that can {task_description}",
        agents=[code_writer],
        result_type=str
    )

    # Step 2: generate an agent to run the code
    custom_agent_params = cf.run(
        "Create a ControlFlow agent using the provided code",
        agents=[agent_creator],
        context={"code": code},
        result_type=AgentParams
    )
    custom_agent = create_agent(custom_agent_params)
    

    # Step 3 -- not totally sure
    result = cf.run(
        agents=[custom_agent],
        
    )

    return result
In this flow: • the first task outputs some code • the second task generates an agent [really, it generates params for an agent that I instantiate] • the third task I'm not sure how to map onto your objective
🙌 1
c
Yea something like that, I’m trying to make a agent that can spawn another agent to do a task , in this case running code as an example
j
If helpful, think of
cf.run()
[or any CF run function] as "hey AI, I need
result_type
. Work on it until you're done." and then you take over in totally normal python code.
So in my example, I asked it to create the params so I can instantiate the agent
you could then either run the code yourself with eval/subprocess, or give you agent a tool for running python (see
controlflow.tools.code.python
)
c
Ahh yea I was looking at the code.python I think was giving that tool to the wrong agent to use, I also think I need to be precise on the code format it was returning it in markdown so code.python was erroring
j
For spawning other agents, I would suggest an approach like above where you let an AI generate the parameters and then hydrate the
Agent
yourself, just because agents take a bunch of extraneous params beyond what you probably want the AI to actually produce or that it can't natively output (like tools). So to attach tools to your dynamically created agent, you might tell your creator agent what tools are available, let it output the names of the tools it wants to use, then you manually hand the "full" tool objects to the dynamic agent. Sorry if that's a little unclear
Ah yes,
str
is a nebulous return type. You could try
Annotated[str, "valid Python code"]
which won't actually validate it to be clear, but will be shown to the agent
c
This helps a lot I was on the right path
j
Or give an agent a Python tool, tell it to rewrite the code until it runs!
Let us know what patterns you find that work -- this is very much let's discover best practices together territory
🙌 1
c
Yea I think I’m going to make it return a pydantic model and make it more verbose I’ve seen people do function mappings to those before and had decent luck
💥 1