Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2025 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,9 @@

package org.springframework.ai.model.stabilityai.autoconfigure;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.ai.image.observation.ImageModelObservationConvention;
import org.springframework.ai.model.SpringAIModelProperties;
import org.springframework.ai.model.SpringAIModels;
import org.springframework.ai.stabilityai.StabilityAiImageModel;
Expand Down Expand Up @@ -68,8 +71,15 @@ public StabilityAiApi stabilityAiApi(StabilityAiConnectionProperties commonPrope
@Bean
@ConditionalOnMissingBean
public StabilityAiImageModel stabilityAiImageModel(StabilityAiApi stabilityAiApi,
StabilityAiImageProperties stabilityAiImageProperties) {
return new StabilityAiImageModel(stabilityAiApi, stabilityAiImageProperties.getOptions());
StabilityAiImageProperties stabilityAiImageProperties,
ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<ImageModelObservationConvention> observationConvention) {
var imageModel = new StabilityAiImageModel(stabilityAiApi, stabilityAiImageProperties.getOptions(),
observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP));

observationConvention.ifAvailable(imageModel::setObservationConvention);

return imageModel;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@ public class StabilityAiImageProperties extends StabilityAiParentProperties {
public static final String CONFIG_PREFIX = "spring.ai.stabilityai.image";

@NestedConfigurationProperty
private final StabilityAiImageOptions options = StabilityAiImageOptions.builder().build(); // stable-diffusion-v1-6
private final StabilityAiImageOptions options = StabilityAiImageOptions.builder().build(); // stable-diffusion-xl-1024-v1-0

public StabilityAiImageOptions getOptions() {
return this.options;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,14 +19,21 @@
import java.util.List;
import java.util.stream.Collectors;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImageGeneration;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.image.ImageResponseMetadata;
import org.springframework.ai.image.observation.DefaultImageModelObservationConvention;
import org.springframework.ai.image.observation.ImageModelObservationContext;
import org.springframework.ai.image.observation.ImageModelObservationConvention;
import org.springframework.ai.image.observation.ImageModelObservationDocumentation;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.observation.conventions.AiProvider;
import org.springframework.ai.stabilityai.api.StabilityAiApi;
import org.springframework.ai.stabilityai.api.StabilityAiImageOptions;
import org.springframework.util.Assert;
Expand All @@ -37,19 +44,40 @@
*/
public class StabilityAiImageModel implements ImageModel {

private static final AiProvider PROVIDER_NAME = AiProvider.STABILITY_AI;

private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention();

private final StabilityAiImageOptions defaultOptions;

private final StabilityAiApi stabilityAiApi;

/**
* Observation registry used for instrumentation.
*/
private final ObservationRegistry observationRegistry;

/**
* Conventions to use for generating observations.
*/
private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;

public StabilityAiImageModel(StabilityAiApi stabilityAiApi) {
this(stabilityAiApi, StabilityAiImageOptions.builder().build());
}

public StabilityAiImageModel(StabilityAiApi stabilityAiApi, StabilityAiImageOptions defaultOptions) {
this(stabilityAiApi, defaultOptions, ObservationRegistry.NOOP);
}

public StabilityAiImageModel(StabilityAiApi stabilityAiApi, StabilityAiImageOptions defaultOptions,
ObservationRegistry observationRegistry) {
Assert.notNull(stabilityAiApi, "StabilityAiApi must not be null");
Assert.notNull(defaultOptions, "StabilityAiImageOptions must not be null");
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
this.stabilityAiApi = stabilityAiApi;
this.defaultOptions = defaultOptions;
this.observationRegistry = observationRegistry;
}

private static StabilityAiApi.GenerateImageRequest getGenerateImageRequest(ImagePrompt stabilityAiImagePrompt,
Expand Down Expand Up @@ -84,6 +112,7 @@ public StabilityAiImageOptions getOptions() {
* options
* @return the ImageResponse generated by the StabilityAiImageModel
*/
@Override
public ImageResponse call(ImagePrompt imagePrompt) {
// Merge the runtime options passed via the prompt with the default options
// configured via the constructor.
Expand All @@ -95,12 +124,24 @@ public ImageResponse call(ImagePrompt imagePrompt) {
StabilityAiApi.GenerateImageRequest generateImageRequest = getGenerateImageRequest(imagePrompt,
requestImageOptions);

// Make the request
StabilityAiApi.GenerateImageResponse generateImageResponse = this.stabilityAiApi
.generateImage(generateImageRequest);
var observationContext = ImageModelObservationContext.builder()
.imagePrompt(imagePrompt)
.provider(PROVIDER_NAME.value())
.build();

return ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION
.observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext,
this.observationRegistry)
.observe(() -> {
StabilityAiApi.GenerateImageResponse generateImageResponse = this.stabilityAiApi
.generateImage(generateImageRequest);

ImageResponse imageResponse = convertResponse(generateImageResponse);

observationContext.setResponse(imageResponse);

// Convert to org.springframework.ai.model derived ImageResponse data type
return convertResponse(generateImageResponse);
return imageResponse;
});
}

private ImageResponse convertResponse(StabilityAiApi.GenerateImageResponse generateImageResponse) {
Expand Down Expand Up @@ -153,4 +194,13 @@ StabilityAiImageOptions mergeOptions(ImageOptions runtimeOptions, StabilityAiIma
return builder.build();
}

/**
* Use the provided convention for reporting observation data.
* @param observationConvention The provided convention
*/
public void setObservationConvention(ImageModelObservationConvention observationConvention) {
Assert.notNull(observationConvention, "observationConvention cannot be null");
this.observationConvention = observationConvention;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,10 +34,36 @@
*/
public class StabilityAiApi {

public static final String DEFAULT_IMAGE_MODEL = "stable-diffusion-v1-6";
public static final String DEFAULT_IMAGE_MODEL = ImageModel.STABLE_DIFFUSION_XL_1024_V1_0.getValue();

public static final String DEFAULT_BASE_URL = "https://api.stability.ai/v1";

/**
* Stability AI Image Models.
*/
public enum ImageModel {

/**
* Stable Diffusion XL 1024 v1.0.
*/
STABLE_DIFFUSION_XL_1024_V1_0("stable-diffusion-xl-1024-v1-0");

private final String value;

ImageModel(String value) {
this.value = value;
}

/**
* Return the value of the model.
* @return the value of the model
*/
public String getValue() {
return this.value;
}

}

private final RestClient restClient;

private final String apiKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,7 @@ public class StabilityAiImageOptions implements ImageOptions {
* The engine/model to use in Stability AI The model is passed in the URL as a path
* parameter
*
* The default value is stable-diffusion-v1-6
* The default value is stable-diffusion-xl-1024-v1-0
*/
private String model = StabilityAiApi.DEFAULT_IMAGE_MODEL;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2026-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.stabilityai;

import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.image.observation.DefaultImageModelObservationConvention;
import org.springframework.ai.observation.conventions.AiOperationType;
import org.springframework.ai.observation.conventions.AiProvider;
import org.springframework.ai.stabilityai.api.StabilityAiApi;
import org.springframework.ai.stabilityai.api.StabilityAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.HighCardinalityKeyNames;
import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.LowCardinalityKeyNames;

/**
* Integration tests for observation instrumentation in {@link StabilityAiImageModel}.
*/
@SpringBootTest(classes = StabilityAiImageModelObservationIT.Config.class)
@EnabledIfEnvironmentVariable(named = "STABILITYAI_API_KEY", matches = ".+")
public class StabilityAiImageModelObservationIT {

@Autowired
TestObservationRegistry observationRegistry;

@Autowired
StabilityAiImageModel imageModel;

@Test
void observationForImageOperation() {
var options = StabilityAiImageOptions.builder()
.model(StabilityAiApi.DEFAULT_IMAGE_MODEL)
.N(1)
.height(1024)
.width(1024)
.build();

var instructions = "A cute baby sea otter";

ImagePrompt imagePrompt = new ImagePrompt(instructions, options);

ImageResponse imageResponse = this.imageModel.call(imagePrompt);
assertThat(imageResponse.getResults()).hasSize(1);

TestObservationRegistryAssert.assertThat(this.observationRegistry)
.doesNotHaveAnyRemainingCurrentObservation()
.hasObservationWithNameEqualTo(DefaultImageModelObservationConvention.DEFAULT_NAME)
.that()
.hasContextualNameEqualTo("image " + StabilityAiApi.DEFAULT_IMAGE_MODEL)
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(),
AiOperationType.IMAGE.value())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.STABILITY_AI.value())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(),
StabilityAiApi.DEFAULT_IMAGE_MODEL)
.hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_IMAGE_SIZE.asString(), "1024x1024")
.hasBeenStarted()
.hasBeenStopped();
}

@SpringBootConfiguration
static class Config {

@Bean
public TestObservationRegistry observationRegistry() {
return TestObservationRegistry.create();
}

@Bean
public StabilityAiApi stabilityAiApi() {
return new StabilityAiApi(System.getenv("STABILITYAI_API_KEY"));
}

@Bean
public StabilityAiImageModel stabilityAiImageModel(StabilityAiApi stabilityAiApi,
TestObservationRegistry observationRegistry) {
return new StabilityAiImageModel(stabilityAiApi, StabilityAiImageOptions.builder().build(),
observationRegistry);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2025 the original author or authors.
* Copyright 2023-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,6 +95,11 @@ public enum AiProvider {
*/
SPRING_AI("spring_ai"),

/**
* AI system provided by Stability AI.
*/
STABILITY_AI("stability_ai"),

/**
* AI system provided by Vertex AI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The prefix `spring.ai.stabilityai.image` is the property prefix that lets you co
| spring.ai.stabilityai.image.base-url | Optional overrides the spring.ai.openai.base-url to provide a specific url | `+https://api.stability.ai/v1+`
| spring.ai.stabilityai.image.api-key | Optional overrides the spring.ai.openai.api-key to provide a specific api-key | -
| spring.ai.stabilityai.image.option.n | The number of images to be generated. Must be between 1 and 10. | 1
| spring.ai.stabilityai.image.option.model | The engine/model to use in Stability AI. The model is passed in the URL as a path parameter. | `stable-diffusion-v1-6`
| spring.ai.stabilityai.image.option.model | The engine/model to use in Stability AI. The model is passed in the URL as a path parameter. | `stable-diffusion-xl-1024-v1-0`
| spring.ai.stabilityai.image.option.width | Width of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies. | 512
| spring.ai.stabilityai.image.option.height | Height of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies.| 512
| spring.ai.stabilityai.image.option.responseFormat | The format in which the generated images are returned. Must be "application/json" or "image/png". | -
Expand Down