Version 1.4.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# AI Functions Reference
|
||||
|
||||
**Last Verified:** December 25, 2025
|
||||
**Last Verified:** January 5, 2026
|
||||
|
||||
---
|
||||
|
||||
@@ -8,13 +8,20 @@
|
||||
|
||||
IGNY8's AI engine provides functions for content planning and generation. Located in `backend/igny8_core/ai/`.
|
||||
|
||||
**Providers:**
|
||||
- **OpenAI** - GPT-4 for text, DALL-E 3 for images
|
||||
- **Runware** - Alternative image generation
|
||||
**Providers (v1.4.0):**
|
||||
- **OpenAI** - GPT-4 for text, DALL-E 3 for images (via `IntegrationProvider`)
|
||||
- **Anthropic** - Claude models for text (via `IntegrationProvider`)
|
||||
- **Runware** - Alternative image generation (via `IntegrationProvider`)
|
||||
|
||||
**New in v1.4.0:**
|
||||
- Provider API keys stored in `IntegrationProvider` model
|
||||
- Model configurations stored in `AIModelConfig` model
|
||||
- System defaults stored in `SystemAISettings` singleton
|
||||
- Credit-based pricing per model via `AIModelConfig.tokens_per_credit` / `credits_per_image`
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
## Architecture (Updated v1.4.0)
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
@@ -25,12 +32,20 @@ IGNY8's AI engine provides functions for content planning and generation. Locate
|
||||
│ │ AIEngine │───►│ Function │───►│ Provider │ │
|
||||
│ │ (router) │ │ (logic) │ │ (API) │ │
|
||||
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
||||
│ │ ▲ │
|
||||
│ │ │ │
|
||||
│ ▼ │ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
||||
│ │ModelRegistry │───►│AIModelConfig │───►│Integration │ │
|
||||
│ │ (service) │ │ (database) │ │ Provider │ │
|
||||
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
||||
│ │
|
||||
│ Functions: │
|
||||
│ • AutoClusterKeywords │
|
||||
│ • GenerateContentIdeas │
|
||||
│ • GenerateContent │
|
||||
│ • GenerateImages │
|
||||
│ • GenerateImagePrompts │
|
||||
│ • OptimizeContent (pending) │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
@@ -38,6 +53,38 @@ IGNY8's AI engine provides functions for content planning and generation. Locate
|
||||
|
||||
---
|
||||
|
||||
## Model Registry (NEW v1.4.0)
|
||||
|
||||
**Location:** `backend/igny8_core/ai/model_registry.py`
|
||||
|
||||
Central registry for AI model configurations with caching.
|
||||
|
||||
```python
|
||||
from igny8_core.ai.model_registry import ModelRegistry
|
||||
|
||||
# Get model configuration
|
||||
model = ModelRegistry.get_model('gpt-4o-mini')
|
||||
|
||||
# Get pricing rate
|
||||
input_rate = ModelRegistry.get_rate('gpt-4o-mini', 'input')
|
||||
|
||||
# Calculate cost
|
||||
cost = ModelRegistry.calculate_cost('gpt-4o-mini', input_tokens=1000, output_tokens=500)
|
||||
|
||||
# Get API key for provider
|
||||
api_key = ModelRegistry.get_api_key('openai')
|
||||
|
||||
# Get default model
|
||||
default_text = ModelRegistry.get_default_model('text')
|
||||
default_image = ModelRegistry.get_default_model('image')
|
||||
|
||||
# List available models
|
||||
text_models = ModelRegistry.list_models(model_type='text')
|
||||
image_models = ModelRegistry.list_models(model_type='image', provider='runware')
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## AIEngine
|
||||
|
||||
**Location:** `backend/igny8_core/ai/engine.py`
|
||||
@@ -48,9 +95,9 @@ Main orchestrator for AI operations.
|
||||
class AIEngine:
|
||||
def __init__(self, account: Account):
|
||||
self.account = account
|
||||
self.settings = self._load_settings()
|
||||
self.text_provider = self._init_text_provider()
|
||||
self.image_provider = self._init_image_provider()
|
||||
self.settings = self._load_settings() # Uses SystemAISettings + AccountSettings
|
||||
self.text_provider = self._init_text_provider() # Uses ModelRegistry
|
||||
self.image_provider = self._init_image_provider() # Uses ModelRegistry
|
||||
|
||||
def auto_cluster(self, keywords: List[Keyword]) -> List[Cluster]:
|
||||
"""Cluster keywords by topic"""
|
||||
@@ -61,8 +108,8 @@ class AIEngine:
|
||||
def generate_content(self, task: Task) -> Content:
|
||||
"""Generate full article content"""
|
||||
|
||||
def generate_images(self, content: Content, count: int = 1) -> List[ContentImage]:
|
||||
"""Generate images for content"""
|
||||
def generate_images(self, content: Content, count: int = 1, quality_tier: str = 'quality') -> List[ContentImage]:
|
||||
"""Generate images for content with quality tier selection (v1.4.0)"""
|
||||
```
|
||||
|
||||
---
|
||||
@@ -103,9 +150,9 @@ class AIEngine:
|
||||
|
||||
**Prompt Template:** `auto_cluster`
|
||||
|
||||
**Model:** GPT-4
|
||||
**Model:** Default text model (from `AIModelConfig.is_default`)
|
||||
|
||||
**Credit Cost:** 1 idea credit per operation
|
||||
**Credit Cost:** Based on `AIModelConfig.tokens_per_credit` for model used
|
||||
|
||||
---
|
||||
|
||||
@@ -146,9 +193,9 @@ class AIEngine:
|
||||
|
||||
**Prompt Template:** `generate_ideas`
|
||||
|
||||
**Model:** GPT-4
|
||||
**Model:** Default text model (from `AIModelConfig.is_default`)
|
||||
|
||||
**Credit Cost:** 1 idea credit per idea generated
|
||||
**Credit Cost:** Based on `AIModelConfig.tokens_per_credit` for model used
|
||||
|
||||
---
|
||||
|
||||
@@ -174,7 +221,9 @@ class AIEngine:
|
||||
"target_word_count": 2000,
|
||||
"include_headings": True,
|
||||
"include_lists": True,
|
||||
"include_code_blocks": True
|
||||
"include_code_blocks": True,
|
||||
"temperature": 0.7, # From SystemAISettings or AccountSettings override
|
||||
"max_tokens": 8192 # From SystemAISettings or AccountSettings override
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -193,15 +242,15 @@ class AIEngine:
|
||||
|
||||
**Prompt Template:** `generate_content`
|
||||
|
||||
**Model:** GPT-4
|
||||
**Model:** Default text model (from `AIModelConfig.is_default`)
|
||||
|
||||
**Credit Cost:** 1 content credit per generation
|
||||
**Credit Cost:** Based on `AIModelConfig.tokens_per_credit` for model used
|
||||
|
||||
---
|
||||
|
||||
## Function: GenerateImages
|
||||
## Function: GenerateImages (Updated v1.4.0)
|
||||
|
||||
**Purpose:** Create images for article content
|
||||
**Purpose:** Create images for article content with quality tier selection
|
||||
|
||||
**Input:**
|
||||
```python
|
||||
@@ -213,17 +262,27 @@ class AIEngine:
|
||||
},
|
||||
"options": {
|
||||
"count": 3,
|
||||
"style": "photorealistic", # photorealistic, illustration, diagram
|
||||
"size": "1024x1024"
|
||||
"quality_tier": "quality", # basic (1 credit), quality (5 credits), premium (15 credits)
|
||||
"style": "photorealistic", # From SystemAISettings or override
|
||||
"size": "1024x1024" # From SystemAISettings or override
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Quality Tiers (v1.4.0):**
|
||||
|
||||
| Tier | Model Example | credits_per_image | Description |
|
||||
|------|---------------|-------------------|-------------|
|
||||
| basic | runware:97@1 | 1 | Fast generation, simple images |
|
||||
| quality | dall-e-3 | 5 | Balanced quality and speed |
|
||||
| premium | google:4@2 | 15 | Best quality, slower |
|
||||
|
||||
**Process:**
|
||||
1. Analyze content to identify image opportunities
|
||||
2. Generate prompts for each image
|
||||
3. Call image provider API
|
||||
4. Store images and generate thumbnails
|
||||
1. Get quality tier model from `AIModelConfig.get_image_models_by_tier()`
|
||||
2. Analyze content to identify image opportunities
|
||||
3. Generate prompts for each image (via `GenerateImagePrompts`)
|
||||
4. Call image provider API (using `IntegrationProvider.get_api_key()`)
|
||||
5. Store images and generate thumbnails
|
||||
|
||||
**Output:**
|
||||
```python
|
||||
@@ -235,7 +294,10 @@ class AIEngine:
|
||||
"alt_text": "Python code example showing...",
|
||||
"caption": "Example of Python list comprehension",
|
||||
"prompt": "A clean code editor showing Python syntax...",
|
||||
"is_featured": True
|
||||
"is_featured": True,
|
||||
"model_used": "dall-e-3",
|
||||
"quality_tier": "quality",
|
||||
"credits_used": 5
|
||||
},
|
||||
...
|
||||
]
|
||||
@@ -244,9 +306,9 @@ class AIEngine:
|
||||
|
||||
**Prompt Template:** `generate_image_prompts`
|
||||
|
||||
**Model:** DALL-E 3 or Runware
|
||||
**Model:** Selected by quality tier from `AIModelConfig`
|
||||
|
||||
**Credit Cost:** 1 image credit per image
|
||||
**Credit Cost:** `AIModelConfig.credits_per_image` for selected model
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Database Models Reference
|
||||
|
||||
**Last Verified:** January 3, 2026
|
||||
**Last Verified:** January 5, 2026
|
||||
|
||||
---
|
||||
|
||||
@@ -8,37 +8,219 @@
|
||||
|
||||
| Scope | Models | Base Class | Filter By |
|
||||
|-------|--------|------------|-----------|
|
||||
| **Global** | `GlobalIntegrationSettings`, `GlobalAIPrompt`, `GlobalAuthorProfile`, `GlobalStrategy`, `GlobalModuleSettings`, `Industry`, `SeedKeyword` | `models.Model` | None (platform-wide) |
|
||||
| **Account** | `Account`, `User`, `Plan`, `IntegrationSettings`, `ModuleEnableSettings`, `AISettings`, `AIPrompt`, `AuthorProfile`, `CreditBalance` | `AccountBaseModel` | `account` |
|
||||
| **Global** | `IntegrationProvider`, `AIModelConfig`, `SystemAISettings`, `GlobalAIPrompt`, `GlobalAuthorProfile`, `GlobalStrategy`, `GlobalModuleSettings`, `Industry`, `SeedKeyword` | `models.Model` | None (platform-wide) |
|
||||
| **Account** | `Account`, `User`, `Plan`, `AccountSettings`, `ModuleEnableSettings`, `AISettings`, `AIPrompt`, `AuthorProfile`, `CreditBalance` | `AccountBaseModel` | `account` |
|
||||
| **Site** | `Site`, `PublishingSettings`, `AutomationConfig`, `SiteIntegration` | `AccountBaseModel` | `account`, `site` |
|
||||
| **Site+Sector** | `Keywords`, `Clusters`, `ContentIdeas`, `Tasks`, `Content`, `Images` | `SiteSectorBaseModel` | `site`, `sector` |
|
||||
|
||||
---
|
||||
|
||||
## Global Models (`igny8_core/modules/system/global_settings_models.py`)
|
||||
## System Models (v1.4.0) (`igny8_core/modules/system/`)
|
||||
|
||||
**Purpose:** Platform-wide defaults and API keys. Admin-only. NOT account-scoped.
|
||||
**Purpose:** Centralized AI configuration, provider API keys, and system-wide defaults.
|
||||
|
||||
### GlobalIntegrationSettings (Singleton)
|
||||
### IntegrationProvider (NEW v1.4.0)
|
||||
|
||||
Centralized storage for ALL external service API keys. Admin-only.
|
||||
|
||||
```python
|
||||
class GlobalIntegrationSettings(models.Model):
|
||||
# OpenAI (used by ALL accounts)
|
||||
openai_api_key = CharField(max_length=500)
|
||||
openai_model = CharField(default='gpt-4o-mini')
|
||||
openai_temperature = FloatField(default=0.7)
|
||||
openai_max_tokens = IntegerField(default=8192)
|
||||
class IntegrationProvider(models.Model):
|
||||
"""Per final-model-schemas.md - Centralized API key storage"""
|
||||
provider_id = CharField(max_length=50, primary_key=True) # openai, runware, stripe, etc.
|
||||
display_name = CharField(max_length=100)
|
||||
provider_type = CharField(max_length=20) # ai, payment, email, storage
|
||||
|
||||
# DALL-E / Image Generation
|
||||
dalle_api_key = CharField(max_length=500)
|
||||
dalle_model = CharField(default='dall-e-3')
|
||||
dalle_size = CharField(default='1024x1024')
|
||||
# Authentication
|
||||
api_key = CharField(max_length=500, blank=True)
|
||||
api_secret = CharField(max_length=500, blank=True)
|
||||
webhook_secret = CharField(max_length=500, blank=True)
|
||||
api_endpoint = URLField(blank=True)
|
||||
|
||||
# Runware
|
||||
runware_api_key = CharField(max_length=500)
|
||||
# Configuration
|
||||
config = JSONField(default=dict)
|
||||
is_active = BooleanField(default=True)
|
||||
is_sandbox = BooleanField(default=False)
|
||||
|
||||
# Audit
|
||||
updated_by = ForeignKey(User, null=True)
|
||||
created_at = DateTimeField(auto_now_add=True)
|
||||
updated_at = DateTimeField(auto_now=True)
|
||||
```
|
||||
|
||||
**Critical:** Singleton (pk=1). API keys here are used by ALL accounts.
|
||||
**Seeded Providers:**
|
||||
- `openai` - AI (text + DALL-E)
|
||||
- `runware` - AI (images)
|
||||
- `anthropic` - AI (future)
|
||||
- `stripe` - Payment
|
||||
- `paypal` - Payment
|
||||
- `resend` - Email
|
||||
|
||||
**Helper Methods:**
|
||||
- `IntegrationProvider.get_provider(provider_id)` - Get active provider
|
||||
- `IntegrationProvider.get_api_key(provider_id)` - Get API key for provider
|
||||
- `IntegrationProvider.get_providers_by_type(type)` - List providers by type
|
||||
|
||||
---
|
||||
|
||||
### AIModelConfig (NEW v1.4.0)
|
||||
|
||||
Single Source of Truth for all AI models with pricing and credit configuration.
|
||||
|
||||
```python
|
||||
class AIModelConfig(models.Model):
|
||||
"""Per final-model-schemas.md - Model definitions + pricing"""
|
||||
model_name = CharField(max_length=100, unique=True) # gpt-4o-mini, dall-e-3, runware:97@1
|
||||
model_type = CharField(max_length=20) # text, image
|
||||
provider = CharField(max_length=50) # Links to IntegrationProvider
|
||||
display_name = CharField(max_length=200)
|
||||
|
||||
is_default = BooleanField(default=False) # One default per type
|
||||
is_active = BooleanField(default=True)
|
||||
|
||||
# Text Model Pricing (per 1K tokens)
|
||||
cost_per_1k_input = DecimalField(max_digits=10, decimal_places=6, null=True)
|
||||
cost_per_1k_output = DecimalField(max_digits=10, decimal_places=6, null=True)
|
||||
tokens_per_credit = IntegerField(null=True) # e.g., 1000, 10000
|
||||
|
||||
# Image Model Pricing
|
||||
credits_per_image = IntegerField(null=True) # e.g., 1, 5, 15
|
||||
quality_tier = CharField(max_length=20, null=True) # basic, quality, premium
|
||||
|
||||
# Model Limits
|
||||
max_tokens = IntegerField(null=True)
|
||||
context_window = IntegerField(null=True)
|
||||
capabilities = JSONField(default=dict) # vision, function_calling, etc.
|
||||
|
||||
created_at = DateTimeField(auto_now_add=True)
|
||||
updated_at = DateTimeField(auto_now=True)
|
||||
```
|
||||
|
||||
**Credit Configuration Examples:**
|
||||
|
||||
| Model | Type | tokens_per_credit | credits_per_image | quality_tier |
|
||||
|-------|------|-------------------|-------------------|--------------|
|
||||
| gpt-4o | text | 1000 | - | - |
|
||||
| gpt-4o-mini | text | 10000 | - | - |
|
||||
| runware:97@1 | image | - | 1 | basic |
|
||||
| dall-e-3 | image | - | 5 | quality |
|
||||
| google:4@2 | image | - | 15 | premium |
|
||||
|
||||
**Helper Methods:**
|
||||
- `AIModelConfig.get_default_text_model()` - Get default text model
|
||||
- `AIModelConfig.get_default_image_model()` - Get default image model
|
||||
- `AIModelConfig.get_image_models_by_tier()` - List image models by quality tier
|
||||
|
||||
---
|
||||
|
||||
### SystemAISettings (NEW v1.4.0)
|
||||
|
||||
System-wide AI defaults. Singleton (pk=1).
|
||||
|
||||
```python
|
||||
class SystemAISettings(models.Model):
|
||||
"""Per final-model-schemas.md - Renamed from GlobalIntegrationSettings"""
|
||||
# AI Parameters
|
||||
temperature = FloatField(default=0.7) # 0.0-2.0
|
||||
max_tokens = IntegerField(default=8192)
|
||||
|
||||
# Image Generation Settings
|
||||
image_style = CharField(max_length=30, default='photorealistic')
|
||||
image_quality = CharField(max_length=20, default='standard') # standard, hd
|
||||
max_images_per_article = IntegerField(default=4) # 1-8
|
||||
image_size = CharField(max_length=20, default='1024x1024')
|
||||
|
||||
# Audit
|
||||
updated_by = ForeignKey(User, null=True)
|
||||
updated_at = DateTimeField(auto_now=True)
|
||||
```
|
||||
|
||||
**Image Style Choices:**
|
||||
- `photorealistic` - Ultra realistic photography
|
||||
- `illustration` - Digital illustration
|
||||
- `3d_render` - Computer generated 3D
|
||||
- `minimal_flat` - Minimal / Flat Design
|
||||
- `artistic` - Artistic / Painterly
|
||||
- `cartoon` - Cartoon / Stylized
|
||||
|
||||
**Image Size Choices:**
|
||||
- `1024x1024` - Square
|
||||
- `1792x1024` - Landscape
|
||||
- `1024x1792` - Portrait
|
||||
|
||||
**Helper Methods:**
|
||||
- `SystemAISettings.get_instance()` - Get singleton instance
|
||||
- `SystemAISettings.get_effective_temperature(account)` - Get with account override
|
||||
- `SystemAISettings.get_effective_image_style(account)` - Get with account override
|
||||
|
||||
---
|
||||
|
||||
### AccountSettings (Per-Account Overrides)
|
||||
|
||||
Generic key-value store for account-specific settings.
|
||||
|
||||
```python
|
||||
class AccountSettings(AccountBaseModel):
|
||||
"""Per final-model-schemas.md - Account overrides"""
|
||||
key = CharField(max_length=100) # Setting key
|
||||
value = JSONField(default=dict) # Setting value
|
||||
|
||||
created_at = DateTimeField(auto_now_add=True)
|
||||
updated_at = DateTimeField(auto_now=True)
|
||||
```
|
||||
|
||||
**AI-Related Keys** (override SystemAISettings defaults):
|
||||
|
||||
| Key | Type | Example | Notes |
|
||||
|-----|------|---------|-------|
|
||||
| `ai.temperature` | float | 0.8 | Override system default |
|
||||
| `ai.max_tokens` | int | 8192 | Override system default |
|
||||
| `ai.image_style` | string | "illustration" | Override system default |
|
||||
| `ai.image_quality` | string | "hd" | Override system default |
|
||||
| `ai.max_images` | int | 6 | Override system default |
|
||||
| `ai.image_quality_tier` | string | "premium" | User's preferred tier |
|
||||
|
||||
---
|
||||
|
||||
### CreditCostConfig (Operation-Level Pricing)
|
||||
|
||||
Fixed credit costs per operation type.
|
||||
|
||||
```python
|
||||
class CreditCostConfig(models.Model):
|
||||
"""Per final-model-schemas.md - Operation pricing"""
|
||||
operation_type = CharField(max_length=50, primary_key=True) # Unique operation ID
|
||||
display_name = CharField(max_length=100)
|
||||
base_credits = IntegerField(default=1) # Fixed credits per operation
|
||||
is_active = BooleanField(default=True)
|
||||
description = TextField(blank=True)
|
||||
```
|
||||
|
||||
**Note:** `tokens_per_credit` moved to AIModelConfig as of v1.4.0.
|
||||
|
||||
---
|
||||
|
||||
## DEPRECATED Models
|
||||
|
||||
### GlobalIntegrationSettings (DEPRECATED in v1.4.0)
|
||||
|
||||
**Replaced by:** `IntegrationProvider` (API keys) + `AIModelConfig` (model configs) + `SystemAISettings` (defaults)
|
||||
|
||||
```python
|
||||
# DEPRECATED - Do not use
|
||||
class GlobalIntegrationSettings(models.Model):
|
||||
# API keys now in IntegrationProvider
|
||||
openai_api_key = CharField(max_length=500) # → IntegrationProvider.get_api_key('openai')
|
||||
runware_api_key = CharField(max_length=500) # → IntegrationProvider.get_api_key('runware')
|
||||
|
||||
# Models now in AIModelConfig
|
||||
openai_model = CharField(default='gpt-4o-mini') # → AIModelConfig.is_default
|
||||
|
||||
# Settings now in SystemAISettings
|
||||
openai_temperature = FloatField(default=0.7) # → SystemAISettings.temperature
|
||||
openai_max_tokens = IntegerField(default=8192) # → SystemAISettings.max_tokens
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### GlobalAIPrompt
|
||||
|
||||
|
||||
Reference in New Issue
Block a user