The Python model can be used for generation of Python code. It takes care of indentation and import management, which simplifies “assembly” of Python sources from multiple elements.
// Building
PythonFactory pythonFactory = PythonFactory.eINSTANCE;
// Class
Class pClass = pythonFactory.createClass();
pClass.setName("LatestAiDevelopment");
pClass.getDecorators().add("CrewBase");
pClass.addItemImport("crewai.project", "CrewBase");
org.nasdanika.models.source.Source classDoc = org.nasdanika.models.source.Source.create("\"\"\"LatestAiDevelopment crew\"\"\"");
pClass.getBody().add(classDoc);
pClass.getBody().add(org.nasdanika.models.source.Source.create(
"""
# Learn more about YAML configuration files here:
# Agents: https://docs.crewai.com/concepts/agents#yaml-configuration-recommended
# Tasks: https://docs.crewai.com/concepts/tasks#yaml-configuration-recommended
agents_config = 'config/agents.yaml'
"""));
// Variable
Variable taskConfig = pythonFactory.createVariable();
taskConfig.setName("task_config");
taskConfig.setExpression("'config/tasks.yaml'");
pClass.getBody().add(taskConfig);
// Function
Function researcherFunction = pythonFactory.createFunction();
org.nasdanika.models.source.Source researcherFunctionBody = org.nasdanika.models.source.Source.create(
"""
return Agent(
config=self.agents_config['researcher'],
verbose=True
)
""");
researcherFunction.getBody().add(researcherFunctionBody);
researcherFunction.setAnnotation("Agent");
researcherFunction.setName("researcher");
researcherFunction.getParameters().add("self");
researcherFunction.getDecorators().add("agent");
researcherFunction.addItemImport("crewai", "Agent");
researcherFunction.addItemImport("crewai.project", "agent");
pClass.getBody().add(researcherFunction);
// Saving
CapabilityLoader capabilityLoader = new CapabilityLoader();
ProgressMonitor progressMonitor = new PrintStreamProgressMonitor();
Requirement<ResourceSetRequirement, ResourceSet> requirement = ServiceCapabilityFactory.createRequirement(ResourceSet.class);
ResourceSet resourceSet = capabilityLoader.loadOne(requirement, progressMonitor);
File pythonFile = new File("target/test.py").getCanonicalFile();
Resource pythonResource = resourceSet.createResource(URI.createFileURI(pythonFile.getAbsolutePath()));
pythonResource.getContents().add(pClass);
pythonResource.save(null);
Output:
from crewai import Agent
from crewai.project import CrewBase, agent
@CrewBase
class LatestAiDevelopment:
"""LatestAiDevelopment crew"""
# Learn more about YAML configuration files here:
# Agents: https://docs.crewai.com/concepts/agents#yaml-configuration-recommended
# Tasks: https://docs.crewai.com/concepts/tasks#yaml-configuration-recommended
agents_config = 'config/agents.yaml'
task_config = 'config/tasks.yaml'
@agent
def researcher(self) -> Agent:
return Agent(
config=self.agents_config['researcher'],
verbose=True
)