【功能新增】AI:画图通用功能增加硅基流动平台
This commit is contained in:
parent
acf68b1cec
commit
7b3401e216
|
@ -11,6 +11,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
|||
import cn.hutool.http.HttpUtil;
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconflowImageOptions;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDrawReqVO;
|
||||
|
@ -144,7 +145,12 @@ public class AiImageServiceImpl implements AiImageService {
|
|||
.withStyle(MapUtil.getStr(draw.getOptions(), "style")) // 风格
|
||||
.withResponseFormat("b64_json")
|
||||
.build();
|
||||
} else if (ObjUtil.equal(model.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) {
|
||||
} else if (ObjUtil.equal(model.getPlatform(), AiPlatformEnum.SILICON_FLOW.getPlatform())) {
|
||||
// https://docs.siliconflow.cn/cn/api-reference/images/images-generations
|
||||
return SiliconflowImageOptions.builder().withModel(model.getModel())
|
||||
.withHeight(draw.getHeight()).withWidth(draw.getWidth())
|
||||
.build();
|
||||
} else if (ObjUtil.equal(model.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) {
|
||||
// https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage
|
||||
// https://platform.stability.ai/docs/api-reference#tag/Text-to-Image/operation/textToImage
|
||||
return StabilityAiImageOptions.builder().model(model.getModel())
|
||||
|
|
|
@ -8,6 +8,8 @@ import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatModel;
|
|||
import cn.iocoder.yudao.framework.ai.core.model.doubao.DouBaoChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.hunyuan.HunYuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiiconflowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiiconflowmageApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
||||
|
@ -113,11 +115,11 @@ public class YudaoAiAutoConfiguration {
|
|||
|
||||
public SiliconFlowChatModel buildSiliconFlowChatClient(YudaoAiProperties.SiliconFlowProperties properties) {
|
||||
if (StrUtil.isEmpty(properties.getModel())) {
|
||||
properties.setModel(SiliconFlowChatModel.MODEL_DEFAULT);
|
||||
properties.setModel(SiiconflowApiConstants.MODEL_DEFAULT);
|
||||
}
|
||||
OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(SiliconFlowChatModel.BASE_URL)
|
||||
.baseUrl(SiiconflowApiConstants.DEFAULT_BASE_URL)
|
||||
.apiKey(properties.getApiKey())
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
|
|
|
@ -15,7 +15,10 @@ import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatModel;
|
|||
import cn.iocoder.yudao.framework.ai.core.model.doubao.DouBaoChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.hunyuan.HunYuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiiconflowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiiconflowmageApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconflowImageModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
||||
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
|
||||
|
@ -224,6 +227,8 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
|||
return buildZhiPuAiImageModel(apiKey, url);
|
||||
case OPENAI:
|
||||
return buildOpenAiImageModel(apiKey, url);
|
||||
case SILICON_FLOW:
|
||||
return buildSiiconflowImageModel(apiKey,url);
|
||||
case STABLE_DIFFUSION:
|
||||
return buildStabilityAiImageModel(apiKey, url);
|
||||
default:
|
||||
|
@ -468,6 +473,15 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
|||
return new OpenAiImageModel(openAiApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Siiconflow
|
||||
*/
|
||||
private SiliconflowImageModel buildSiiconflowImageModel(String apiToken, String url) {
|
||||
url = StrUtil.blankToDefault(url, SiiconflowApiConstants.DEFAULT_BASE_URL);
|
||||
SiiconflowmageApi openAiApi = SiiconflowmageApi.builder().baseUrl(url).apiKey(apiToken).build();
|
||||
return new SiliconflowImageModel(openAiApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link OllamaAutoConfiguration} 的 ollamaApi 方法
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2023-2024 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 cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
/**
|
||||
* Common value constants for Siiconflow api.
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public final class SiiconflowApiConstants {
|
||||
|
||||
public static final String DEFAULT_BASE_URL = "https://api.siliconflow.cn";
|
||||
|
||||
public static final String MODEL_DEFAULT = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B";
|
||||
|
||||
public static final String PROVIDER_NAME = "Siiconflow";
|
||||
|
||||
private SiiconflowApiConstants() {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,207 @@
|
|||
/*
|
||||
* Copyright 2023-2024 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 cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.ai.model.ApiKey;
|
||||
import org.springframework.ai.model.NoopApiKey;
|
||||
import org.springframework.ai.model.SimpleApiKey;
|
||||
import org.springframework.ai.openai.api.OpenAiImageApi;
|
||||
import org.springframework.ai.retry.RetryUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Siiconflow Image API.
|
||||
*
|
||||
* @see <a href= "https://docs.siliconflow.cn/cn/api-reference/images/images-generations">Images</a>
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public class SiiconflowmageApi {
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
/**
|
||||
* Create a new Siiconflow Image api with base URL set.
|
||||
* @param aiToken OpenAI apiKey.
|
||||
*/
|
||||
public SiiconflowmageApi(String aiToken) {
|
||||
this(SiiconflowApiConstants.DEFAULT_BASE_URL, aiToken, RestClient.builder());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Siiconflow Image API with the provided base URL.
|
||||
* @param baseUrl the base URL for the OpenAI API.
|
||||
* @param openAiToken Siiconflow apiKey.
|
||||
*/
|
||||
public SiiconflowmageApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder) {
|
||||
this(baseUrl, openAiToken, restClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new OpenAI Image API with the provided base URL.
|
||||
* @param baseUrl the base URL for the OpenAI API.
|
||||
* @param apiKey OpenAI apiKey.
|
||||
* @param restClientBuilder the rest client builder to use.
|
||||
*/
|
||||
public SiiconflowmageApi(String baseUrl, String apiKey, RestClient.Builder restClientBuilder,
|
||||
ResponseErrorHandler responseErrorHandler) {
|
||||
this(baseUrl, apiKey, CollectionUtils.toMultiValueMap(Map.of()), restClientBuilder, responseErrorHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new OpenAI Image API with the provided base URL.
|
||||
* @param baseUrl the base URL for the OpenAI API.
|
||||
* @param apiKey OpenAI apiKey.
|
||||
* @param headers the http headers to use.
|
||||
* @param restClientBuilder the rest client builder to use.
|
||||
* @param responseErrorHandler the response error handler to use.
|
||||
*/
|
||||
public SiiconflowmageApi(String baseUrl, String apiKey, MultiValueMap<String, String> headers,
|
||||
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
|
||||
|
||||
this(baseUrl, new SimpleApiKey(apiKey), headers, restClientBuilder, responseErrorHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new OpenAI Image API with the provided base URL.
|
||||
* @param baseUrl the base URL for the OpenAI API.
|
||||
* @param apiKey OpenAI apiKey.
|
||||
* @param headers the http headers to use.
|
||||
* @param restClientBuilder the rest client builder to use.
|
||||
* @param responseErrorHandler the response error handler to use.
|
||||
*/
|
||||
public SiiconflowmageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
|
||||
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
|
||||
|
||||
// @formatter:off
|
||||
this.restClient = restClientBuilder.baseUrl(baseUrl)
|
||||
.defaultHeaders(h -> {
|
||||
if(!(apiKey instanceof NoopApiKey)) {
|
||||
h.setBearerAuth(apiKey.getValue());
|
||||
}
|
||||
h.setContentType(MediaType.APPLICATION_JSON);
|
||||
h.addAll(headers);
|
||||
})
|
||||
.defaultStatusHandler(responseErrorHandler)
|
||||
.build();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
public ResponseEntity<OpenAiImageApi.OpenAiImageResponse> createImage(SiliconflowImageRequest siliconflowImageRequest) {
|
||||
Assert.notNull(siliconflowImageRequest, "Image request cannot be null.");
|
||||
Assert.hasLength(siliconflowImageRequest.prompt(), "Prompt cannot be empty.");
|
||||
|
||||
return this.restClient.post()
|
||||
.uri("v1/images/generations")
|
||||
.body(siliconflowImageRequest)
|
||||
.retrieve()
|
||||
.toEntity(OpenAiImageApi.OpenAiImageResponse.class);
|
||||
}
|
||||
|
||||
|
||||
// @formatter:off
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record SiliconflowImageRequest (
|
||||
@JsonProperty("prompt") String prompt,
|
||||
@JsonProperty("model") String model,
|
||||
@JsonProperty("batch_size") Integer batchSize,
|
||||
@JsonProperty("negative_prompt") String negativePrompt,
|
||||
@JsonProperty("seed") Integer seed,
|
||||
@JsonProperty("num_inference_steps") Integer numInferenceSteps,
|
||||
@JsonProperty("guidance_scale") Float guidanceScale,
|
||||
@JsonProperty("image") String image) {
|
||||
|
||||
public SiliconflowImageRequest(String prompt, String model) {
|
||||
this(prompt, model, null, null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder to construct {@link SiiconflowmageApi} instance.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String baseUrl = SiiconflowApiConstants.DEFAULT_BASE_URL;
|
||||
|
||||
private ApiKey apiKey;
|
||||
|
||||
private MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
|
||||
private RestClient.Builder restClientBuilder = RestClient.builder();
|
||||
|
||||
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
|
||||
|
||||
public Builder baseUrl(String baseUrl) {
|
||||
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
|
||||
this.baseUrl = baseUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder apiKey(ApiKey apiKey) {
|
||||
Assert.notNull(apiKey, "apiKey cannot be null");
|
||||
this.apiKey = apiKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder apiKey(String simpleApiKey) {
|
||||
Assert.notNull(simpleApiKey, "simpleApiKey cannot be null");
|
||||
this.apiKey = new SimpleApiKey(simpleApiKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder headers(MultiValueMap<String, String> headers) {
|
||||
Assert.notNull(headers, "headers cannot be null");
|
||||
this.headers = headers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder restClientBuilder(RestClient.Builder restClientBuilder) {
|
||||
Assert.notNull(restClientBuilder, "restClientBuilder cannot be null");
|
||||
this.restClientBuilder = restClientBuilder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
|
||||
Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null");
|
||||
this.responseErrorHandler = responseErrorHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiiconflowmageApi build() {
|
||||
Assert.notNull(this.apiKey, "apiKey must be set");
|
||||
return new SiiconflowmageApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
|
||||
this.responseErrorHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -20,10 +20,6 @@ import reactor.core.publisher.Flux;
|
|||
@RequiredArgsConstructor
|
||||
public class SiliconFlowChatModel implements ChatModel {
|
||||
|
||||
public static final String BASE_URL = "https://api.siliconflow.cn";
|
||||
|
||||
public static final String MODEL_DEFAULT = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B";
|
||||
|
||||
/**
|
||||
* 兼容 OpenAI 接口,进行复用
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* Copyright 2023-2024 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 cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.image.*;
|
||||
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.openai.api.OpenAiImageApi;
|
||||
import org.springframework.ai.openai.api.common.OpenAiApiConstants;
|
||||
import org.springframework.ai.openai.metadata.OpenAiImageGenerationMetadata;
|
||||
import org.springframework.ai.retry.RetryUtils;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* cv openapi图片模型方法
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public class SiliconflowImageModel implements ImageModel {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SiliconflowImageModel.class);
|
||||
|
||||
private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention();
|
||||
|
||||
/**
|
||||
* The default options used for the image completion requests.
|
||||
*/
|
||||
private final SiliconflowImageOptions defaultOptions;
|
||||
|
||||
/**
|
||||
* The retry template used to retry the OpenAI Image API calls.
|
||||
*/
|
||||
private final RetryTemplate retryTemplate;
|
||||
|
||||
/**
|
||||
* Low-level access to the OpenAI Image API.
|
||||
*/
|
||||
private final SiiconflowmageApi siiconflowmageApi;
|
||||
|
||||
/**
|
||||
* Observation registry used for instrumentation.
|
||||
*/
|
||||
private final ObservationRegistry observationRegistry;
|
||||
|
||||
/**
|
||||
* Conventions to use for generating observations.
|
||||
*/
|
||||
private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;
|
||||
|
||||
/**
|
||||
* Creates an instance of the OpenAiImageModel.
|
||||
* @param siiconflowmageApi The OpenAiImageApi instance to be used for interacting with
|
||||
* the OpenAI Image API.
|
||||
* @throws IllegalArgumentException if openAiImageApi is null
|
||||
*/
|
||||
public SiliconflowImageModel(SiiconflowmageApi siiconflowmageApi) {
|
||||
this(siiconflowmageApi, SiliconflowImageOptions.builder().build(), RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the OpenAiImageModel.
|
||||
* @param siiconflowmageApi The OpenAiImageApi instance to be used for interacting with
|
||||
* the OpenAI Image API.
|
||||
* @param options The OpenAiImageOptions to configure the image model.
|
||||
* @param retryTemplate The retry template.
|
||||
*/
|
||||
public SiliconflowImageModel(SiiconflowmageApi siiconflowmageApi, SiliconflowImageOptions options, RetryTemplate retryTemplate) {
|
||||
this(siiconflowmageApi, options, retryTemplate, ObservationRegistry.NOOP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the OpenAiImageModel.
|
||||
* @param siiconflowmageApi The OpenAiImageApi instance to be used for interacting with
|
||||
* the OpenAI Image API.
|
||||
* @param options The OpenAiImageOptions to configure the image model.
|
||||
* @param retryTemplate The retry template.
|
||||
* @param observationRegistry The ObservationRegistry used for instrumentation.
|
||||
*/
|
||||
public SiliconflowImageModel(SiiconflowmageApi siiconflowmageApi, SiliconflowImageOptions options, RetryTemplate retryTemplate,
|
||||
ObservationRegistry observationRegistry) {
|
||||
Assert.notNull(siiconflowmageApi, "OpenAiImageApi must not be null");
|
||||
Assert.notNull(options, "options must not be null");
|
||||
Assert.notNull(retryTemplate, "retryTemplate must not be null");
|
||||
Assert.notNull(observationRegistry, "observationRegistry must not be null");
|
||||
this.siiconflowmageApi = siiconflowmageApi;
|
||||
this.defaultOptions = options;
|
||||
this.retryTemplate = retryTemplate;
|
||||
this.observationRegistry = observationRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse call(ImagePrompt imagePrompt) {
|
||||
SiiconflowmageApi.SiliconflowImageRequest imageRequest = createRequest(imagePrompt);
|
||||
|
||||
var observationContext = ImageModelObservationContext.builder()
|
||||
.imagePrompt(imagePrompt)
|
||||
.provider(OpenAiApiConstants.PROVIDER_NAME)
|
||||
.requestOptions(imagePrompt.getOptions())
|
||||
.build();
|
||||
|
||||
return ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION
|
||||
.observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext,
|
||||
this.observationRegistry)
|
||||
.observe(() -> {
|
||||
ResponseEntity<OpenAiImageApi.OpenAiImageResponse> imageResponseEntity = this.retryTemplate
|
||||
.execute(ctx -> this.siiconflowmageApi.createImage(imageRequest));
|
||||
|
||||
ImageResponse imageResponse = convertResponse(imageResponseEntity, imageRequest);
|
||||
|
||||
observationContext.setResponse(imageResponse);
|
||||
|
||||
return imageResponse;
|
||||
});
|
||||
}
|
||||
|
||||
private SiiconflowmageApi.SiliconflowImageRequest createRequest(ImagePrompt imagePrompt) {
|
||||
String instructions = imagePrompt.getInstructions().get(0).getText();
|
||||
|
||||
SiiconflowmageApi.SiliconflowImageRequest imageRequest = new SiiconflowmageApi.SiliconflowImageRequest(instructions,
|
||||
imagePrompt.getOptions().getModel());
|
||||
|
||||
return ModelOptionsUtils.merge(imagePrompt.getOptions(), imageRequest, SiiconflowmageApi.SiliconflowImageRequest.class);
|
||||
}
|
||||
|
||||
private ImageResponse convertResponse(ResponseEntity<OpenAiImageApi.OpenAiImageResponse> imageResponseEntity,
|
||||
SiiconflowmageApi.SiliconflowImageRequest siliconflowImageRequest) {
|
||||
OpenAiImageApi.OpenAiImageResponse imageApiResponse = imageResponseEntity.getBody();
|
||||
if (imageApiResponse == null) {
|
||||
logger.warn("No image response returned for request: {}", siliconflowImageRequest);
|
||||
return new ImageResponse(List.of());
|
||||
}
|
||||
|
||||
List<ImageGeneration> imageGenerationList = imageApiResponse.data()
|
||||
.stream()
|
||||
.map(entry -> new ImageGeneration(new Image(entry.url(), entry.b64Json()),
|
||||
new OpenAiImageGenerationMetadata(entry.revisedPrompt())))
|
||||
.toList();
|
||||
|
||||
ImageResponseMetadata openAiImageResponseMetadata = new ImageResponseMetadata(imageApiResponse.created());
|
||||
return new ImageResponse(imageGenerationList, openAiImageResponseMetadata);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
package cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.ai.image.ImageOptions;
|
||||
import org.springframework.ai.openai.OpenAiImageOptions;
|
||||
|
||||
/**
|
||||
* 硅基流动画图能力
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
@Data
|
||||
public class SiliconflowImageOptions implements ImageOptions {
|
||||
|
||||
@JsonProperty("model")
|
||||
private String model;
|
||||
|
||||
@JsonProperty("negative_prompt")
|
||||
private String negativePrompt;
|
||||
|
||||
/**
|
||||
* The number of images to generate. Must be between 1 and 4.
|
||||
*/
|
||||
@JsonProperty("image_size")
|
||||
private String imageSize;
|
||||
|
||||
/**
|
||||
* The number of images to generate. Must be between 1 and 4.
|
||||
*/
|
||||
@JsonProperty("batch_size")
|
||||
private Integer batchSize = 1;
|
||||
|
||||
/**
|
||||
* number of inference steps
|
||||
*/
|
||||
@JsonProperty("num_inference_steps")
|
||||
private Integer numInferenceSteps = 25;
|
||||
|
||||
|
||||
/**
|
||||
* This value is used to control the degree of match between the generated image and the given prompt. The higher the value, the more the generated image will tend to strictly match the text prompt. The lower the value, the more creative and diverse the generated image will be, potentially containing more unexpected elements.
|
||||
*
|
||||
* Required range: 0 <= x <= 20
|
||||
*/
|
||||
@JsonProperty("guidance_scale")
|
||||
private Float guidanceScale = 0.75F;
|
||||
|
||||
/**
|
||||
* 如果想要每次都生成固定的图片,可以把seed设置为固定值。
|
||||
*
|
||||
*/
|
||||
@JsonProperty("seed")
|
||||
private Integer seed = (int)(Math.random() * 1_000_000_000);
|
||||
|
||||
/**
|
||||
* The image that needs to be uploaded should be converted into base64 format.
|
||||
*/
|
||||
@JsonProperty("image")
|
||||
private String image;
|
||||
|
||||
|
||||
/**
|
||||
* 宽
|
||||
*/
|
||||
private Integer width;
|
||||
|
||||
|
||||
/**
|
||||
* 高
|
||||
*/
|
||||
private Integer height;
|
||||
|
||||
public void setHeight(Integer height) {
|
||||
this.height = height;
|
||||
if (this.width != null && this.height != null) {
|
||||
this.imageSize = this.width + "x" + this.height;
|
||||
}
|
||||
}
|
||||
|
||||
public void setWidth(Integer width) {
|
||||
this.width = width;
|
||||
if (this.width != null && this.height != null) {
|
||||
this.imageSize = this.width + "x" + this.height;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 硅基流动
|
||||
* @return
|
||||
*/
|
||||
public static SiliconflowImageOptions.Builder builder() {
|
||||
return new SiliconflowImageOptions.Builder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
return "SiliconflowImageOptions{" + "model='" + getModel() + '\'' + ", batch_size=" + batchSize + ", imageSize=" + imageSize + ", negativePrompt='"
|
||||
+ negativePrompt + '\'' + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getN() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResponseFormat() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStyle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Builder extends OpenAiImageOptions{
|
||||
|
||||
private final SiliconflowImageOptions options;
|
||||
|
||||
private Builder() {
|
||||
this.options = new SiliconflowImageOptions();
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder model(String model) {
|
||||
this.options.setModel(model);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withBatchSize(Integer batchSize) {
|
||||
options.setBatchSize(batchSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withModel(String model) {
|
||||
options.setModel(model);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withWidth(Integer width) {
|
||||
options.setWidth(width);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withHeight(Integer height) {
|
||||
options.setHeight(height);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withSeed(Integer seed) {
|
||||
options.setSeed(seed);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions.Builder withNegativePrompt(String negativePrompt) {
|
||||
options.setNegativePrompt(negativePrompt);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SiliconflowImageOptions build() {
|
||||
return options;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -50,8 +50,8 @@ public class AiUtils {
|
|||
case HUN_YUAN: // 复用 OpenAI 客户端
|
||||
case XING_HUO: // 复用 OpenAI 客户端
|
||||
case SILICON_FLOW: // 复用 OpenAI 客户端
|
||||
return OpenAiChatOptions.builder().model(model).temperature(temperature).maxTokens(maxTokens)
|
||||
.toolNames(toolNames).build();
|
||||
OpenAiChatOptions.Builder builder = OpenAiChatOptions.builder().model(model).temperature(temperature).maxTokens(maxTokens);
|
||||
return toolNames == null ? builder.build() : builder.toolNames(toolNames).build();
|
||||
case AZURE_OPENAI:
|
||||
// TODO 芋艿:貌似没 model 字段???!
|
||||
return AzureOpenAiChatOptions.builder().deploymentName(model).temperature(temperature).maxTokens(maxTokens)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.yudao.framework.ai.chat;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiiconflowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -25,11 +26,11 @@ public class SiliconFlowChatModelTests {
|
|||
|
||||
private final OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(SiliconFlowChatModel.BASE_URL)
|
||||
.baseUrl(SiiconflowApiConstants.DEFAULT_BASE_URL)
|
||||
.apiKey("sk-epsakfenqnyzoxhmbucsxlhkdqlcbnimslqoivkshalvdozz") // apiKey
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.model(SiliconFlowChatModel.MODEL_DEFAULT) // 模型
|
||||
.model(SiiconflowApiConstants.MODEL_DEFAULT) // 模型
|
||||
// .model("deepseek-ai/DeepSeek-R1") // 模型(deepseek-ai/DeepSeek-R1)可用赠费
|
||||
// .model("Pro/deepseek-ai/DeepSeek-R1") // 模型(Pro/deepseek-ai/DeepSeek-R1)需要付费
|
||||
.temperature(0.7)
|
||||
|
|
Loading…
Reference in New Issue