UNPKG

@langchain/openai

Version:
1 lines 7.64 kB
{"version":3,"file":"imageGeneration.d.ts","names":["OpenAI","OpenAIClient","ServerTool","ImageGenerationInputMask","ImageGenerationOptions","ImageGenerationTool","Responses","Tool","ImageGeneration","imageGeneration"],"sources":["../../src/tools/imageGeneration.d.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\nimport type { ServerTool } from \"@langchain/core/tools\";\n/**\n * Optional mask for inpainting. Allows you to specify areas of the image\n * that should be regenerated.\n */\nexport interface ImageGenerationInputMask {\n /**\n * Base64-encoded mask image URL.\n */\n imageUrl?: string;\n /**\n * File ID for the mask image (uploaded via OpenAI File API).\n */\n fileId?: string;\n}\n/**\n * Options for the Image Generation tool.\n */\nexport interface ImageGenerationOptions {\n /**\n * Background type for the generated image.\n * - `transparent`: Generate image with transparent background\n * - `opaque`: Generate image with opaque background\n * - `auto`: Let the model decide based on the prompt\n * @default \"auto\"\n */\n background?: \"transparent\" | \"opaque\" | \"auto\";\n /**\n * Control how much effort the model will exert to match the style and features,\n * especially facial features, of input images. This parameter is only supported\n * for `gpt-image-1`. Unsupported for `gpt-image-1-mini`.\n * - `high`: Higher fidelity to input images\n * - `low`: Lower fidelity to input images\n * @default \"low\"\n */\n inputFidelity?: \"high\" | \"low\";\n /**\n * Optional mask for inpainting. Use this to specify areas of an image\n * that should be regenerated.\n */\n inputImageMask?: ImageGenerationInputMask;\n /**\n * The image generation model to use.\n * @default \"gpt-image-1\"\n */\n model?: \"gpt-image-1\" | \"gpt-image-1-mini\";\n /**\n * Moderation level for the generated image.\n * - `auto`: Standard moderation\n * - `low`: Less restrictive moderation\n * @default \"auto\"\n */\n moderation?: \"auto\" | \"low\";\n /**\n * Compression level for the output image (0-100).\n * Only applies to JPEG and WebP formats.\n * @default 100\n */\n outputCompression?: number;\n /**\n * The output format of the generated image.\n * @default \"png\"\n */\n outputFormat?: \"png\" | \"webp\" | \"jpeg\";\n /**\n * Number of partial images to generate in streaming mode (0-3).\n * When set, the model will return partial images as they are generated,\n * providing faster visual feedback.\n * @default 0\n */\n partialImages?: number;\n /**\n * The quality of the generated image.\n * - `low`: Faster generation, lower quality\n * - `medium`: Balanced generation time and quality\n * - `high`: Slower generation, higher quality\n * - `auto`: Let the model decide based on the prompt\n * @default \"auto\"\n */\n quality?: \"low\" | \"medium\" | \"high\" | \"auto\";\n /**\n * The size of the generated image.\n * - `1024x1024`: Square format\n * - `1024x1536`: Portrait format\n * - `1536x1024`: Landscape format\n * - `auto`: Let the model decide based on the prompt\n * @default \"auto\"\n */\n size?: \"1024x1024\" | \"1024x1536\" | \"1536x1024\" | \"auto\";\n}\n/**\n * OpenAI Image Generation tool type for the Responses API.\n */\nexport type ImageGenerationTool = OpenAIClient.Responses.Tool.ImageGeneration;\n/**\n * Creates an Image Generation tool that allows models to generate or edit images\n * using text prompts and optional image inputs.\n *\n * The image generation tool leverages the GPT Image model and automatically\n * optimizes text inputs for improved performance. When included in a request,\n * the model can decide when and how to generate images as part of the conversation.\n *\n * **Key Features**:\n * - Generate images from text descriptions\n * - Edit existing images with text instructions\n * - Multi-turn image editing by referencing previous responses\n * - Configurable output options (size, quality, format)\n * - Streaming support for partial image generation\n *\n * **Prompting Tips**:\n * - Use terms like \"draw\" or \"edit\" in your prompt for best results\n * - For combining images, say \"edit the first image by adding this element\" instead of \"combine\"\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-image-generation | OpenAI Image Generation Documentation}\n *\n * @param options - Configuration options for the Image Generation tool\n * @returns An Image Generation tool definition to be passed to the OpenAI Responses API\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-4o\" });\n *\n * // Basic usage - generate an image\n * const response = await model.invoke(\n * \"Generate an image of a gray tabby cat hugging an otter with an orange scarf\",\n * { tools: [tools.imageGeneration()] }\n * );\n *\n * // Access the generated image\n * const imageData = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"image_generation_call\"\n * );\n * if (imageData?.result) {\n * // imageData.result contains the base64-encoded image\n * const fs = await import(\"fs\");\n * fs.writeFileSync(\"output.png\", Buffer.from(imageData.result, \"base64\"));\n * }\n *\n * // With custom options\n * const response = await model.invoke(\n * \"Draw a beautiful sunset over mountains\",\n * {\n * tools: [tools.imageGeneration({\n * size: \"1536x1024\", // Landscape format\n * quality: \"high\", // Higher quality output\n * outputFormat: \"jpeg\", // JPEG format\n * outputCompression: 90, // 90% compression\n * })]\n * }\n * );\n *\n * // With transparent background\n * const response = await model.invoke(\n * \"Create a logo with a transparent background\",\n * {\n * tools: [tools.imageGeneration({\n * background: \"transparent\",\n * outputFormat: \"png\",\n * })]\n * }\n * );\n *\n * // Force the model to use image generation\n * const response = await model.invoke(\n * \"A serene lake at dawn\",\n * {\n * tools: [tools.imageGeneration()],\n * tool_choice: { type: \"image_generation\" },\n * }\n * );\n *\n * // Enable streaming with partial images\n * const response = await model.invoke(\n * \"Draw a detailed fantasy castle\",\n * {\n * tools: [tools.imageGeneration({\n * partialImages: 2, // Get 2 partial images during generation\n * })]\n * }\n * );\n * ```\n *\n * @remarks\n * - Supported models: gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, o3\n * - The image generation process always uses `gpt-image-1` model internally\n * - The model will automatically revise prompts for improved performance\n * - Access the revised prompt via `revised_prompt` field in the output\n * - Multi-turn editing is supported by passing previous response messages\n */\nexport declare function imageGeneration(options?: ImageGenerationOptions): ServerTool;\n//# sourceMappingURL=imageGeneration.d.ts.map"],"mappings":";;;;;;;AAMA;AAaA;AA2EYK,UAxFKF,wBAAAA,CAwFiBF;EAkGVQ;;;;;;;;;;;;UA7KPL,sBAAAA;;;;;;;;;;;;;;;;;;;;;;mBAsBID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAqDTE,mBAAAA,GAAsBJ,QAAAA,CAAaK,SAAAA,CAAUC,IAAAA,CAAKC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkGtCC,eAAAA,WAA0BL,yBAAyBF"}