generate_image
Create images from text prompts with customizable parameters like style, quality, dimensions, and output count using AI model inference.
Instructions
Generate an image using the specified parameters.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| style | No | ||
| quality | No | balanced | |
| width | No | ||
| height | No | ||
| num_outputs | No | ||
| seed | No |
Implementation Reference
- The core handler for the 'generate_image' tool. It applies quality and style presets to construct input parameters for the SDXL model (hardcoded version), creates a Replicate prediction, and returns a resource URI and metadata for tracking progress.
async def generate_image( prompt: str, style: str | None = None, quality: str = "balanced", width: int | None = None, height: int | None = None, num_outputs: int = 1, seed: int | None = None, ) -> dict[str, Any]: """Generate an image using the specified parameters.""" # Get quality preset parameters if quality not in QUALITY_PRESETS["presets"]: quality = "balanced" parameters = QUALITY_PRESETS["presets"][quality]["parameters"].copy() # Apply style preset if specified if style: if style in STYLE_PRESETS["presets"]: style_params = STYLE_PRESETS["presets"][style]["parameters"] # Merge prompt prefixes if "prompt_prefix" in style_params: prompt = f"{style_params['prompt_prefix']}, {prompt}" # Copy other parameters for k, v in style_params.items(): if k != "prompt_prefix": parameters[k] = v # Override size if specified if width: parameters["width"] = width if height: parameters["height"] = height # Add other parameters parameters.update( { "prompt": prompt, "num_outputs": num_outputs, } ) if seed is not None: parameters["seed"] = seed # Create prediction with SDXL model async with ReplicateClient(api_token=os.getenv("REPLICATE_API_TOKEN")) as client: result = await client.create_prediction( version="39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b", # SDXL v1.0 input=parameters, ) # Return resource information return { "resource_uri": f"generations://{result['id']}", "status": result["status"], "message": ( "🎨 Starting your image generation...\n\n" f"Prompt: {prompt}\n" f"Style: {style or 'Default'}\n" f"Quality: {quality}\n\n" "Let me check the status of your generation. I'll use:\n" f"`get_prediction(\"{result['id']}\", wait=true)`\n\n" "This will let me monitor the progress and show you the image as soon as it's ready." ), "next_prompt": "after_generation", "metadata": { # Add metadata for client use "prompt": prompt, "style": style, "quality": quality, "width": width, "height": height, "seed": seed, "model": "SDXL v1.0", "created_at": result.get("created_at"), }, } - QUALITY_PRESETS dictionary used by generate_image to set default parameters based on quality level (draft, balanced, quality, extreme).
QUALITY_PRESETS = { "id": "quality-presets", "name": "Quality Presets", "description": "Common quality presets for different generation scenarios", "model_type": "any", "presets": { "draft": { "description": "Fast draft quality for quick iterations", "parameters": { "num_inference_steps": 20, "guidance_scale": 5.0, "width": 512, "height": 512, }, }, "balanced": { "description": "Balanced quality and speed for most use cases", "parameters": { "num_inference_steps": 30, "guidance_scale": 7.5, "width": 768, "height": 768, }, }, "quality": { "description": "High quality for final outputs", "parameters": { "num_inference_steps": 50, "guidance_scale": 7.5, "width": 1024, "height": 1024, }, }, "extreme": { "description": "Maximum quality, very slow", "parameters": { "num_inference_steps": 150, "guidance_scale": 8.0, "width": 1536, "height": 1536, }, }, }, "version": "1.0.0", } - STYLE_PRESETS dictionary used by generate_image to apply style-specific parameters and prompt prefixes (photorealistic, cinematic, anime, etc.).
STYLE_PRESETS = { "id": "style-presets", "name": "Style Presets", "description": "Common style presets for different artistic looks", "model_type": "any", "presets": { "photorealistic": { "description": "Highly detailed photorealistic style", "parameters": { "prompt_prefix": "professional photograph, photorealistic, highly detailed, 8k uhd", "negative_prompt": "painting, drawing, illustration, anime, cartoon, artistic, unrealistic", "guidance_scale": 8.0, }, }, "cinematic": { "description": "Dramatic cinematic style", "parameters": { "prompt_prefix": "cinematic shot, dramatic lighting, movie scene, high budget film", "negative_prompt": "low quality, amateur, poorly lit", "guidance_scale": 7.5, }, }, "anime": { "description": "Anime/manga style", "parameters": { "prompt_prefix": "anime style, manga art, clean lines, vibrant colors", "negative_prompt": "photorealistic, 3d render, photograph, western art style", "guidance_scale": 7.0, }, }, "digital_art": { "description": "Digital art style", "parameters": { "prompt_prefix": "digital art, vibrant colors, detailed illustration", "negative_prompt": "photograph, realistic, grainy, noisy", "guidance_scale": 7.0, }, }, "oil_painting": { "description": "Oil painting style", "parameters": { "prompt_prefix": "oil painting, textured brushstrokes, artistic, rich colors", "negative_prompt": "photograph, digital art, 3d render, smooth", "guidance_scale": 7.0, }, }, }, "version": "1.0.0", }