Skip to content

Solar2004/DOP-Ensemble

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DOP-Ensemble

Divergent–Originality–Precision Ensemble Framework for Generative Language Models

DOP-Ensemble Architecture


Quick Start

CLI Usage

# Build
cargo build --release

# Run with Lunaris API (default)
dop-ensemble "Your prompt here"

# Run with OpenAI API
set OPENAI_API_KEY=your-key
dop-ensemble "Your prompt here" --api openai

📖 Full Documentation: See USAGE.md for detailed instructions on installation, configuration, and API setup.


Overview

DOP-Ensemble (Divergent–Originality–Precision Ensemble Framework) is a mathematical-algorithmic system designed to enhance creativity and precision in generative language models. Instead of relying solely on prompts, DOP-Ensemble implements a multi-stage pipeline that combines:

  • Divergent Generation: Producing multiple candidate responses with controlled stochasticity
  • Originality Scoring: Measuring uniqueness against generic patterns
  • Precision Enhancement: Applying RAG, fact-checking, and structured output enforcement

The system uses a composite creativity score to select the optimal output:

S = α·D + β·O + γ·P

Where:

  • D = Diversity (inter-response dissimilarity)
  • O = Originality (distance from generic templates)
  • P = Precision (factual accuracy + relevance)
  • α, β, γ = Weighted coefficients (configurable)

Mathematical Foundation

1. Creativity Score Model

Based on "A Mathematical Model to Enhance Creativity in Generative AI Systems", creativity is modeled as a multi-dimensional optimization problem:

Diversity (D)

Measures how distinct candidate responses are from each other:

D = (1 / N²) Σᵢ Σⱼ dist(embedding(rᵢ), embedding(rⱼ))

Where dist can be cosine distance, Euclidean distance, or BLEU score.

Originality (O)

Measures distance from "generic" response patterns:

O = 1 - similarity(rᵢ, generic_patterns)

Computed via embedding comparison against a corpus of baseline responses.

Precision (P)

Combines factual accuracy and task relevance:

P = w₁·accuracy + w₂·relevance + w₃·coherence

Where each component comes from:

  • Accuracy: Fact-checking against knowledge bases
  • Relevance: RAG-based context matching
  • Coherence: Logical flow evaluation

2. Ensemble Methods

Following "Harnessing Multiple Large Language Models: A Survey on LLM Ensemble":

  • Ensemble-during-inference: Multiple models generate in parallel
  • Ensemble-after-inference: Multiple candidates generated, then fused/selected

DOP-Ensemble implements ensemble-after-inference with scoring-based selection.

3. Sequential Monte Carlo Sampling

Inspired by "Ensembling Language Models with Sequential Monte Carlo", the framework can sample from an "ensemble distribution" combining uncertainty from multiple generation paths:

P(ensemble) ∝ Σᵢ wᵢ · P(response | model_i)

System Architecture

┌─────────────────────────────────────────────────────────────────┐
│                        USER INPUT                               │
└─────────────────────────────┬───────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                  DIVERGENT GENERATOR                            │
│  • Temperature-controlled sampling (T > 0)                     │
│  • Context perturbation for variety                            │
│  • Explicit diversity constraints                              │
│  Output: N candidate responses {r₁, r₂, ..., r_N}              │
└─────────────────────────────┬───────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                    METRIC COMPUTATION                           │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐           │
│  │  DIVERSITY   │  │  ORIGINALITY │  │  PRECISION   │           │
│  │  ANALYZER    │  │   SCORER     │  │   ENGINE     │           │
│  │              │  │              │  │              │           │
│  │  Embedding   │  │  vs Generic  │  │  +RAG        │           │
│  │  distances   │  │  patterns    │  │  +Fact Check │           │
│  │  Inter-sample│  │  Novelty     │  │  +Structure  │           │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘           │
│         │                 │                 │                    │
│         └────────────┬────┴────────────────┘                    │
│                      ▼                                          │
│         ┌─────────────────────┐                                 │
│         │  CREATIVITY SCORE   │                                 │
│         │  S = αD + βO + γP   │                                 │
│         └─────────┬───────────┘                                 │
└───────────────────┼─────────────────────────────────────────────┘
                    │
                    ▼
┌─────────────────────────────────────────────────────────────────┐
│                    SELECTOR / ENSEMBLE                         │
│  • Rank candidates by S                                        │
│  • Apply threshold filtering                                   │
│  • Optional: merge top-K fragments                             │
└─────────────────────────────┬───────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                      FINAL OUTPUT                               │
│  Selected response with highest creativity score               │
└─────────────────────────────────────────────────────────────────┘

Component Details

1. Divergent Generator

Purpose: Explore the "idea space" by generating diverse candidates

Techniques:

  • Stochastic Sampling: Use temperature > 0 and top_p > 0 to avoid mode collapse
  • Context Perturbation: Vary prompt framing per candidate
  • Diversity Constraints: Explicit instructions to avoid similar names/roles

Mathematical View: Sampling from a wider probability distribution:

P(sample) ∝ P(token | context)^(1/T)

Where T > 1 broadens the distribution.

2. Diversity Analyzer

Purpose: Quantify inter-response differences

Implementation:

  • Compute embeddings for all candidates
  • Calculate pairwise distances
  • Average or max-pool to get D score

3. Originality Scorer

Purpose: Penalize "generic" responses

Implementation:

  • Maintain a baseline corpus of typical responses
  • Compute similarity between each candidate and baseline
  • O = 1 - max(similarity) or O = mean(1 - similarity)

4. Precision Engine

Components:

RAG (Retrieval-Augmented Generation)

  • Search knowledge base for relevant context
  • Inject context before generation
  • Ensures grounded, specific outputs

Fact Checker

  • Validate claims against trusted sources
  • Flag inconsistencies

Structure Enforcer

  • Constrain output to JSON/schema format
  • Force specific fields (name, role, responsibilities)
  • Reduces vagueness

5. Selector / Ensemble

Strategies:

  1. Greedy: Return candidate with max S
  2. Threshold: Return all where S > threshold
  3. Merge: Combine best fragments from top-K

Usage Example

Rust (Core Library)

use dop_ensemble::{DOPEngine, Config, Schema};

let config = Config {
    alpha: 0.4,
    beta: 0.3, 
    gamma: 0.3,
    n_candidates: 5,
    temperature: 0.8,
};

let engine = DOPEngine::new(config);
let result = engine.generate(prompt, context, schema).await;

println!("Score: {}", result.score);

CLI (Recommended)

# Basic usage
dop-ensemble "Create a team of AI agents for marketing"

# With custom weights
dop-ensemble "Marketing agent system" \
  --alpha 0.5 --beta 0.3 --gamma 0.2 \
  --candidates 10

# With LLM merge strategy
dop-ensemble "Creative marketing campaign" --strategy llm-merge

💡 Tip: Use the CLI for quick experimentation. For programmatic access, use the Rust API.


Configuration

Parameter Type Default Description
alpha f32 0.33 Diversity weight
beta f32 0.33 Originality weight
gamma f32 0.34 Precision weight
n_candidates usize 5 Number of generated candidates
temperature f32 0.7 Sampling temperature
top_p f32 0.9 Nucleus sampling parameter
threshold f32 0.5 Minimum score to return
api_type ApiType Lunaris API backend (lunaris or openai)

Related Work

Core Papers

  1. "A Mathematical Model to Enhance Creativity in Generative AI Systems"

    • Introduces creativity score combining divergence + convergence
    • Models diversity, originality, and utility
  2. "Harnessing Multiple Large Language Models: A Survey on LLM Ensemble"

    • Taxonomy: ensemble-before/during/after inference
    • Voting, scoring, and merging strategies
  3. "Ensembling Language Models with Sequential Monte Carlo"

    • SMC sampling for combining model uncertainties
    • Better uncertainty quantification
  4. "Variance Reduction in Output from Generative AI"

    • Techniques to avoid "regression to generic"
    • Targeted variance injection
  5. "Creativity in LLM-based Multi-Agent Systems: A Survey"

    • Multi-agent creativity mechanisms
    • Divergent exploration + convergent selection

Complementary Techniques

  • RAG: Retrieval-Augmented Generation for grounding
  • Constrained Decoding: Schema-based output enforcement
  • PEFT: Parameter-efficient fine-tuning for specific domains

Installation

⚠️ Note: The Python package is planned for a future release. Currently, only the Rust core library is available.

Rust

cargo add dop-ensemble

Python (Coming Soon)

# pip install dop-ensemble  # Coming soon!

API Compatibility

DOP-Ensemble supports two API backends:

1. Lunaris API (Default)

The system is compatible with Lunaris AI API, a custom provider system.

Environment Variables:

# Lunaris API endpoint (default: http://127.0.0.1:8789)
AI_API_BASE_URL=http://127.0.0.1:8789

CLI Usage:

# Using Lunaris API (default)
dop-ensemble "Your prompt" --api lunaris

2. OpenAI API Compatible

The system is also compatible with any OpenAI API-compatible endpoint (OpenAI, Azure OpenAI, local models, etc.).

Environment Variables:

# OpenAI API base URL (default: https://api.openai.com/v1)
OPENAI_API_BASE_URL=https://api.openai.com/v1

# OpenAI API key
OPENAI_API_KEY=your_api_key_here

CLI Usage:

# Using OpenAI API compatible
dop-ensemble "Your prompt" --api openai

Configuration Example:

use dop_ensemble::{Config, ApiType};

let config = Config {
    api_type: ApiType::OpenAI,
    // ... other config options
};

CLI Usage

# Generate with default settings (uses Lunaris API)
dop-ensemble generate "Create a team of AI agents for e-commerce"

# Using OpenAI API compatible
dop-ensemble generate "Marketing agent system" --api openai

# Custom weights
dop-ensemble generate "Marketing agent system" \
  --alpha 0.5 --beta 0.3 --gamma 0.2 \
  --candidates 10

# With RAG context
dop-ensemble generate "Customer support agents" \
  --context-file knowledge.json \
  --schema agents.schema.json

# LLM Merge strategy (fusion ensemble)
dop-ensemble "Creative campaign" --strategy llm-merge

Output Schema

The system can enforce structured output:

{
  "agents": [
    {
      "name": "MarketingStrategist",
      "role": "Senior Marketing Strategist",
      "responsibilities": [
        "Develop comprehensive marketing strategies",
        "Analyze market trends and competitor positioning"
      ],
      "interactions": ["ContentCreator", "DataAnalyst"]
    }
  ],
  "metadata": {
    "score": 0.87,
    "diversity": 0.82,
    "originality": 0.79,
    "precision": 0.91
  }
}

Extensions

Reinforcement Learning (Advanced)

Train weights (α, β, γ) via RL to learn user preferences:

# Pseudo-code
rewards = []
for episode in range(episodes):
    response = engine.generate(prompt)
    reward = user_feedback(response)  # Thumbs up/down
    rewards.append(reward)
    
# Update weights via policy gradient
alpha, beta, gamma = update_weights(rewards)

Multi-Model Ensemble

Extend to multiple underlying LLMs:

models = ["gpt-4", "claude-3", "gemini-pro"]
candidates = []
for model in models:
    candidates.extend(generate(model, prompt, n=3))

License

Apache License 2.0 - See LICENSE file for details.


Citation

@misc{dop-ensemble,
  title = {DOP-Ensemble: Divergent–Originality–Precision Ensemble Framework},
  author = {DOP-Ensemble Authors},
  year = {2024},
  note = {Combining mathematical creativity scoring with multi-agent LLM ensembles},
  url = {https://github.com/Solar2004/dop-ensemble}
}

References

  • Wankhade et al. "A Mathematical Model to Enhance Creativity in Generative AI Systems"
  • "Harnessing Multiple Large Language Models: A Survey on LLM Ensemble"
  • "Ensembling Language Models with Sequential Monte Carlo"
  • "LLM Responses: How To Change, Control, And Improve"
  • "Variance Reduction in Output from Generative AI"
  • "Creativity in LLM-based Multi-Agent Systems: A Survey"

About

A Divergent–Originality–Precision Ensemble Framework for Generative Language Models

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages