Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs/add code generation example #132

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code_generation_crew/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=key_here
5 changes: 5 additions & 0 deletions code_generation_crew/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
.venv
venv
db
__pycache__
103 changes: 103 additions & 0 deletions code_generation_crew/README.md
Original file line number Diff line number Diff line change
@@ -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.
28 changes: 28 additions & 0 deletions code_generation_crew/agents.py
Original file line number Diff line number Diff line change
@@ -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,
)
31 changes: 31 additions & 0 deletions code_generation_crew/main.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions code_generation_crew/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
crewai==0.35.8
crewai-tools==0.4.6
17 changes: 17 additions & 0 deletions code_generation_crew/tasks.py
Original file line number Diff line number Diff line change
@@ -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,
)