joaomdmoura / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
16.98k stars 2.29k forks source link

Does CrewAI support dynamic generation of multiple agents? #605

Open a58982284 opened 1 month ago

a58982284 commented 1 month ago
from flask import Flask, request, jsonify
from crewai import Crew
from textwrap import dedent

from stock_analysis_agents import StockAnalysisAgents
from stock_analysis_tasks import StockAnalysisTasks

from dotenv import load_dotenv
load_dotenv()

app = Flask(__name__)

class FinancialCrew:
  def __init__(self, company):
    self.company = company

  def run(self):
    agents = StockAnalysisAgents()
    tasks = StockAnalysisTasks()

    research_analyst_agent = agents.research_analyst()
    financial_analyst_agent = agents.financial_analyst()
    investment_advisor_agent = agents.investment_advisor()

    research_task = tasks.research(research_analyst_agent, self.company)
    financial_task = tasks.financial_analysis(financial_analyst_agent)
    filings_task = tasks.filings_analysis(financial_analyst_agent)
    recommend_task = tasks.recommend(investment_advisor_agent)

    crew = Crew(
      agents=[
        research_analyst_agent,
        financial_analyst_agent,
        investment_advisor_agent
      ],
      tasks=[
        research_task,
        financial_task,
        filings_task,
        recommend_task
      ],
      verbose=True
    )

    result = crew.kickoff()
    return result

@app.route('/analyze', methods=['POST'])
def analyze():
  company = request.json.get('company')
  financial_crew = FinancialCrew(company)
  result = financial_crew.run()
  return jsonify(result)

if __name__ == "__main__":
  app.run(debug=True)

The above is a scenario example: I have encapsulated multiple agents using the Flask framework. During the running of the Flask program, if I want to dynamically add agents, such as research_analyst_agent2, financial_analyst_agent2, investment_advisor_agent2, how should I proceed?

masonzhang commented 1 month ago

yes, but you may need to complete the dynamic agent generation on your side.