Welcome to Portkey Forum

Updated 8 months ago

Generating Images Through Portkey API

Try creating an image through Portkey will give base64 format, even after setting up response_format to be url.
Plain Text
config_json = json.dumps(config)

url = 'https://api.portkey.ai/v1/images/generations'

headers = {
            'Content-Type': 'application/json',
            'x-portkey-api-key': f'{PORTKEY_API_KEY}',
          }

headers['x-portkey-config'] = config_json

data = {"prompt": "Harry potter using aeroplane for transport", "response_format":"url"}

response = requests.post(url, headers=headers, data=json.dumps(data))

generation_response = response.json()

print(generation_response)

# {'created': '1710664648966', 'data': [{'b64_json': 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACA....

This could be because Stability AI does not return URL in it's response (after Accept: image/png). But it does return bytes. How can one get them through Portkey ?
V
r
v
4 comments
cc @visarg who can share more on this!
Yes, stabillity only returns base_64
Stability will only return base_64 when used through the unified image generation API. If you want to get the bytes, then you will have to use the proxy API. But proxy API will take stability-ai params and not the openai compatible params. And the response will also be stability-ai type and not openai type
In case you want bytes response, then you can use this to make a proxy call.
Plain Text
curl --location 'https://api.portkey.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image' \
--header 'x-portkey-provider: stability-ai' \
--header 'Authorization: <stability-key>' \
--header 'x-portkey-api-key: <portkey-key>' \
--header 'Accept: image/png' \
--header 'Content-Type: application/json' \
--data '{
    "steps": 40,
    "width": 1024,
    "height": 1024,
    "seed": 0,
    "cfg_scale": 5,
    "samples": 1,
    "text_prompts": [
        {
            "text": "A painting of a cat",
            "weight": 1
        },
        {
            "text": "blurry, bad",
            "weight": -1
        }
    ]
}'
Add a reply
Sign up and join the conversation on Discord