Skip to content

Commit

Permalink
ADDED: ML Assistant Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
AquibPy committed Jul 1, 2024
1 parent cb2f0ca commit 4256c47
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ percentage, missing keywords, and profile summary.
- **Job Posting Drafting:** The job description writer agent uses the insights to create a detailed, engaging, and enticing job posting that aligns with the company's culture and values.
- **Review and Editing:** The review and editing specialist agent reviews the job posting for clarity, engagement, grammatical accuracy, and alignment with company values, refining it to ensure perfection.

### 29. ML Assistant Agent

- **Route:** `/ml_assistant`
- **Description:** This endpoint coordinates a team of AI agents to process an uploaded CSV file and a user-defined machine learning problem description. It aims to provide a clear problem definition, assess the data quality, recommend suitable machine learning models, and generate starter Python code for the project.
- **Features:**
- **Input Data:**
- **CSV File:** Users can upload a CSV file containing the dataset.
- **User Question:** Users provide a detailed description of their machine learning problem.
- **Problem Definition:**
- The problem definition agent clarifies and defines the machine learning problem, identifying the type of problem (e.g., classification, regression) and any specific requirements.
- **Data Assessment:**
- The data assessment agent evaluates the quality and suitability of the provided data and suggests preprocessing or augmentation steps if necessary.
- **Model Recommendation:**
- The model recommendation agent suggests suitable machine learning models based on the problem definition and data assessment, providing reasons for each recommendation.
- **Starter Code Generation:**
- The starter code generator agent produces starter Python code for the project, including data loading, model definition, and a basic training loop based on the findings from the problem definition, data assessment, and model recommendation.

## Usage

Each endpoint accepts specific parameters as described in the respective endpoint documentation. Users can make POST requests to these endpoints with the required parameters to perform the desired tasks.
Expand Down
63 changes: 63 additions & 0 deletions agents/ml_assistant/agents.py
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,
)
52 changes: 52 additions & 0 deletions agents/ml_assistant/crew.py
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"))

43 changes: 43 additions & 0 deletions agents/ml_assistant/tasks.py
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."
)
50 changes: 50 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from agents.investment_risk_analyst_agent.crew import run_investment_crew
from agents.agent_doc.crew import run_doc_crew
from agents.job_posting_agent.crew import run_job_crew
from agents.ml_assistant.crew import run_ml_crew
from langchain.agents import AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_cohere.react_multi_hop.agent import create_cohere_react_agent
Expand Down Expand Up @@ -992,7 +993,56 @@ async def run_job_agent(request:Request,
return ResponseText(response=jd)
except Exception as e:
return {"error": str(e)}

@app.post("/ml_assistant",description="""
Upload a CSV file and describe your machine learning problem.
The API will process the file and input to provide problem definition, data assessment, model recommendation, and starter code.
NOTE: In model input default is llama3-70b-8192 but you can choose mixtral-8x7b-32768, gemma-7b-it and llama3-8b-8192."
""")
async def ml_crew(file: UploadFile = File(...),user_question: str = Form(...),model: str = Form("llama3-70b-8192"),token: str = Depends(oauth2_scheme)):
try:
payload = jwt.decode(token, os.getenv("TOKEN_SECRET_KEY"), algorithms=[settings.ALGORITHM])
email = payload.get("sub")
if email is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
user = users_collection.find_one({"email": email})
if user is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")
except JWTError:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")

try:
with tempfile.NamedTemporaryFile(delete=False, suffix='.' + file.filename.split('.')[-1]) as tmp:
shutil.copyfileobj(file.file, tmp)
file_location = tmp.name
except Exception as e:
return JSONResponse(content={"error": f"Error handling uploaded file: {e}"}, status_code=400)
finally:
file.file.close()


try:
output = run_ml_crew(file_location, user_question,model=model)
os.remove(file_location)

if "error" in output:
return JSONResponse(content=output, status_code=400)

db = MongoDB()
payload = {
"endpoint": "/ml_assistant",
"propmt" : user_question,
"Model" : model,
"Output" : output
}
mongo_data = {"Document": payload}
result = db.insert_data(mongo_data)
print(result)
return ResponseText(response=output)

except Exception as e:
return {"error": str(e)}

if __name__ == '__main__':
import uvicorn
Expand Down

0 comments on commit 4256c47

Please sign in to comment.