Skip to content

Commit ac7a529

Browse files
committed
Update docs
1 parent e726f24 commit ac7a529

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

docs/_core_features/image-generation.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ After reading this guide, you will know:
2626
* How to generate images from text prompts.
2727
* How to select different image generation models.
2828
* How to specify image sizes (for supported models).
29+
* How to use model hosting platforms like Replicate.
2930
* How to access and save generated image data (URL or Base64).
3031
* How to integrate image generation with Rails Active Storage.
3132
* Tips for writing effective image prompts.
@@ -122,9 +123,29 @@ image_portrait = RubyLLM.paint(
122123
> Not all models support size customization. Check the provider's documentation or the [Available Models Guide]({% link _reference/available-models.md %}) for supported sizes.
123124
{: .note }
124125

126+
## Using model hosting platforms
127+
128+
Platforms like Replicate host a large collection of models with different capabilities and parameters. Due to the variety of models available, you should be aware of the parameters supported by the model you're using. This information is available in the platform's documentation.
129+
130+
For example, Imagen 4 Ultra supports `aspect_ratio` as a parameter when used [via Replicate](https://replicate.com/google/imagen-4-ultra). Since it's optional, you can omit it. But if you'd like to specify a value, you just need to add it to your paint call.
131+
132+
```ruby
133+
image = RubyLLM.paint(
134+
"A photorealistic image of a red panda coding Ruby on a laptop",
135+
model: "google/imagen-4-ultra",
136+
provider: :replicate,
137+
aspect_ratio: "16:9"
138+
)
139+
```
140+
141+
> When switching between different models, you'll typically need to change the parameters as well, since different models support different parameter sets. Always check the model's documentation for the specific parameters it accepts.
142+
{: .note }
143+
125144
## Working with Generated Images
126145

127-
The `RubyLLM::Image` object provides access to the generated image data and metadata.
146+
When models return the image immediately, the `RubyLLM::Image` object provides access to the generated image data and metadata.
147+
148+
Some models generate images asynchronously. In this case, you will receive a `RubyLLM::DeferredImage` object instead. You can still access the image data, but you will either need to wait for the image to be generated or fetch it by other means—typically after being notified via a webhook.
128149

129150
### Accessing Image Data
130151

@@ -133,6 +154,10 @@ The `RubyLLM::Image` object provides access to the generated image data and meta
133154
* `image.mime_type`: Returns the MIME type (e.g., `"image/png"`, `"image/jpeg"`).
134155
* `image.base64?`: Returns `true` if the image data is Base64-encoded, `false` otherwise.
135156

157+
### Accessing Deferred Image Data
158+
159+
* `deferred_image.url`: Returns the URL where you can check whether the image has been generated.
160+
136161
### Saving Images Locally
137162

138163
The `save` method works regardless of whether the image was delivered via URL or Base64. It fetches the data if necessary and writes it to the specified file path.
@@ -150,9 +175,23 @@ rescue => e
150175
end
151176
```
152177

178+
For deferred images, the `save` method will write the file and return its path only if the image has been generated. Otherwise, it will return `nil`.
179+
180+
The ideal way to handle deferred images is by having the provider notify you via webhook when the image is generated, and then fetching the image outside of RubyLLM. But if you're not able to configure a webhook, another way to handle these is to call the method recursively with a delay until it succeeds or a condition is met. This is equivalent to a "polling" mechanism. Check your provider's documentation for any rate limits that may apply.
181+
182+
```ruby
183+
# Only do this if you're not able to configure a webhook
184+
def save_deferred_image(image, path, remaining_attempts = 10)
185+
return nil if remaining_attempts <= 0
186+
image.save(path) || (sleep(2) && save_deferred_image(image, path, remaining_attempts - 1))
187+
end
188+
189+
save_deferred_image(deferred_image, "deferred_image.png")
190+
```
191+
153192
### Getting Raw Image Blob
154193

155-
The `to_blob` method returns the raw binary image data (decoded from Base64 or downloaded from URL). This is useful for integration with other libraries or frameworks.
194+
The `to_blob` method returns the raw binary image data (decoded from Base64 or downloaded from URL). This is useful for integration with other libraries or frameworks. Deferred images return `nil` if the image has not finished generating.
156195

157196
```ruby
158197
image = RubyLLM.paint("Abstract geometric patterns in pastel colors")

0 commit comments

Comments
 (0)