Files
igny8/new-updated.md
IGNY8 VPS (Salman) b2922ebec5 refactor-4th-jan-plan
2026-01-04 00:39:44 +00:00

165 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## IGNY8 AI & Configuration Settings Report
---
### 1. AI Mode Configuration
**Architecture**: Two-tier database-driven model configuration system
#### AIModelConfig Model (Primary)
Stores all AI model configurations in database, replacing legacy hardcoded constants.
**Model Types**:
- Text Generation ✅
- Image Generation ✅
**Supported Providers**:
- OpenAI ✅
- Runware ✅
**Key Configuration Fields**:
| Category | Fields |
|----------|--------|
| **Identity** | `model_id`, `display_name`, `model_type`, `provider` |
| **Text Pricing** | `input_token_rate`, `output_token_rate` (per 1M tokens in USD) |
| **Text Limits** | `max_input_tokens`, `max_output_tokens` |
| **Image Pricing** | `cost_per_image` (fixed USD per image) |
| **Image Config** | `available_sizes` (JSON array of valid dimensions) |
| **Status** | `is_active`, `is_default`, `sort_order` |
| **Metadata** | `notes`, `release_date`, `deprecation_date` |
**Seeded Models**:
- **OpenAI Text**: gpt-4o-mini, gpt-4o, gpt-5.1(default)
- **OpenAI Image**: dall-e-3 (default)
- **Runware**: runware:97@1, google:4@2
---
### 2. Global Integration Settings
**Model**: `GlobalIntegrationSettings` (Singleton - always pk=1)
Stores **platform-wide** API keys and default settings used by ALL accounts.
| Provider | API Key Field | Default Model | Parameters |
|----------|---------------|---------------|------------|
| **OpenAI** | `openai_api_key` | gpt-5.1 | temperature: 0.7, max_tokens: 8192 |
| **DALL-E** | `dalle_api_key` | dall-e-3 | size: 1024x1024 |
| **Runware** | `runware_api_key` | runware:97@1 & google:4@2 | — |
**Default Provider Settings**:
- `default_text_provider`: 'openai'
- `default_image_service`: 'openai'
**Universal Image Settings**:
- `image_quality`: standard/hd
- `image_style`: photorealistic, illustration, etc.
- `max_in_article_images`: Default 2
**Critical Security Rule**: API keys exist ONLY in GlobalIntegrationSettings - never stored at account/user level.
---
### 4. User-Specific Record Creation (Save Mechanism)
**Three-Tier Hierarchy**:
```
Global (Platform) → Account (Tenant) → User (Personal)
```
#### Models Involved
| Model | Scope | Unique Key |
|-------|-------|------------|
| `GlobalIntegrationSettings` | Platform-wide | Singleton (pk=1) |
| `AccountSettings` | Per-tenant | account + key |
| `IntegrationSettings` | Per-tenant overrides | account + integration_type |
| `UserSettings` | Per-user preferences | user + account + key |
#### Save Flow When User Changes Config
1. **Frontend** calls POST/PUT to `/api/v1/system/settings/user/`
2. **Backend** ViewSet extracts user and account from authenticated request
3. **Check existing**: Query for existing setting with same user + account + key
4. **Create or Update**:
- If not exists → `serializer.save(user=user, account=account)` creates new record
- If exists → Updates the `value` JSON field
5. **Validation**: Schema validation runs against `SETTINGS_SCHEMAS` before save
6. **Response** returns the saved setting object
#### Integration Override Pattern
For AI/integration settings specifically:
1. User changes model/temperature (NOT API keys)
2. System strips any API key fields from request (security)
3. `IntegrationSettings.objects.get_or_create(account=account, integration_type=type)`
4. Only allowed override fields saved in `config` JSON field
5. On read, system merges: Global defaults → Account overrides
---
### 6. Image Generation: Internal Cost vs Customer Credit Allocation
#### Internal Cost (What Platform Pays to Providers)
| Model | Provider | Cost Per Image (USD) |
|-------|----------|---------------------|
| dall-e-3 | OpenAI | $0.05 |
| runware:97@1 | Runware - Hi Dream Full | ~$0.013 |
| google:4@2 | Runware - Google Nano Banaan | ~$0.15 |
**Storage**: AIModelConfig.`cost_per_image` field + legacy `IMAGE_MODEL_RATES` constants
!!! This need to be fixed rates tobe laoded and used form configured AI Models !! not from hard coded location
#### Customer Credit Cost (What Customer Pays)
| Operation | Credits Charged | Price per Credit | Min Charge |
|-----------|-----------------|------------------|------------|
| Image Generation | 5 credits | $0.02 | $0.10 |
| Image Prompt Extraction | 2 credits | $0.01 | $0.02 |
!!!morre robust image gneartion csoting and pricing mecahnishm required, withotu long chains or workarounds!!!
**Configuration Model**: `CreditCostConfig`
- `tokens_per_credit`: 50 (image gen uses fewer tokens per credit = higher cost)
- `min_credits`: 5
- `price_per_credit_usd`: $0.02
#### Margin Calculation
| Metric | DALL-E 3 Example |
|--------|------------------|
| Provider Cost | $0.040 |
| Customer Charge | $0.10 (5 credits × $0.02) |
| **Margin** | $0.06 (60% of customer charge) |
| **Markup** | ~150% |
**Flow**:
1. **Before AI call**: Calculate required credits based on image count
2. **Check balance**: Verify account has sufficient credits
3. **Deduct credits**: Remove from balance, log transaction
4. **Execute**: Make AI provider API call
5. **Track**: `CreditUsageLog` stores both `credits_used` (customer) and `cost_usd` (actual)
**Revenue Analytics** queries `CreditUsageLog` to calculate:
- Total revenue = Σ(credits_used × credit_price)
- Total cost = Σ(cost_usd from provider)
- Margin = Revenue - Cost
---
### Summary
The IGNY8 platform implements a sophisticated multi-tier configuration system:
- **AI configuration** is database-driven with fallback to legacy constants
- **Global settings** hold platform API keys; accounts only override model/parameters
- **User settings** create per-user records keyed by user + account + key combination
- **Credit system** charges customers a markup (~150%) over actual provider costs
- **Several fields are deprecated** including `mobile_image_size`, `reference_id`, and the `get_model()` method