# kennethreitz/simplemind architecture

> Architecture diagram of the GitHub repository kennethreitz/simplemind, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kennethreitz/simplemind
- Repository: https://github.com/kennethreitz/simplemind

## Overview

##### Main Components

a) Core Components:
- Client Interface Layer
- Provider Abstraction Layer
- Plugin System
- Tool/Function Calling System
- Memory Management
- Logging System

b) External Components:
- Multiple LLM Providers (OpenAI, Anthropic, Amazon, Gemini, etc.)
- External Tools/Functions
- Logging Service (Logfire)

**┌─ Application Layer ─────────────────────────────────────┐**
- Client Code                                          │
- Examples & Use Cases                                 │
- ┘
- ↓
**┌─ Public API Layer ──────────────────────────────────────┐**
- generate_text()                                      │
- generate_data()                                      │
- create_conversation()                                │
- Session Management                                   │
- ┘
- ↓
**┌─ Core Features Layer ───────────────────────────────────┐**
- ┌─ Plugin System ─┐ ┌─ Tool System ┐ ┌─ Memory System ┐│

- ┘ └──────────────┘ └────────────────┘│
- ┘
- ↓
**┌─ Provider Abstraction Layer ──────────────────────────┐**
- Base Provider Interface                             │
- Provider-specific Implementations                   │
- Model Management                                    │
- ┘
- ↓
**┌─ External Services Layer ────────────────────────────────┐**
- ┌─ LLM Providers ─┐ ┌─ External Tools ┐ ┌─ Logging ─┐  │
- OpenAI       │ │ - Custom Tools   │ │ - Logfire │  │
- Anthropic    │ │ - Functions      │ └──────────┘   │
- Amazon       │ └────────────────┘                  │
- Gemini       │                                     │
- Others       │                                     │
- ┘                                      │
- ┘

##### Important Relationships

- Bidirectional flow between the Provider Abstraction Layer and External Services
- Plugin system's interaction with the core conversation flow
- Tool system's integration with external functions
- Memory system's persistence across conversations
- Logging system's cross-cutting nature

##### Key Design Patterns to Highlight

- Factory Pattern: For provider creation
- Strategy Pattern: For different LLM implementations
- Observer Pattern: For plugin system
- Adapter Pattern: For provider integration
- Builder Pattern: For conversation construction

##### Implementation Details

- How `simplemind/providers` contains different provider implementations
- How the plugin system (`BasePlugin`) integrates with conversations
- Demonstrate the tool calling system's integration with Python functions
- Session management system for maintaining defaults

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Application
        Client[Client Code]
        Examples[Examples & Use Cases]:::examples
    end

    subgraph PublicAPI
        GenText["generate_text()"]
        GenData["generate_data()"]
        CreateConv["create_conversation()"]
        Session["Session Management"]:::core
    end

    subgraph CoreFeatures
        direction TB
        PluginSystem["Plugin System"]:::plugin
        ToolSystem["Tool System"]:::tool
        MemorySystem["Memory System"]:::memory
    end

    subgraph ProviderLayer["Provider Abstraction Layer"]
        BaseProvider["Base Provider Interface"]:::provider
        ModelMgmt["Model Management"]:::provider
    end

    subgraph ExternalServices
        direction TB
        subgraph LLMProviders
            OpenAI["OpenAI"]:::provider
            Anthropic["Anthropic"]:::provider
            Amazon["Amazon"]:::provider
            Gemini["Gemini"]:::provider
            Groq["Groq"]:::provider
            Ollama["Ollama"]:::provider
            XAI["XAI"]:::provider
        end

        subgraph ExternalTools
            CustomTools["Custom Tools"]:::tool
            Functions["Functions"]:::tool
        end

        subgraph LoggingServices
            Logfire["Logfire"]:::logging
        end
    end

    %% Connections
    Client --> GenText & GenData & CreateConv
    Examples --> GenText & GenData & CreateConv
    GenText & GenData & CreateConv --> Session
    Session --> PluginSystem & ToolSystem & MemorySystem
    PluginSystem & ToolSystem & MemorySystem --> BaseProvider
    BaseProvider --> ModelMgmt
    ModelMgmt --> LLMProviders
    ToolSystem --> ExternalTools
    BaseProvider --> LoggingServices

    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef provider fill:#4CAF50,stroke:#2E7D32,color:white
    classDef plugin fill:#FFC107,stroke:#FFA000,color:black
    classDef tool fill:#FF9800,stroke:#F57C00,color:white
    classDef memory fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef logging fill:#607D8B,stroke:#455A64,color:white
    classDef examples fill:#E91E63,stroke:#C2185B,color:white

    %% Click Events
    click BaseProvider "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/_base.py"
    click ToolSystem "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/_base_tools.py"
    click LoggingServices "https://github.com/kennethreitz/simplemind/blob/main/simplemind/logging.py"
    click Session "https://github.com/kennethreitz/simplemind/blob/main/simplemind/models.py"
    click ModelMgmt "https://github.com/kennethreitz/simplemind/blob/main/simplemind/settings.py"
    click OpenAI "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/openai.py"
    click Anthropic "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/anthropic.py"
    click Amazon "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/amazon.py"
    click Gemini "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/gemini.py"
    click Groq "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/groq.py"
    click Ollama "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/ollama.py"
    click XAI "https://github.com/kennethreitz/simplemind/blob/main/simplemind/providers/xai.py"
    click Examples "https://github.com/kennethreitz/simplemind/tree/main/examples"
    click PluginSystem "https://github.com/kennethreitz/simplemind/tree/main/examples"
    click MemorySystem "https://github.com/kennethreitz/simplemind/tree/main/examples"
    click CustomTools "https://github.com/kennethreitz/simplemind/blob/main/examples/tool_calling.py"
    click Functions "https://github.com/kennethreitz/simplemind/blob/main/simplemind/utils.py"
```
