Portkey's AI gateway supports image generation capabilities that many foundational model providers offer. The most common use case is that of text-to-image where the user sends a prompt which the image model processes and returns an image.
Portkey supports the OpenAI signature to make text-to-image requests.
import Portkey from'portkey-ai';// Initialize the Portkey clientconstportkey=newPortkey({ apiKey:"PORTKEY_API_KEY",// Replace with your Portkey API key virtualKey:"VIRTUAL_KEY"// Add your provider's virtual key});asyncfunctionmain() {constimage=awaitportkey.images.generate({ model:"dall-e-3", prompt:"Lucy in the sky with diamonds" });console.log(image.data);}main();
from portkey_ai import Portkeyfrom IPython.display import display, Image# Initialize the Portkey clientportkey =Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key virtual_key="VIRTUAL_KEY"# Add your provider's virtual key)image = portkey.images.generate( model="dall-e-3", prompt="Lucy in the sky with diamonds")# Display the imagedisplay(Image(url=image.data[0].url))
import OpenAI from'openai'; // We're using the v4 SDKimport { PORTKEY_GATEWAY_URL, createHeaders } from'portkey-ai'constopenai=newOpenAI({ apiKey:'OPENAI_API_KEY',// defaults to process.env["OPENAI_API_KEY"], baseURL:PORTKEY_GATEWAY_URL, defaultHeaders:createHeaders({ provider:"openai", apiKey:"PORTKEY_API_KEY"// defaults to process.env["PORTKEY_API_KEY"] })});asyncfunctionmain() {constimage=awaitopenai.images.generate({ model:"dall-e-3", prompt:"Lucy in the sky with diamonds" });console.log(image.data);}main();
from openai import OpenAIfrom portkey_ai import PORTKEY_GATEWAY_URL, createHeadersfrom IPython.display import display, Imageclient =OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY" ))image = client.images.generate( model="dall-e-3", prompt="Lucy in the sky with diamonds", n=1, size="1024x1024")# Display the imagedisplay(Image(url=image.data[0].url))
curl"https://api.portkey.ai/v1/chat/completions" \-H"Content-Type: application/json" \-H"x-portkey-api-key: $PORTKEY_API_KEY" \-H"x-portkey-virtual-key: openai-virtual-key" \-d'{ "model": "dall-e-3", "prompt": "Lucy in the sky with diamonds" }'
On completion, the request will get logged in the logs UI where the image can be viewed.
(Note that providers may remove the hosted image after a period of time, so some logs might only contain the url)
Supported Providers and Models
The following providers are supported for image generation with more providers getting added soon. Please raise a request or a PR to add model or provider to the AI gateway.