docs/docs/integrations/image-models/open-ai-official.md
:::note
This is the documentation for the OpenAI Official SDK integration, that uses the official OpenAI Java SDK.
LangChain4j provides 3 different integrations with OpenAI for generating images, and this is #2 :
:::
This integration uses the OpenAI Java SDK GitHub Repository, and will work for all OpenAI models which can be provided by:
It will also work with models supporting the OpenAI API.
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-official</artifactId>
<version>1.18.1-beta28</version>
</dependency>
To use OpenAI models, you usually need an endpoint URL, an API key, and a model name. This depends on where the model is hosted, and this integration tries to make it easier with some auto-configuration:
import com.openai.models.images.ImageModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.openaiofficial.OpenAiOfficialImageModel;
import static com.openai.models.images.ImageModel.GPT_IMAGE_1_MINI;
// ....
ImageModel model = OpenAiOfficialImageModel.builder()
.baseUrl(System.getenv("OPENAI_BASE_URL"))
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName(GPT_IMAGE_1_MINI)
.build();
Similar to configuring the OpenAI Official Chat Model, you can configure the OpenAiOfficialImageModel with
Microsoft Foundry and GitHub Models, using the isAzure() and isGitHubModels() methods.
ImageModel model = OpenAiOfficialImageModel.builder()
.baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT"))
.apiKey(System.getenv("AZURE_OPENAI_KEY"))
.modelName(GPT_IMAGE_1_MINI)
.isAzure(true) // Not necessary if the base URL ends with `openai.azure.com`
.build();
You can also use "passwordless" authentication, as described in the OpenAI Official Chat Model documentation.
ImageModel model = OpenAiOfficialImageModel.builder()
.modelName(GPT_IMAGE_1_MINI)
.isGitHubModels(true)
.build();
Once the model is configured, you can use it to generate images:
String imageUrl = imageModel
.generate("A coffee mug in Paris, France")
.content()
.base64Data();