├── .env.sample ├── .gitignore ├── README.md ├── main.py ├── o1_outputs ├── Figure_1.png ├── Figure_2.png └── output__Create_a_report_on_the_evolution_of_labor_pro.md ├── requirements.txt └── setup.sh /.env.sample: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=XXX 2 | E2B_API_KEY=XXX -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | myenv 3 | __pycache__ 4 | tmp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenAI O1 Code Interpreter 2 | 3 | Simple hack to add code interpreting capabilities to OpenAI o1. It's able to display charts as outputs as well. 4 | 5 | All code execution runs on [e2b](https://e2b.dev); you can modify `#code_interpret` to run locally instead (not recommended for safety reasons). 6 | 7 | # Setup 8 | 9 | - Add API keys to `.env` based on `.env.sample` 10 | - Modify the code with a new prompt or pass it through the cli: `python main.py Create a report on the evolution of labor productivity in the United States, and what major technological shifts led to it` 11 | - Outputs are saved to `o1_outputs` folder 12 | 13 | TODOs: 14 | - When a chart is created, automatically save it and match the name in the report. Right now I save the charts manually in the folder. -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | import os 3 | import time 4 | import re 5 | from pydantic import BaseModel 6 | import dotenv 7 | import sys 8 | 9 | from openai import OpenAI 10 | from e2b_code_interpreter import CodeInterpreter, ProcessExitException 11 | import base64 12 | import io 13 | from PIL import Image 14 | import matplotlib.pyplot as plt 15 | 16 | dotenv.load_dotenv() 17 | 18 | client = OpenAI() 19 | 20 | class PythonNotebookCell(BaseModel): 21 | code: str 22 | pip_packages_required: List[str] 23 | 24 | def ask_openai(prompt, model='o1-preview',path=None): 25 | # Caching the output of the prompt to a file because o1 goes brrr 26 | if path is not None and os.path.exists(path): 27 | with open(path, "r") as f: 28 | return f.read() 29 | 30 | result = client.chat.completions.create( 31 | model=model, 32 | messages=[ 33 | {"role": "user", "content": prompt} 34 | ], 35 | ) 36 | 37 | return result.choices[0].message.content 38 | 39 | def extract_code(execution_plan): 40 | result = client.beta.chat.completions.parse( 41 | model="gpt-4o-mini", 42 | messages=[ 43 | {"role": "system", "content": "You are an expert software engineer that receives an execution plan, and then creates a single Python script that does everything in the plan. It will be executed in a single Python notebook cell." }, 44 | {"role": "user", "content": f"This is the plan I received, please write fully functional code that I can run in one notebook cell, and list all its dependencies: {execution_plan}"}, 45 | ], 46 | response_format=PythonNotebookCell 47 | ) 48 | 49 | return result.choices[0].message.parsed 50 | 51 | def code_interpret(sandbox: CodeInterpreter, code: str): 52 | print("Running code interpreter...") 53 | exec = sandbox.notebook.exec_cell( 54 | code, 55 | on_stderr=lambda stderr: print("[Code Interpreter]", stderr), 56 | on_stdout=lambda stdout: print("[Code Interpreter]", stdout), 57 | ) 58 | 59 | if exec.error: 60 | print("[Code Interpreter ERROR]", exec.error) 61 | else: 62 | return exec.results 63 | 64 | def display_png(png_data): 65 | image_data = base64.b64decode(png_data) 66 | 67 | # Create a BytesIO object 68 | image_buffer = io.BytesIO(image_data) 69 | 70 | # Open the image using PIL 71 | image = Image.open(image_buffer) 72 | 73 | # Display the image using matplotlib 74 | plt.imshow(image) 75 | plt.axis('off') # Turn off axis 76 | plt.show() 77 | 78 | def run_code(script: PythonNotebookCell): 79 | sandbox = CodeInterpreter(timeout=300) 80 | 81 | code_interpret(sandbox, "pip install " + " ".join(script.pip_packages_required)) 82 | 83 | code_to_run = script.code 84 | waiting_for_answer = True 85 | 86 | while waiting_for_answer: 87 | try: 88 | results = code_interpret(sandbox, code_to_run) 89 | waiting_for_answer = False 90 | except ProcessExitException as e: 91 | prompt = f""" 92 | Code Run: '{code_to_run}' 93 | Error: {str(e)} 94 | 95 | How can I fix this? 96 | """ 97 | 98 | solution = ask_openai( 99 | prompt, 100 | model="gpt-4o" 101 | ) 102 | 103 | script = extract_code(solution) 104 | 105 | code_to_run = script.code 106 | print(f"Suggested solution: {code_to_run}") 107 | 108 | print(results) 109 | for result in results: 110 | print(result) 111 | 112 | if hasattr(result, 'png'): 113 | display_png(result.png) 114 | 115 | def main(prompt = None): 116 | if prompt is None: 117 | prompt = """ 118 | I want to create a visualization of the growth of the Roman empire population and land under its control. 119 | Mark every important historical event along the way; if you don't have data on the population between two events, just keep it flat. 120 | """ 121 | 122 | # Remember the system it has code interpreter 123 | prompt = f""" 124 | {prompt} 125 | You have access to a code interpreter that can run python code; display the charts in the notebook. 126 | """ 127 | 128 | cleaned_prompt = re.sub(r'\W+', '_', prompt[:50]) 129 | filename = "output_" + cleaned_prompt + ".md" 130 | path = f"./o1_outputs/{filename}" 131 | 132 | output = ask_openai(prompt, path=path) 133 | 134 | # Saving those o1 outputs 135 | with open(path, "w") as f: 136 | f.write(output) 137 | 138 | code_to_run = extract_code(output) 139 | 140 | run_code(code_to_run) 141 | 142 | if __name__ == "__main__": 143 | if len(sys.argv) > 1: 144 | # Join all arguments after the script name into a single string 145 | prompt = " ".join(sys.argv[1:]) 146 | main(prompt) 147 | else: 148 | main() # Run with the default prompt -------------------------------------------------------------------------------- /o1_outputs/Figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FanaHOVA/openai-o1-code-interpreter/8ba0aea98db81ceee9f528baf75e1ae7e6024693/o1_outputs/Figure_1.png -------------------------------------------------------------------------------- /o1_outputs/Figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FanaHOVA/openai-o1-code-interpreter/8ba0aea98db81ceee9f528baf75e1ae7e6024693/o1_outputs/Figure_2.png -------------------------------------------------------------------------------- /o1_outputs/output__Create_a_report_on_the_evolution_of_labor_pro.md: -------------------------------------------------------------------------------- 1 | # The Evolution of Labor Productivity in the United States and the Impact of Technological Shifts 2 | 3 | ## Introduction 4 | 5 | Labor productivity, defined as the amount of goods and services produced per hour of labor, is a fundamental indicator of economic performance. It reflects the efficiency with which labor inputs are utilized in producing output and is crucial for long-term economic growth, wage increases, and improvements in living standards. In the United States, labor productivity has evolved significantly over time, influenced by various technological advancements, structural shifts in the economy, and changes in workforce dynamics. 6 | 7 | This report examines the historical evolution of labor productivity in the United States and explores the major technological shifts that have influenced its trajectory. By analyzing data trends and correlating them with technological milestones, we aim to understand how innovations have propelled productivity growth and shaped the modern economic landscape. 8 | 9 | --- 10 | 11 | ## Evolution of Labor Productivity in the United States 12 | 13 | To analyze the evolution of labor productivity, we'll use historical data from the **Bureau of Labor Statistics (BLS)**, specifically focusing on the nonfarm business sector, which accounts for a significant portion of the U.S. economy. 14 | 15 | ### Loading and Visualizing Labor Productivity Data 16 | 17 | We will start by importing the necessary libraries and loading the data: 18 | 19 | ```python 20 | import pandas as pd 21 | import matplotlib.pyplot as plt 22 | import seaborn as sns 23 | 24 | # Load labor productivity data from FRED (Federal Reserve Economic Data) 25 | # Nonfarm Business Sector: Labor Productivity (OPHNFB) 26 | 27 | url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=OPHNFB' 28 | df = pd.read_csv(url, parse_dates=['DATE']) 29 | df.rename(columns={'DATE': 'Date', 'OPHNFB': 'Labor Productivity'}, inplace=True) 30 | df.set_index('Date', inplace=True) 31 | 32 | # Display the first few rows of the data 33 | df.head() 34 | ``` 35 | 36 | Now, let's visualize the labor productivity over time: 37 | 38 | ```python 39 | plt.figure(figsize=(12,6)) 40 | sns.lineplot(data=df, x='Date', y='Labor Productivity') 41 | plt.title('Labor Productivity in the United States (Nonfarm Business Sector)') 42 | plt.ylabel('Index (2012=100)') 43 | plt.xlabel('Year') 44 | plt.show() 45 | ``` 46 | 47 | **Figure 1: Labor Productivity in the United States (1947 - Present)** 48 | 49 | ![Labor Productivity Over Time](Figure_1.png) 50 | 51 | --- 52 | 53 | ### Discussion 54 | 55 | The chart above illustrates the growth of labor productivity in the United States nonfarm business sector from 1947 to the present. The overall upward trend reflects the nation's increasing efficiency in producing goods and services. However, the rate of growth has varied over different periods, influenced by economic cycles and technological innovations. 56 | 57 | --- 58 | 59 | ## Major Technological Shifts and Their Impact 60 | 61 | Technological advancements have been a primary driver of labor productivity growth. Below, we explore key technological shifts and their impact on productivity. 62 | 63 | ### 1. Post-War Industrial Expansion (1940s - 1960s) 64 | 65 | - **Technological Advances**: Post-World War II era saw significant industrial expansion, with advancements in manufacturing technologies, transportation, and infrastructure development. 66 | - **Impact**: Increased automation, adoption of assembly lines, and improved industrial processes led to substantial productivity gains. 67 | 68 | ### 2. Information Technology Revolution (1970s - 1990s) 69 | 70 | - **Technological Advances**: Introduction of computers, semiconductors, and information systems revolutionized business operations. 71 | - **Impact**: Automation of administrative tasks, improved communication, and data processing capabilities enhanced efficiency across industries. 72 | 73 | ### 3. Internet and Digitalization (1990s - 2000s) 74 | 75 | - **Technological Advances**: Widespread adoption of the internet, e-commerce, and digital communication tools. 76 | - **Impact**: Enabled global connectivity, streamlined supply chains, and created new business models, contributing to productivity improvements. 77 | 78 | ### 4. Advanced Technologies and Automation (2010s - Present) 79 | 80 | - **Technological Advances**: Growth of artificial intelligence, machine learning, robotics, and advanced manufacturing. 81 | - **Impact**: Potential for significant productivity gains through automation of complex tasks, though actual productivity growth has been modest compared to previous periods. 82 | 83 | --- 84 | 85 | ## Analyzing Productivity Growth Rates 86 | 87 | To better understand how these technological shifts have impacted productivity, let's analyze the annual growth rates of labor productivity. 88 | 89 | ### Calculating and Visualizing Annual Growth Rates 90 | 91 | ```python 92 | # Calculate annual growth rates 93 | df['Annual Growth Rate'] = df['Labor Productivity'].pct_change(periods=4) * 100 # 4 quarters = 1 year 94 | 95 | # Plot the annual growth rates 96 | plt.figure(figsize=(12,6)) 97 | sns.lineplot(data=df, x='Date', y='Annual Growth Rate') 98 | plt.title('Annual Growth Rate of Labor Productivity in the United States') 99 | plt.ylabel('Annual Growth Rate (%)') 100 | plt.xlabel('Year') 101 | plt.axhline(0, color='black', linewidth=0.8) 102 | plt.show() 103 | ``` 104 | 105 | **Figure 2: Annual Growth Rate of Labor Productivity (1948 - Present)** 106 | 107 | ![Annual Growth Rate](Figure_2.png) 108 | 109 | --- 110 | 111 | ### Discussion 112 | 113 | The annual growth rate chart highlights periods of rapid productivity growth and periods of stagnation or decline. Notable observations include: 114 | 115 | - **1950s - 1960s**: High growth rates corresponding with post-war industrial expansion. 116 | - **1970s - Early 1980s**: Declining growth rates, possibly due to oil shocks and economic recessions. 117 | - **Mid-1990s - Early 2000s**: Increase in growth rates aligning with the IT revolution and internet adoption. 118 | - **Post-2000s**: Overall decline in productivity growth rates despite advancements in technology, leading to debates about the productivity paradox. 119 | 120 | --- 121 | 122 | ## Recent Trends and Future Outlook 123 | 124 | ### The Productivity Paradox 125 | 126 | Despite rapid technological advancements in recent years, productivity growth has been relatively modest. This phenomenon, known as the **productivity paradox**, raises questions about why technology hasn't translated into expected productivity gains. 127 | 128 | **Potential Explanations:** 129 | 130 | - **Lag in Technology Adoption**: It may take time for businesses to fully integrate and leverage new technologies. 131 | - **Skills Gap**: Workforce may lack the necessary skills to utilize advanced technologies effectively. 132 | - **Measurement Challenges**: Traditional metrics may not capture productivity gains in the digital economy accurately. 133 | - **Concentration of Benefits**: Productivity gains may be concentrated in specific sectors or firms, not reflected broadly across the economy. 134 | 135 | ### Future Outlook 136 | 137 | Emerging technologies hold the promise of boosting productivity: 138 | 139 | - **Artificial Intelligence and Machine Learning**: Potential to automate complex tasks, enhance decision-making, and create new products and services. 140 | - **Automation and Robotics**: Increased efficiency in manufacturing, logistics, and even service industries. 141 | - **Digital Transformation**: Integration of digital technologies into all areas of business, fundamentally changing how companies operate and deliver value. 142 | 143 | **Policy Considerations:** 144 | 145 | - **Investing in Education and Training**: Equip the workforce with skills needed for the digital economy. 146 | - **Encouraging Innovation**: Support research and development to spur technological advancements. 147 | - **Infrastructure Development**: Enhance digital infrastructure to facilitate technology adoption. 148 | 149 | --- 150 | 151 | ## Conclusion 152 | 153 | The evolution of labor productivity in the United States reflects the complex interplay between technological innovation, economic factors, and workforce capabilities. Major technological shifts have historically led to significant productivity gains, driving economic growth and improving living standards. However, recent trends highlight challenges in translating technological advancements into productivity growth, emphasizing the need for policies that support technology adoption, workforce development, and innovation. 154 | 155 | Understanding past patterns can inform strategies to harness future technologies' potential, ensuring that productivity gains contribute to broad-based economic prosperity. 156 | 157 | --- 158 | 159 | ## References 160 | 161 | - Bureau of Labor Statistics (BLS): [Productivity and Costs](https://www.bls.gov/lpc/) 162 | - Federal Reserve Economic Data (FRED): [Nonfarm Business Sector: Labor Productivity](https://fred.stlouisfed.org/series/OPHNFB) 163 | - Gordon, R. J. (2016). *The Rise and Fall of American Growth: The U.S. Standard of Living since the Civil War*. Princeton University Press. 164 | - Brynjolfsson, E., & McAfee, A. (2014). *The Second Machine Age: Work, Progress, and Prosperity in a Time of Brilliant Technologies*. W. W. Norton & Company. 165 | 166 | --- -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | e2b 3 | e2b-code-interpreter 4 | python-dotenv -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | python3 -m venv myenv 2 | source myenv/bin/activate # Activate the virtual environment 3 | pip install -r requirements.txt --------------------------------------------------------------------------------