Cody Webb
10/08/2024, 9:52 PMJeremiah
Jeremiah
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
Jeremiah
Cody Webb
10/08/2024, 9:59 PMJeremiah
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.Jeremiah
Jeremiah
controlflow.tools.code.python
)Cody Webb
10/08/2024, 10:02 PMJeremiah
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 unclearJeremiah
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 agentCody Webb
10/08/2024, 10:04 PMJeremiah
Jeremiah
Cody Webb
10/08/2024, 10:06 PM