Commit c3e36c19ead84ae4706103470c49cccb440a0ce7

Authored by cherishsince
1 parent 6a3cafb4

复制 spring-ai image

yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/Image.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import java.util.Objects;
  20 +
  21 +public class Image {
  22 +
  23 + /**
  24 + * The URL where the image can be accessed.
  25 + */
  26 + private String url;
  27 +
  28 + /**
  29 + * Base64 encoded image string.
  30 + */
  31 + private String b64Json;
  32 +
  33 + public Image(String url, String b64Json) {
  34 + this.url = url;
  35 + this.b64Json = b64Json;
  36 + }
  37 +
  38 + public String getUrl() {
  39 + return url;
  40 + }
  41 +
  42 + public void setUrl(String url) {
  43 + this.url = url;
  44 + }
  45 +
  46 + public String getB64Json() {
  47 + return b64Json;
  48 + }
  49 +
  50 + public void setB64Json(String b64Json) {
  51 + this.b64Json = b64Json;
  52 + }
  53 +
  54 + @Override
  55 + public String toString() {
  56 + return "Image{" + "url='" + url + '\'' + ", b64Json='" + b64Json + '\'' + '}';
  57 + }
  58 +
  59 + @Override
  60 + public boolean equals(Object o) {
  61 + if (this == o)
  62 + return true;
  63 + if (!(o instanceof Image image))
  64 + return false;
  65 + return Objects.equals(url, image.url) && Objects.equals(b64Json, image.b64Json);
  66 + }
  67 +
  68 + @Override
  69 + public int hashCode() {
  70 + return Objects.hash(url, b64Json);
  71 + }
  72 +
  73 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageClient.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +
  20 +import cn.iocoder.yudao.framework.ai.model.ModelClient;
  21 +
  22 +@FunctionalInterface
  23 +public interface ImageClient extends ModelClient<ImagePrompt, ImageResponse> {
  24 +
  25 + ImageResponse call(ImagePrompt request);
  26 +
  27 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageGeneration.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +
  20 +import cn.iocoder.yudao.framework.ai.model.ModelResult;
  21 +
  22 +public class ImageGeneration implements ModelResult<Image> {
  23 +
  24 + private ImageGenerationMetadata imageGenerationMetadata;
  25 +
  26 + private Image image;
  27 +
  28 + public ImageGeneration(Image image) {
  29 + this.image = image;
  30 + }
  31 +
  32 + public ImageGeneration(Image image, ImageGenerationMetadata imageGenerationMetadata) {
  33 + this.image = image;
  34 + this.imageGenerationMetadata = imageGenerationMetadata;
  35 + }
  36 +
  37 + @Override
  38 + public Image getOutput() {
  39 + return image;
  40 + }
  41 +
  42 + @Override
  43 + public ImageGenerationMetadata getMetadata() {
  44 + return imageGenerationMetadata;
  45 + }
  46 +
  47 + @Override
  48 + public String toString() {
  49 + return "ImageGeneration{" + "imageGenerationMetadata=" + imageGenerationMetadata + ", image=" + image + '}';
  50 + }
  51 +
  52 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageGenerationMetadata.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import cn.iocoder.yudao.framework.ai.model.ResultMetadata;
  20 +
  21 +public interface ImageGenerationMetadata extends ResultMetadata {
  22 +
  23 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageMessage.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import java.util.Objects;
  20 +
  21 +public class ImageMessage {
  22 +
  23 + private String text;
  24 +
  25 + private Float weight;
  26 +
  27 + public ImageMessage(String text) {
  28 + this.text = text;
  29 + }
  30 +
  31 + public ImageMessage(String text, Float weight) {
  32 + this.text = text;
  33 + this.weight = weight;
  34 + }
  35 +
  36 + public String getText() {
  37 + return text;
  38 + }
  39 +
  40 + public Float getWeight() {
  41 + return weight;
  42 + }
  43 +
  44 + @Override
  45 + public String toString() {
  46 + return "mageMessage{" + "text='" + text + '\'' + ", weight=" + weight + '}';
  47 + }
  48 +
  49 + @Override
  50 + public boolean equals(Object o) {
  51 + if (this == o)
  52 + return true;
  53 + if (!(o instanceof ImageMessage that))
  54 + return false;
  55 + return Objects.equals(text, that.text) && Objects.equals(weight, that.weight);
  56 + }
  57 +
  58 + @Override
  59 + public int hashCode() {
  60 + return Objects.hash(text, weight);
  61 + }
  62 +
  63 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageOptions.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import cn.iocoder.yudao.framework.ai.model.ModelOptions;
  20 +
  21 +/**
  22 + * ImageOptions represent the common options, portable across different image generation
  23 + * models.
  24 + */
  25 +public interface ImageOptions extends ModelOptions {
  26 +
  27 + Integer getN();
  28 +
  29 + String getModel();
  30 +
  31 + Integer getWidth();
  32 +
  33 + Integer getHeight();
  34 +
  35 + String getResponseFormat(); // openai - url or base64 : stability ai byte[] or base64
  36 +
  37 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageOptionsBuilder.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +public class ImageOptionsBuilder {
  20 +
  21 + private class ImageModelOptionsImpl implements ImageOptions {
  22 +
  23 + private Integer n;
  24 +
  25 + private String model;
  26 +
  27 + private Integer width;
  28 +
  29 + private Integer height;
  30 +
  31 + private String responseFormat;
  32 +
  33 + @Override
  34 + public Integer getN() {
  35 + return n;
  36 + }
  37 +
  38 + public void setN(Integer n) {
  39 + this.n = n;
  40 + }
  41 +
  42 + @Override
  43 + public String getModel() {
  44 + return model;
  45 + }
  46 +
  47 + public void setModel(String model) {
  48 + this.model = model;
  49 + }
  50 +
  51 + @Override
  52 + public String getResponseFormat() {
  53 + return responseFormat;
  54 + }
  55 +
  56 + public void setResponseFormat(String responseFormat) {
  57 + this.responseFormat = responseFormat;
  58 + }
  59 +
  60 + @Override
  61 + public Integer getWidth() {
  62 + return width;
  63 + }
  64 +
  65 + public void setWidth(Integer width) {
  66 + this.width = width;
  67 + }
  68 +
  69 + @Override
  70 + public Integer getHeight() {
  71 + return height;
  72 + }
  73 +
  74 + public void setHeight(Integer height) {
  75 + this.height = height;
  76 + }
  77 +
  78 + }
  79 +
  80 + private final ImageModelOptionsImpl options = new ImageModelOptionsImpl();
  81 +
  82 + private ImageOptionsBuilder() {
  83 +
  84 + }
  85 +
  86 + public static ImageOptionsBuilder builder() {
  87 + return new ImageOptionsBuilder();
  88 + }
  89 +
  90 + public ImageOptionsBuilder withN(Integer n) {
  91 + options.setN(n);
  92 + return this;
  93 + }
  94 +
  95 + public ImageOptionsBuilder withModel(String model) {
  96 + options.setModel(model);
  97 + return this;
  98 + }
  99 +
  100 + public ImageOptionsBuilder withResponseFormat(String responseFormat) {
  101 + options.setResponseFormat(responseFormat);
  102 + return this;
  103 + }
  104 +
  105 + public ImageOptionsBuilder withWidth(Integer width) {
  106 + options.setWidth(width);
  107 + return this;
  108 + }
  109 +
  110 + public ImageOptionsBuilder withHeight(Integer height) {
  111 + options.setHeight(height);
  112 + return this;
  113 + }
  114 +
  115 + public ImageOptions build() {
  116 + return options;
  117 + }
  118 +
  119 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImagePrompt.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import cn.iocoder.yudao.framework.ai.model.ModelRequest;
  20 +
  21 +import java.util.Collections;
  22 +import java.util.List;
  23 +import java.util.Objects;
  24 +
  25 +/**
  26 + * 图片内容
  27 + */
  28 +public class ImagePrompt implements ModelRequest<List<ImageMessage>> {
  29 +
  30 + private final List<ImageMessage> messages;
  31 +
  32 + private ImageOptions imageModelOptions;
  33 +
  34 + public ImagePrompt(List<ImageMessage> messages) {
  35 + this.messages = messages;
  36 + }
  37 +
  38 + public ImagePrompt(List<ImageMessage> messages, ImageOptions imageModelOptions) {
  39 + this.messages = messages;
  40 + this.imageModelOptions = imageModelOptions;
  41 + }
  42 +
  43 + public ImagePrompt(ImageMessage imageMessage, ImageOptions imageOptions) {
  44 + this(Collections.singletonList(imageMessage), imageOptions);
  45 + }
  46 +
  47 + public ImagePrompt(String instructions, ImageOptions imageOptions) {
  48 + this(new ImageMessage(instructions), imageOptions);
  49 + }
  50 +
  51 + public ImagePrompt(String instructions) {
  52 + this(new ImageMessage(instructions), ImageOptionsBuilder.builder().build());
  53 + }
  54 +
  55 + @Override
  56 + public List<ImageMessage> getInstructions() {
  57 + return messages;
  58 + }
  59 +
  60 + @Override
  61 + public ImageOptions getOptions() {
  62 + return imageModelOptions;
  63 + }
  64 +
  65 + @Override
  66 + public String toString() {
  67 + return "NewImagePrompt{" + "messages=" + messages + ", imageModelOptions=" + imageModelOptions + '}';
  68 + }
  69 +
  70 + @Override
  71 + public boolean equals(Object o) {
  72 + if (this == o)
  73 + return true;
  74 + if (!(o instanceof ImagePrompt that))
  75 + return false;
  76 + return Objects.equals(messages, that.messages) && Objects.equals(imageModelOptions, that.imageModelOptions);
  77 + }
  78 +
  79 + @Override
  80 + public int hashCode() {
  81 + return Objects.hash(messages, imageModelOptions);
  82 + }
  83 +
  84 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageResponse.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +import cn.iocoder.yudao.framework.ai.model.ModelResponse;
  20 +
  21 +import java.util.List;
  22 +import java.util.Objects;
  23 +
  24 +public class ImageResponse implements ModelResponse<ImageGeneration> {
  25 +
  26 + private final ImageResponseMetadata imageResponseMetadata;
  27 +
  28 + private final List<ImageGeneration> imageGenerations;
  29 +
  30 + public ImageResponse(List<ImageGeneration> generations) {
  31 + this(generations, ImageResponseMetadata.NULL);
  32 + }
  33 +
  34 + public ImageResponse(List<ImageGeneration> generations, ImageResponseMetadata imageResponseMetadata) {
  35 + this.imageResponseMetadata = imageResponseMetadata;
  36 + this.imageGenerations = List.copyOf(generations);
  37 + }
  38 +
  39 + @Override
  40 + public ImageGeneration getResult() {
  41 + return imageGenerations.get(0);
  42 + }
  43 +
  44 + @Override
  45 + public List<ImageGeneration> getResults() {
  46 + return imageGenerations;
  47 + }
  48 +
  49 + @Override
  50 + public ImageResponseMetadata getMetadata() {
  51 + return imageResponseMetadata;
  52 + }
  53 +
  54 + @Override
  55 + public String toString() {
  56 + return "ImageResponse{" + "imageResponseMetadata=" + imageResponseMetadata + ", imageGenerations="
  57 + + imageGenerations + '}';
  58 + }
  59 +
  60 + @Override
  61 + public boolean equals(Object o) {
  62 + if (this == o)
  63 + return true;
  64 + if (!(o instanceof ImageResponse that))
  65 + return false;
  66 + return Objects.equals(imageResponseMetadata, that.imageResponseMetadata)
  67 + && Objects.equals(imageGenerations, that.imageGenerations);
  68 + }
  69 +
  70 + @Override
  71 + public int hashCode() {
  72 + return Objects.hash(imageResponseMetadata, imageGenerations);
  73 + }
  74 +
  75 +}
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/image/ImageResponseMetadata.java 0 → 100644
  1 +/*
  2 + * Copyright 2024-2024 the original author or authors.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * https://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package cn.iocoder.yudao.framework.ai.image;
  18 +
  19 +
  20 +import cn.iocoder.yudao.framework.ai.model.ResponseMetadata;
  21 +
  22 +public interface ImageResponseMetadata extends ResponseMetadata {
  23 +
  24 + ImageResponseMetadata NULL = new ImageResponseMetadata() {
  25 + };
  26 +
  27 + default Long created() {
  28 + return System.currentTimeMillis();
  29 + }
  30 +
  31 +}
... ...