# imsharad/openai-code-interpreter architecture

> Architecture diagram of the GitHub repository imsharad/openai-code-interpreter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/imsharad/openai-code-interpreter
- Repository: https://github.com/imsharad/openai-code-interpreter

## Overview

##### Main Components
- CLI/Entry Point: The secure_analyzer.py script acts as the command-line interface triggering the analysis process.
- Agents: The agent system is split into multiple agents (e.g., FileAccessAgent, PythonExecAgent) under resources/registry/agents, which manage specific tasks like file handling and code execution within Docker.
- Services: Located under resources/object_oriented_agents/services, these components (LanguageModelInterface, OpenAILanguageModel, OpenAIClientFactory) encapsulate interactions with the external OpenAI LLM.
- Docker Container: Represents the isolated execution environment as configured in resources/docker.
- Tools: The extensible tool interface (with implementations like file_access_tool.py and python_code_interpreter_tool.py) allows extension and customization of analysis features.
- Utilities: Logging (logger.py) and helper functions (openai_util.py) ensure maintainability and traceability.

##### Relationships and Interactions
- The CLI initiates an analysis request triggering:
- A file reading request via FileAccessAgent, which uses the ToolManager to perform secure file access.
- A request to PythonExecAgent for code generation and execution, which in turn interacts with the LLM service layer.
- The Docker container is involved throughout by providing the isolated environment needed when running file operations or executing generated code.
- Detailed logging is available throughout via the logger utility.

##### Architectural Patterns and Design Principles
- Object-Oriented Design: Agents inherit from a BaseAgent, and tools implement a common ToolInterface.
- Modular & Extensible Architecture: Clear separation allows for plugging in new tools or agents.
- Container Isolation: Docker is leveraged to ensure safe execution with strict security measures.
- Clean Separation of Concerns: Distinct directories for agents, services, utils, and registry components show tight decoupling from concerns such as file operations and external API calls.

##### Relevant Technologies and Frameworks
- Python 3.10+ as the main language.
- Docker for containerization.
- OpenAI’s language models (e.g., GPT-4 or o3-mini, as referenced in the README).
- Libraries such as pandas, numpy, matplotlib, and seaborn (for data analysis and visualization).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Layer
    subgraph "Entry Layer"
        CLI["CLI (secure_analyzer.py)"]
    end

    %% Agent Layer
    subgraph "Agent Layer"
        FAA["FileAccessAgent"]
        PEA["PythonExecAgent"]
        Base["BaseAgent (abstract)"]
    end

    %% Tool Layer
    subgraph "Tool Layer"
        FAT["FileAccessTool"]
        PCT["PythonCodeInterpreterTool"]
        TI["ToolInterface (abstract)"]
    end

    %% Service Layer
    subgraph "Service Layer"
        LMI["LanguageModelInterface"]
        OLM["OpenAI Language Model"]
        OCF["OpenAI Client Factory"]
    end

    %% Execution Layer
    subgraph "Execution Layer"
        DC["Docker Container"]
    end

    %% Utility Layer
    subgraph "Utility Layer"
        Logger["Logger"]
        OAU["OpenAI Util"]
    end

    %% Relationships
    CLI -->|"initiates"| FAA
    CLI -->|"initiates"| PEA

    FAA -->|"performs file read using"| FAT
    PEA -->|"executes code using"| PCT

    FAA -->|"logs"| Logger
    PEA -->|"logs"| Logger

    PEA -->|"calls"| LMI
    LMI -->|"routes request to"| OLM
    OLM -->|"utilizes"| OCF

    FAA -->|"executes in"| DC
    PEA -->|"executes in"| DC

    %% Utilities are used across the system
    LMI -->|"assists with"| OAU

    %% Inheritance and Interface relationships (informational - not directional)
    Base --- FAA
    Base --- PEA
    TI --- FAT
    TI --- PCT

    %% Click Events
    click CLI "https://github.com/imsharad/openai-code-interpreter/blob/main/secure_analyzer.py"
    click FAA "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/registry/agents/file_access_agent.py"
    click PEA "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/registry/agents/python_code_exec_agent.py"
    click LMI "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/object_oriented_agents/services/language_model_interface.py"
    click OLM "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/object_oriented_agents/services/openai_language_model.py"
    click OCF "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/object_oriented_agents/services/openai_factory.py"
    click DC "https://github.com/imsharad/openai-code-interpreter/tree/main/resources/docker"
    click FAT "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/registry/tools/file_access_tool.py"
    click PCT "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/registry/tools/python_code_interpreter_tool.py"
    click Logger "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/object_oriented_agents/utils/logger.py"
    click OAU "https://github.com/imsharad/openai-code-interpreter/blob/main/resources/object_oriented_agents/utils/openai_util.py"

    %% Styles
    class CLI cli
    class FAA,PEA agent
    class LMI,OLM,OCF service
    class FAT,PCT tool
    class DC docker
    class Logger,OAU util

    %% Class Definitions
    classDef cli fill:#ffebcd,stroke:#8b4513,stroke-width:2px;
    classDef agent fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef service fill:#ccffcc,stroke:#008000,stroke-width:2px;
    classDef tool fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef docker fill:#cce5ff,stroke:#0056b3,stroke-width:2px;
    classDef util fill:#e6ccff,stroke:#800080,stroke-width:2px;
```
