-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from crewai import Agent | ||
from langchain_groq import ChatGroq | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
class MLAgents(): | ||
def __init__(self,model) -> None: | ||
self.llm = ChatGroq( | ||
api_key=os.environ['GROQ_API_KEY'], | ||
model_name=model) | ||
|
||
def problem_definition_agent(self): | ||
return Agent( | ||
role='Problem_Definition_Agent', | ||
goal="""clarify the machine learning problem the user wants to solve, | ||
identifying the type of problem (e.g., classification, regression) and any specific requirements.""", | ||
backstory="""You are an expert in understanding and defining machine learning problems. | ||
Your goal is to extract a clear, concise problem statement from the user's input, | ||
ensuring the project starts with a solid foundation.""", | ||
verbose=True, | ||
allow_delegation=False, | ||
llm=self.llm, | ||
) | ||
|
||
def data_assessment_agent(self): | ||
return Agent( | ||
role='Data_Assessment_Agent', | ||
goal="""evaluate the data provided by the user, assessing its quality, | ||
suitability for the problem, and suggesting preprocessing steps if necessary.""", | ||
backstory="""You specialize in data evaluation and preprocessing. | ||
Your task is to guide the user in preparing their dataset for the machine learning model, | ||
including suggestions for data cleaning and augmentation.""", | ||
verbose=True, | ||
allow_delegation=False, | ||
llm=self.llm, | ||
) | ||
|
||
def model_recommendation_agent(self): | ||
return Agent( | ||
role='Model_Recommendation_Agent', | ||
goal="""suggest the most suitable machine learning models based on the problem definition | ||
and data assessment, providing reasons for each recommendation.""", | ||
backstory="""As an expert in machine learning algorithms, you recommend models that best fit | ||
the user's problem and data. You provide insights into why certain models may be more effective than others, | ||
considering classification vs regression and supervised vs unsupervised frameworks.""", | ||
verbose=True, | ||
allow_delegation=False, | ||
llm=self.llm, | ||
) | ||
|
||
def starter_code_agent(self): | ||
return Agent( | ||
role='Starter_Code_Generator_Agent', | ||
goal="""generate starter Python code for the project, including data loading, | ||
model definition, and a basic training loop, based on findings from the problem definitions, | ||
data assessment and model recommendation""", | ||
backstory="""You are a code wizard, able to generate starter code templates that users | ||
can customize for their projects. Your goal is to give users a head start in their coding efforts.""", | ||
verbose=True, | ||
allow_delegation=False, | ||
llm=self.llm, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from crewai import Crew | ||
from .tasks import MLTask | ||
from .agents import MLAgents | ||
import pandas as pd | ||
|
||
def run_ml_crew(file_path, user_question, model="llama3-70b-8192"): | ||
try: | ||
df = pd.read_csv(file_path).head(5) | ||
except Exception as e: | ||
return {"error": f"Error reading the file: {e}"} | ||
|
||
# Initialize agents and tasks | ||
tasks = MLTask() | ||
agents = MLAgents(model=model) | ||
|
||
problem_definition_agent = agents.problem_definition_agent() | ||
data_assessment_agent = agents.data_assessment_agent() | ||
model_recommendation_agent = agents.model_recommendation_agent() | ||
starter_code_agent = agents.starter_code_agent() | ||
|
||
task_define_problem = tasks.task_define_problem(problem_definition_agent) | ||
task_assess_data = tasks.task_assess_data(data_assessment_agent) | ||
task_recommend_model = tasks.task_recommend_model(model_recommendation_agent) | ||
task_generate_code = tasks.task_generate_code(starter_code_agent) | ||
|
||
# Format the input data for agents | ||
input_data = { | ||
"ml_problem": user_question, | ||
"df": df.head(), | ||
"file_name": file_path | ||
} | ||
|
||
# Initialize and run the crew | ||
ml_crew = Crew( | ||
agents=[problem_definition_agent, data_assessment_agent, model_recommendation_agent, starter_code_agent], | ||
tasks=[task_define_problem, task_assess_data, task_recommend_model, task_generate_code], | ||
verbose=True | ||
) | ||
|
||
result = ml_crew.kickoff(input_data) | ||
return result | ||
|
||
if __name__=="__main__": | ||
print(run_ml_crew(file_path="data/iris.csv", | ||
user_question=""" | ||
I have the iris dataset and I would like to build a machine learning model to classify the species of iris flowers based on their sepal and petal measurements. | ||
The dataset contains four features: sepal length, sepal width, petal length, and petal width. | ||
The target variable is the species of the iris flower,which can be one of three types: Setosa, Versicolor, or Virginica. | ||
I would like to know the most suitable model for this classification problem and also get some starter code for the project. | ||
""", | ||
model="mixtral-8x7b-32768")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from crewai import Task | ||
|
||
class MLTask(): | ||
def task_define_problem(self,agent): | ||
return Task( | ||
description="""Clarify and define the machine learning problem, | ||
including identifying the problem type and specific requirements. | ||
Here is the user's problem: | ||
{ml_problem} | ||
""", | ||
agent=agent, | ||
expected_output="A clear and concise definition of the machine learning problem." | ||
) | ||
|
||
def task_assess_data(self,agent): | ||
return Task( | ||
description="""Evaluate the user's data for quality and suitability, | ||
suggesting preprocessing or augmentation steps if needed. | ||
Here is a sample of the user's data: | ||
{df} | ||
""", | ||
agent=agent, | ||
expected_output="An assessment of the data's quality and suitability, with suggestions for preprocessing or augmentation if necessary." | ||
) | ||
|
||
def task_recommend_model(self,agent): | ||
return Task( | ||
description="""Suggest suitable machine learning models for the defined problem | ||
and assessed data, providing rationale for each suggestion.""", | ||
agent=agent, | ||
expected_output="A list of suitable machine learning models for the defined problem and assessed data, along with the rationale for each suggestion." | ||
) | ||
|
||
def task_generate_code(self,agent): | ||
return Task( | ||
description="""Generate starter Python code tailored to the user's project using the model recommendation agent's recommendation(s), | ||
including snippets for package import, data handling, model definition, and training. """, | ||
agent=agent, | ||
expected_output="Python code snippets for package import, data handling, model definition, and training, tailored to the user's project, plus a brief summary of the problem and model recommendations." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters