I was looking for a way to dynamically generate the agents.yaml and tasks.yaml files including the task and agent decorators in crew.py. I thought I might have the start of a script built to run “ollama list” pulling the list of local agents, but the whole system changed on me the past couple of days. This would have allowed me to generate a permanent agents and crew.py file with the correct decorators.
Here is the crew.py file:
import os
import subprocess
import yaml
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai_tools.tools.rag.rag_tool import RagTool
from crewai_tools import SerperDevTool
# Tools
rag_tool = RagTool()
serper_tool = SerperDevTool()
@CrewBase
class GenerateAgentInfoCrew:
"""Crew to dynamically generate ideal agent information from models."""
output_file = 'outputs/ollama_agents.md' # Single output file
def __init__(self):
# Ensure output directory exists
os.makedirs(os.path.dirname(self.output_file), exist_ok=True)
self.models = self.load_ollama_models() # Load models dynamically
def load_ollama_models(self):
"""Loads model names from the 'ollama list' command output."""
try:
# Run the ollama list command and capture the output
result = subprocess.run(['ollama', 'list'], capture_output=True, text=True)
# Split the output into lines, skipping the header
lines = result.stdout.splitlines()[1:]
# Extract the model names from the first column
models = []
for line in lines:
parts = line.split()
if len(parts) > 0:
model_name = parts[0].split(':')[0] # Remove the :latest tag
models.append({"llm": model_name})
return models
except FileNotFoundError:
print("Error: 'ollama' command not found. Make sure Ollama is installed and in your PATH.")
return []
@agent
def dynamic_agent(self, model) -> Agent:
"""Creates a dynamic agent for the given model."""
agent_name = f"agent_{model['llm']}"
return Agent(
name=agent_name,
role="Model Ideal Agent Creator",
goal="Generate the ideal CrewAI agent information.",
backstory=(
"You are the model loaded from the Ollama repository. "
"Your task is to describe your ideal CrewAI agent, including role, goal, and backstory."
),
llm=f"ollama/{model['llm']}", # Add the "ollama/" prefix here
tools=[rag_tool, serper_tool],
verbose=True,
memory=True,
allow_delegation=False,
human_input=False
)
@task
def generate_agent_info_task(self, model) -> Task:
"""Task for generating agent information."""
description = (
"Describe your ideal CrewAI agent with the following details:\n"
"- Role: What would be your primary role as an agent?\n"
"- Goal: What would you aim to achieve?\n"
"- Backstory: Provide a creative backstory for your agent.\n"
"- LLM: Mention the model you represent."
)
# Modify the task to append results to a single file
def write_output(output):
with open(self.output_file, mode='a', encoding='utf-8') as f:
f.write(f"## Agent Information for {model['llm']}\n\n")
f.write(output + "\n\n")
agent_name = f"agent_{model['llm']}" # Use the same name here
return Task(
description=description,
expected_output="Role, Goal, Backstory, and LLM description.",
tools=[rag_tool, serper_tool],
agent=agent_name, # Refer to the agent by its name
post_process=write_output # Append output to the single file
)
@crew
def crew(self) -> Crew:
"""Creates a dynamic crew for generating agent information."""
agents = []
tasks = []
for model in self.models:
agent_instance = self.dynamic_agent(model)
agents.append(agent_instance)
tasks.append(self.generate_agent_info_task(model))
def generate_yaml_files(agents, tasks):
agents_data = {agent.name: {"llm": agent.llm} for agent in agents}
with open("config/agents.yaml", "w") as f: # Write to config/agents.yaml
yaml.dump(agents_data, f)
tasks_data = {
task.name: {"agent": task.agent, "description": task.description}
for task in tasks
}
with open("config/tasks.yaml", "w") as f: # Write to config/tasks.yaml
yaml.dump(tasks_data, f)
# Generate the YAML files
generate_yaml_files(agents, tasks)
# Rename existing YAML files if they exist
if os.path.exists("config/agents.yaml"):
os.rename("config/agents.yaml", "config/agents.yaml.old")
if os.path.exists("config/tasks.yaml"):
os.rename("config/tasks.yaml", "config/tasks.yaml.old")
# Load the Crew from the generated YAML files
return Crew.from_yaml() # This will load from config/agents.yaml and config/tasks.yaml
# Example of execution:
if __name__ == "__main__":
crew = GenerateAgentInfoCrew().crew()
crew.kickoff(inputs={})