Implementation:869413421 Ai moive studio GatewayProvider
| Knowledge Sources | |
|---|---|
| Domains | Architecture, Generative_AI |
| Last Updated | 2026-10-23 12:00 GMT |
Overview
Unified AI generation gateway that dispatches text, image, audio, and video generation requests to multiple provider APIs through profile-based protocol selection.
Description
The GatewayProvider class is the central abstraction of the AI generation system. It wraps an AsyncOpenAI client for chat/responses/image/speech protocols and httpx.AsyncClient for non-OpenAI-compatible protocols (Gemini, Seedream, MiniMax, Wan, Grok). Each generation method (completions, generate_image, generate_audio, create_video) first resolves the model via the catalog, then dispatches to the appropriate protocol handler based on the model's profile field.
The provider enforces concurrency limits via asyncio.Semaphore, validates input parameters (aspect ratios, durations, resolutions, voice selections) against model capabilities, and never exposes credentials in error messages.
Usage
- Text generation: Call completions() with model name and messages; supports chat and responses profiles, streaming, and JSON mode
- Image generation: Call generate_image() with prompt, model, and optional reference images; supports gemini_image, images (edits), seedream, seedream_pro profiles
- Audio generation: Call generate_audio() with text, voice, and model; supports speech (OpenAI TTS), minimax_speech, gemini_speech profiles
- Video generation: Call create_video() with prompt, images, and model; supports gateway_video, grok_video, wan_video profiles; returns task ID for async polling
- Video polling: Call get_task_status() with the task ID; requires provider context saved at submission time
Code Reference
Source Location
- Repository: ai-moive-studio
- File: backend/src/services/provider/gateway.py
Signature
class GatewayProvider:
def __init__(self, api_key, max_concurrency=5, base_url=None, provider='custom', context=None):
"""Initialize provider with connection, semaphore, and AsyncOpenAI client."""
async def completions(self, model, messages, **kwargs) -> completion_result:
"""Generate text via chat or responses profile. Supports stream, response_format."""
async def generate_image(self, prompt, model=None, **kwargs) -> ImageResult:
"""Generate image via gemini_image, images, seedream, or seedream_pro profile."""
async def generate_audio(self, input_text, voice=None, model=None, **kwargs) -> AudioResult:
"""Generate audio via speech, minimax_speech, or gemini_speech profile."""
async def create_video(self, prompt, images=None, model=None, **kwargs) -> Dict:
"""Submit video generation task. Returns {id, status, provider_context}."""
async def get_task_status(self, task_id) -> Dict:
"""Poll video task status. Returns {id, status, video_url, progress, error}."""
async def get_video_content(self, task_id) -> Dict:
"""Alias for get_task_status."""
Import
from src.services.provider.gateway import GatewayProvider
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| api_key | str | Yes | Plaintext API key for the provider |
| max_concurrency | int | No | Semaphore limit (default 5) |
| base_url | str | No | Custom base URL (resolved via registry) |
| provider | str | No | Provider name (default 'custom') |
| context | dict | No | Video task protocol context for resume |
| model | str | Yes | Model name (resolved via catalog) |
| messages | List[Dict] | Yes (text) | Chat messages with role/content |
| prompt | str | Yes (img/video) | Generation prompt |
| reference_images | list | No | Reference image URLs/base64/MinIO keys |
| aspect_ratio | str | No | Aspect ratio (validated against model) |
| duration | int | No | Video duration in seconds |
| resolution | str | No | Video resolution |
| voice | str | No | TTS voice ID |
Outputs
| Name | Type | Description |
|---|---|---|
| completions() | SimpleNamespace | .choices[0].message.content (str), .usage |
| generate_image() | ImageResult | List of ImageData (url, b64_json, mime) |
| generate_audio() | AudioResult | .content (bytes), .mime, .extension |
| create_video() | Dict | {id, status: "pending", provider_context} |
| get_task_status() | Dict | {id, status, video_url, progress, error} |
Usage Examples
Text Generation (Chat Profile)
from src.services.provider.factory import ProviderFactory
# Create provider from API key
provider = ProviderFactory.from_key(api_key)
# Generate text
response = await provider.completions(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Generate an image prompt for a sunset."},
],
response_format={"type": "json_object"},
)
text = response.choices[0].message.content
Image Generation with Reference
# Generate image with reference images
result = await provider.generate_image(
prompt="A cinematic shot of a character",
model="gemini-3-pro-image-preview",
reference_images=["uploads/character_ref.jpg"],
aspect_ratio="16:9",
)
image_data = result.images[0] # ImageData with b64_json and mime
Video Generation and Polling
# Submit video generation
response = await provider.create_video(
prompt="Camera pans from left to right",
images=["https://example.com/frame1.jpg"],
model="wan2.1",
aspect_ratio="16:9",
duration=5,
resolution="720p",
)
task_id = response["id"]
provider_context = response["provider_context"]
# Poll for completion (may be in a separate session via resume_video)
status = await provider.get_task_status(task_id)
if status["status"] == "completed":
video_url = status["video_url"]
Related Pages
Implements Principle
- Principle:869413421_Ai_moive_studio_Multi_Provider_Gateway
- Principle:869413421_Ai_moive_studio_LLM_Prompt_Generation
- Principle:869413421_Ai_moive_studio_AI_Image_Generation
- Principle:869413421_Ai_moive_studio_TTS_Audio_Generation
- Principle:869413421_Ai_moive_studio_AI_Video_Generation