<aside> đź’ˇ
The LiteLLM API key also gives you access to image generation models like DALL-E 3. The easiest way to generate images is using the openai python library, like in the example below.
</aside>
curl -X POST '<https://litellm.sph-prod.ethz.ch/v1/images/generations>' \\
-H 'Content-Type: application/json' \\
-H 'Authorization: Bearer YOUR-API-KEY' \\
-d '{
"model": "dall-e-3",
"prompt": "CHANGE HERE: A cute baby sea otter",
"n": 1,
"size": "1024x1024"
}'
This returns, among other information, a URL you can click on to open the image in a browser and save it.
To use it yourself, copy the prompt into a terminal and, enter your API key and a prompt (describing the image you want to generate). You can also try different sizes.
[IO.File]::WriteAllBytes("sea_otter.png", [Convert]::FromBase64String((Invoke-RestMethod -Uri "<https://litellm.sph-prod.ethz.ch/v1/images/generations>" -Method Post -Headers @{ "Authorization" = "Bearer YOUR-API-KEY"; "Content-Type" = "application/json" } -Body '{ "model": "dall-e-3", "prompt": "CHANGE HERE: A cute baby sea otter", "n": 1, "size": "1024x1024", "response_format": "b64_json" }').data[0].b64_json))
This command saves an image with the name currently defined as “sea_otter.png” (change it if you like) in the current directory of the shell. If you have just opened your shell by searching for “PowerShell”, then the directory would be “C:\Users\your-user”, where your-user is the username you are logged in with.
To use it yourself, copy the prompt into a terminal and, enter your API key and a prompt (describing the image you want to generate). You can also try different sizes.
Here is an example where the image is saved in your directory:
import openai
import base64
# Don't forget to fill out your API k
client = openai.OpenAI(api_key="your-api-key",base_url="<https://litellm.sph-prod.ethz.ch/v1>")
# Specify what you want on your image and the model you want to use
prompt = "A picture of a cat."
model = "dall-e-3"
# Sending a request to the model
result = client.images.generate(
model=model,
prompt=prompt,
response_format="b64_json" # The result will be returned in bytecode
)
# Converting the received bytecode to something python can save as an image
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Saving the image
with open("cat_image.jpg", "wb") as f:
f.write(image_bytes)
Another example, where a URL to the generated image is printed: