# rishit-singh/tinytune architecture

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

- Interactive diagram: https://gitdiagram.com/rishit-singh/tinytune
- Repository: https://github.com/rishit-singh/tinytune

## Overview

##### Project Type and Purpose
- TinyTune is an open‑source Python library for prompt engineering and workflow orchestration over Large Language Models (LLMs).
- It exposes modular “prompt jobs,” “pipelines,” and “tools” so users can build, reuse, and chain LLM‐driven tasks in a clean, model‑agnostic way.

##### File Structure Analysis
- Top‑level:
- README.md, pyproject.toml, LICENSE – project metadata & packaging.
- docs/ – full documentation site (core concepts, API reference, advanced usage).
- examples/ – sample scripts showing real‑world usage.
- src/tinytune/ – core library code:
– contexts/ (LLMContext implementations)
– llmcontext.py (abstract base)
– pipeline.py (Pipeline orchestration)
– prompt.py & util/prompt.py (Message, prompt_job decorator)
– tool.py (tool decorator and execution hooks)
- Config files: pyrightconfig.json (type checking), _gitignore.

##### Key Components
a. User Application Layer
- User code (examples/** or consumer app)
- Defines:
- An LLMContext instance (e.g., GPTContext)
- @tool‑decorated Python functions
- @prompt_job‑decorated prompt jobs
- A Pipeline that chains prompt jobs
b. TinyTune Core Library
- Contexts Module: LLMContext base + concrete subclasses (GPTContext, OllamaContext, etc.)
- Prompt Module: decorator @prompt_job, Message data model
- Pipeline Module: orchestrates job sequence, passes prevResult
- Tool Module: decorator @tool, metadata registry, invocation mechanism
- Util Module: shared helpers (e.g., prompt templating)
c. External Services
- Third‑party LLM provider APIs (OpenAI, Groq, Replicate, etc.)
- Any external systems invoked by user‑defined tools (databases, web APIs)

##### Relationships & Data Flow
1. User App → Instantiates an LLMContext.
2. User App → Decorates functions as tools and prompt jobs.
3. Pipeline (user code) invokes Pipeline.Run():
a. For each prompt job:
i. Pipeline calls job(context, prevResult).
ii. Prompt job uses context.Prompt(Message).Run() → HTTP request to LLM API.
iii. LLM API returns response → context captures as Message object.
iv. Pipeline sets prevResult ← response → next job.
b. If a prompt job invokes a tool:
i. TinyTune’s tool registry intercepts call.
ii. Runs user function locally or posts to external service.
iii. Returns tool output into prompt/job.
4. Final output returned to user app.

##### Architectural Patterns & Principles
- Plugin / Decorator Pattern: @prompt_job and @tool annotations register user code.
- Pipeline / Chain of Responsibility: sequential job execution with shared context.
- Adapter Pattern for LLMContext: abstracts over multiple LLM backends.
- Separation of Concerns: user code vs. core orchestration vs. external API calls.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Application Layer
    subgraph "User Application Layer"
        examples_main["main.py"]:::user
        examples_bot["bot.py"]:::user
        examples_ctx["gptcontext.py"]:::user
    end

    %% TinyTune Core Library
    subgraph "TinyTune Core Library"
        subgraph "Contexts Module"
            llm_base["LLMContext<br/>(llmcontext.py)"]:::library
            ctx_gpt["GPTContext<br/>(contexts/gptcontext.py)"]:::library
        end
        subgraph "Prompt Module"
            prompt_core["prompt.py"]:::library
        end
        subgraph "Pipeline Module"
            pipeline_mod["pipeline.py"]:::library
        end
        subgraph "Tool Module"
            tool_mod["tool.py"]:::library
        end
        subgraph "Util Module"
            util_init["__init__.py<br/>(util/)"]:::library
            util_prompt["prompt.py<br/>(util/)"]:::library
        end
    end

    %% External Services
    ExternalLLM[(External LLM<br/>Services)]:::external
    ExternalTools[(External Tool<br/>Services)]:::external

    %% Connections: User App to Core
    examples_ctx -->|"inherits"| ctx_gpt
    ctx_gpt -->|"implements base"| llm_base

    examples_main -->|"defines @prompt_job"| prompt_core
    examples_main -->|"defines @tool"| tool_mod
    examples_main -->|"builds Pipeline"| pipeline_mod

    %% Pipeline execution flow
    pipeline_mod -->|"calls prompt_job(context, prevResult)"| prompt_core
    prompt_core -->|"uses context.Prompt().Run()"| llm_base
    llm_base -->|"sends HTTP request"| ExternalLLM
    ExternalLLM -->|"returns response"| llm_base
    llm_base -->|"returns Message"| pipeline_mod

    %% Tool invocation flow
    prompt_core -->|"invokes tool call"| tool_mod
    tool_mod -->|"executes local or calls"| ExternalTools
    ExternalTools -->|"returns tool output"| tool_mod
    tool_mod -->|"returns output"| prompt_core

    %% Util usage
    prompt_core --> util_prompt
    pipeline_mod --> util_init
    tool_mod --> util_init

    %% Click Events
    click examples_main "https://github.com/rishit-singh/tinytune/blob/main/examples/main.py"
    click examples_bot "https://github.com/rishit-singh/tinytune/blob/main/examples/bot.py"
    click examples_ctx "https://github.com/rishit-singh/tinytune/blob/main/examples/gptcontext.py"
    click llm_base "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/llmcontext.py"
    click ctx_gpt "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/contexts/gptcontext.py"
    click prompt_core "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/prompt.py"
    click util_prompt "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/util/prompt.py"
    click pipeline_mod "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/pipeline.py"
    click tool_mod "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/tool.py"
    click util_init "https://github.com/rishit-singh/tinytune/blob/main/src/tinytune/util/__init__.py"

    %% Styles
    classDef user fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef library fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px;
```
