diff --git a/code_generation_crew/.env.example b/code_generation_crew/.env.example new file mode 100644 index 00000000..4fe70328 --- /dev/null +++ b/code_generation_crew/.env.example @@ -0,0 +1 @@ +OPENAI_API_KEY=key_here diff --git a/code_generation_crew/.gitignore b/code_generation_crew/.gitignore new file mode 100644 index 00000000..e81087b0 --- /dev/null +++ b/code_generation_crew/.gitignore @@ -0,0 +1,5 @@ +.env +.venv +venv +db +__pycache__ diff --git a/code_generation_crew/README.md b/code_generation_crew/README.md new file mode 100644 index 00000000..f3840928 --- /dev/null +++ b/code_generation_crew/README.md @@ -0,0 +1,103 @@ +# AI Crew - Code Generation Crew +## Introduction +This project is an example using the CrewAI framework to run agents that generate code, run it, and report the results. The Agent with `enable_code_execution=True` will be responsible for creating the code based on the tasks goal. The code will be executed and then the output will be passed to the reporting analyst. + + +#### Code Generation Crew + +- [CrewAI Framework](#crewai-framework) +- [Requirements](#requirements) +- [Running the script](#running-the-script) +- [Details & Explanation](#details--explanation) +- [License](#license) + + +## CrewAI Framework +CrewAI is designed to facilitate the collaboration of role-playing AI agents. In this example, these agents work together to give a complete stock analysis and investment recommendation. + +## Requirements + +Docker is required to run the script. If you don't have Docker installed, you can download it [here](https://www.docker.com/products/docker-desktop). + +## Running the Script +- **Configure Environment**: Copy ``.env.example` and set up the environment variables for OpenAI. +- **Install Dependencies**: Run `pip install -r requirements.txt`. +- **Execute the Script**: Run `python main.py` and input your idea on the tasks. + +## Details & Explanation +- **Running the Script**: Execute `python main.py`` and the crew will kickoff bringing the last 30 days of stocks from APPL and NVDA but the use-case here is to show-up the Code Intepreter ability of the Anget. + +The logic will take advantage of the structure that CrewAI provides, so that the agent can create the code and execute it. +After executing the code created by the agent himself, if for some reason it was not correct, the output was passed on to the same agent so that he can correct the code and run it again, after being executed correctly and the output will be passed on to the report analyst. + + +- **Key Components**: + - `./main.py`: Main script file. + - `./tasks.py`: Main file with the tasks prompts. + - `./agents.py`: Main file with the agents creation. + + +## Example + +In the example below, the agent created the code to fetch the last 30 days of stock prices for APPL and NVDA, run it, and printed the results so it can be sent to the reporting analyst. + +```bash +Action: Code Interpreter +Action Input: {"code": "import yfinance as yf\nimport pandas as pd\nfrom datetime import datetime, timedelta\n\n# Define the time range\nend_date = datetime.now()\nstart_date = end_date - timedelta(days=30)\n\n# Fetch stock data\nappl_data = yf.download('AAPL', start=start_date, end=end_date)\nnvda_data = yf.download('NVDA', start=start_date, end=end_date)\n\n# Get the last 30 days of stock prices\nappl_prices = appl_data['Close']\nnvda_prices = nvda_data['Close']\n\n# Print the stock prices\nprint('APPL Stock Prices (Last 30 Days):')\nprint(appl_prices)\nprint('\\nNVDA Stock Prices (Last 30 Days):')\nprint(nvda_prices)", "dependencies_used_in_code": ["yfinance", "pandas"]} + +[*********************100%%**********************] 1 of 1 completed +[*********************100%%**********************] 1 of 1 completed +APPL Stock Prices (Last 30 Days): +Date +2024-06-04 194.350006 +2024-06-05 195.869995 +2024-06-06 194.479996 +2024-06-07 196.889999 +2024-06-10 193.119995 +2024-06-11 207.149994 +2024-06-12 213.070007 +2024-06-13 214.240005 +2024-06-14 212.490005 +2024-06-17 216.669998 +2024-06-18 214.289993 +2024-06-20 209.679993 +2024-06-21 207.490005 +2024-06-24 208.139999 +2024-06-25 209.070007 +2024-06-26 213.250000 +2024-06-27 214.100006 +2024-06-28 210.619995 +2024-07-01 216.750000 +2024-07-02 220.270004 +2024-07-03 221.550003 +Name: Close, dtype: float64 + +NVDA Stock Prices (Last 30 Days): +Date +2024-06-04 116.436996 +2024-06-05 122.440002 +2024-06-06 120.998001 +2024-06-07 120.888000 +2024-06-10 121.790001 +2024-06-11 120.910004 +2024-06-12 125.199997 +2024-06-13 129.610001 +2024-06-14 131.880005 +2024-06-17 130.979996 +2024-06-18 135.580002 +2024-06-20 130.779999 +2024-06-21 126.570000 +2024-06-24 118.110001 +2024-06-25 126.089996 +2024-06-26 126.400002 +2024-06-27 123.989998 +2024-06-28 123.540001 +2024-07-01 124.300003 +2024-07-02 122.669998 +2024-07-03 128.279999 +Name: Close, dtype: float64 +``` + + +## License +This project is released under the MIT License. diff --git a/code_generation_crew/agents.py b/code_generation_crew/agents.py new file mode 100644 index 00000000..7c82df9b --- /dev/null +++ b/code_generation_crew/agents.py @@ -0,0 +1,28 @@ +from crewai_tools.tools import CodeInterpreterTool + +from crewai import Agent + +code_interpreter_tool = CodeInterpreterTool() + + +class Agents: + def quant_financial_agent(self): + return Agent( + role="Quant Financial Engineer", + goal="Analyze the provided information and if needed write code and run it to generate insights.", + backstory="Expert in analyzing company cultures and identifying key values and needs from various sources, including websites and brief descriptions.", + verbose=True, + allow_code_execution=True, # This allows the Agent to generate code and run it + allow_delegation=False, + ) + + def reporting_analyst(self): + return Agent( + role="Reporting Analyst", + goal="Create detailed reports based on the topic provided data analysis and research findings", + tools=[], + backstory="""You're a meticulous analyst with a keen eye for detail. You're known for + your ability to turn complex data into clear and concise reports, making + it easy for others to understand and act on the information you provide.""", + verbose=True, + ) diff --git a/code_generation_crew/main.py b/code_generation_crew/main.py new file mode 100644 index 00000000..1c7ebef2 --- /dev/null +++ b/code_generation_crew/main.py @@ -0,0 +1,31 @@ +from agents import Agents +from tasks import Tasks + +from crewai import Crew + +tasks = Tasks() +agents = Agents() + +# Create Agents +researcher_agent = agents.quant_financial_agent() +reporting_analyst_agent = agents.reporting_analyst() + +# Define Tasks for each agent +research_company_culture_task = tasks.research_company_stocks(researcher_agent) +summarize_stock_task = tasks.summarize_stock_information(researcher_agent) + +# Instantiate the crew with a sequential process +crew = Crew( + agents=[researcher_agent, reporting_analyst_agent], + tasks=[ + research_company_culture_task, + summarize_stock_task, + ], +) + +# Kick off the process +result = crew.kickoff() + +print("Financial Analysis Process Completed.") +print("Final Financial Analysis:") +print(result) diff --git a/code_generation_crew/requirements.txt b/code_generation_crew/requirements.txt new file mode 100644 index 00000000..b2907fb3 --- /dev/null +++ b/code_generation_crew/requirements.txt @@ -0,0 +1,2 @@ +crewai==0.35.8 +crewai-tools==0.4.6 diff --git a/code_generation_crew/tasks.py b/code_generation_crew/tasks.py new file mode 100644 index 00000000..a3eb45b0 --- /dev/null +++ b/code_generation_crew/tasks.py @@ -0,0 +1,17 @@ +from crewai import Task + + +class Tasks: + def research_company_stocks(self, agent): + return Task( + description="Research the company's APPL and NVDA for the last 30 days, stock performance, financial health, and market position. Analyze the company's stock price, market capitalization, revenue, and profit margins. Identify any recent news, events, or trends that may impact the company's stock price.", + expected_output="A detailed analysis of the company's stock performance, financial health, and market position, including key metrics and recent news.", + agent=agent, + ) + + def summarize_stock_information(self, agent): + return Task( + description="Summarize the key information from the company's stock analysis, including the stock performance, financial health, and market position. Provide a brief overview of the company's stock price, market capitalization, revenue, and profit margins. Highlight any recent news, events, or trends that may impact the company's stock price.", + expected_output="A concise summary of the company's stock analysis, including key metrics.", + agent=agent, + )