feat(image generation): Add image generation support for models OpenAI and Google Gemini #1009
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Image Generation Feature Implementation Summary
Overview
This PR adds first-class image generation support to langchainrb, addressing issue #924. The implementation follows existing patterns in the codebase while making image generation a provider-agnostic feature that works seamlessly across OpenAI, Google Gemini, and Google Vertex AI.
Key Design Decisions
1. Provider-Agnostic API
generate_image
method toLangchain::LLM::Base
that raisesNotImplementedError
generate_image(prompt:, n: 1, ...)
2. Model Configuration via Defaults
@defaults
hashimage_generation_model
to DEFAULTS for each provider:"dall-e-3"
"gemini-2.0-flash-preview-image-generation"
"imagen-3.0-generate-002"
3. Response Handling
image_urls
methodimage_base64s
methodImplementation Details
Files Modified
lib/langchain/llm/base.rb
generate_image
method that raisesNotImplementedError
lib/langchain/llm/openai.rb
generate_image
using OpenAI's Images APIimage_generation_model
to DEFAULTSlib/langchain/llm/google_gemini.rb
generate_image
using Gemini's generateContent endpointimage_generation_model
to DEFAULTSlib/langchain/llm/google_vertexai.rb
generate_image
using Vertex AI's Imagen modelimage_generation_model
to DEFAULTSResponse Classes
lib/langchain/llm/response/openai_response.rb
image_urls
method to extract URLs from image generation responseslib/langchain/llm/response/google_gemini_response.rb
image_base64s
method to extract base64 data frominlineData.data
fieldlib/langchain/llm/response/google_vertex_ai_response.rb (new file)
image_base64s
methodExamples and Tests
examples/generate_image.rb
Test Coverage
Usage Example
Benefits
generate_image
following the same patternFuture Considerations
This implementation provides a solid foundation for image generation in langchainrb while maintaining backward compatibility and following established patterns in the codebase.