tools.comfyui_generate_image module
Image generation via Civitai API.
Generates AI images using Civitai’s generation service and returns the image file path or download URL.
- async tools.comfyui_generate_image.run(prompt, negative_prompt=None, width=1024, height=1024, steps=30, cfg=4.0, seed=None, scheduler='DPM2MKarras', model_urn=None, clip_skip=2, ctx=None)[source]
Generate one or more AI images via Civitai and return their file paths.
Entry point for the
civitai_generate_imagetool. It resolves a Civitai API token (per-user viatools.manage_api_keys.get_user_api_keywhenctxhas Redis and a user id, otherwise the environment token), temporarily installs it intoos.environso the civitai-py SDK can pick it up, clamps and normalizes the generation parameters (mapping scheduler aliases viaSCHEDULER_MAP, capping width/height to the generator’s 1024 contract, defaulting the model toDEFAULT_MODEL_URN, randomizing the seed when unset), submits the job with retries throughcivitai.image.create, waits for it with_poll_job_status(), downloads each result with_download_image(), and writes them to per-image temp files. The originalCIVITAI_API_TOKENis always restored in afinallyblock.Dispatched by
tool_loader.pyas thecivitai_generate_imagehandler (located viagetattr(module, "run")); not called directly elsewhere. Side effects include the temporaryos.environtoken swap, the Civitai HTTP calls, and writing PNG files into freshly created temp directories.- Parameters:
prompt (
str) – Text description of the image to generate (required).negative_prompt (
str) – Optional description of what to avoid.width (
int) – Requested width in pixels (clamped to 64-2048; the API call is additionally capped at 1024).height (
int) – Requested height in pixels (clamped to 64-2048; API-capped at 1024).steps (
int) – Sampling steps (clamped to 1-100).cfg (
float) – CFG (classifier-free guidance) scale (1.0-20.0).seed (
int) – Random seed; a random value is chosen whenNone.scheduler (
str) – Sampling scheduler name or alias (mapped viaSCHEDULER_MAP).model_urn (
str) – Optional Civitai model URN; defaults toDEFAULT_MODEL_URN.clip_skip (
int) – Number of CLIP layers to skip.ctx (
ToolContext|None) – Tool execution context; supplies Redis and the user id used to resolve a per-user API key.
- Returns:
A JSON object with
success, the savedfileslist, theseed, and the effectiveparameterson success, or an{"error": ...}object on missing prompt, missing key, API failure, or download failure.- Return type: