mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 18:49:06 +08:00 
			
		
		
		
	复制 spring-ai image
This commit is contained in:
		| @ -0,0 +1,73 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import java.util.Objects; | ||||
|  | ||||
| public class Image { | ||||
|  | ||||
| 	/** | ||||
| 	 * The URL where the image can be accessed. | ||||
| 	 */ | ||||
| 	private String url; | ||||
|  | ||||
| 	/** | ||||
| 	 * Base64 encoded image string. | ||||
| 	 */ | ||||
| 	private String b64Json; | ||||
|  | ||||
| 	public Image(String url, String b64Json) { | ||||
| 		this.url = url; | ||||
| 		this.b64Json = b64Json; | ||||
| 	} | ||||
|  | ||||
| 	public String getUrl() { | ||||
| 		return url; | ||||
| 	} | ||||
|  | ||||
| 	public void setUrl(String url) { | ||||
| 		this.url = url; | ||||
| 	} | ||||
|  | ||||
| 	public String getB64Json() { | ||||
| 		return b64Json; | ||||
| 	} | ||||
|  | ||||
| 	public void setB64Json(String b64Json) { | ||||
| 		this.b64Json = b64Json; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "Image{" + "url='" + url + '\'' + ", b64Json='" + b64Json + '\'' + '}'; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object o) { | ||||
| 		if (this == o) | ||||
| 			return true; | ||||
| 		if (!(o instanceof Image image)) | ||||
| 			return false; | ||||
| 		return Objects.equals(url, image.url) && Objects.equals(b64Json, image.b64Json); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return Objects.hash(url, b64Json); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,27 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ModelClient; | ||||
|  | ||||
| @FunctionalInterface | ||||
| public interface ImageClient extends ModelClient<ImagePrompt, ImageResponse> { | ||||
|  | ||||
| 	ImageResponse call(ImagePrompt request); | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,52 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ModelResult; | ||||
|  | ||||
| public class ImageGeneration implements ModelResult<Image> { | ||||
|  | ||||
| 	private ImageGenerationMetadata imageGenerationMetadata; | ||||
|  | ||||
| 	private Image image; | ||||
|  | ||||
| 	public ImageGeneration(Image image) { | ||||
| 		this.image = image; | ||||
| 	} | ||||
|  | ||||
| 	public ImageGeneration(Image image, ImageGenerationMetadata imageGenerationMetadata) { | ||||
| 		this.image = image; | ||||
| 		this.imageGenerationMetadata = imageGenerationMetadata; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Image getOutput() { | ||||
| 		return image; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public ImageGenerationMetadata getMetadata() { | ||||
| 		return imageGenerationMetadata; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "ImageGeneration{" + "imageGenerationMetadata=" + imageGenerationMetadata + ", image=" + image + '}'; | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,23 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ResultMetadata; | ||||
|  | ||||
| public interface ImageGenerationMetadata extends ResultMetadata { | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,63 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import java.util.Objects; | ||||
|  | ||||
| public class ImageMessage { | ||||
|  | ||||
| 	private String text; | ||||
|  | ||||
| 	private Float weight; | ||||
|  | ||||
| 	public ImageMessage(String text) { | ||||
| 		this.text = text; | ||||
| 	} | ||||
|  | ||||
| 	public ImageMessage(String text, Float weight) { | ||||
| 		this.text = text; | ||||
| 		this.weight = weight; | ||||
| 	} | ||||
|  | ||||
| 	public String getText() { | ||||
| 		return text; | ||||
| 	} | ||||
|  | ||||
| 	public Float getWeight() { | ||||
| 		return weight; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "mageMessage{" + "text='" + text + '\'' + ", weight=" + weight + '}'; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object o) { | ||||
| 		if (this == o) | ||||
| 			return true; | ||||
| 		if (!(o instanceof ImageMessage that)) | ||||
| 			return false; | ||||
| 		return Objects.equals(text, that.text) && Objects.equals(weight, that.weight); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return Objects.hash(text, weight); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,37 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ModelOptions; | ||||
|  | ||||
| /** | ||||
|  * ImageOptions represent the common options, portable across different image generation | ||||
|  * models. | ||||
|  */ | ||||
| public interface ImageOptions extends ModelOptions { | ||||
|  | ||||
| 	Integer getN(); | ||||
|  | ||||
| 	String getModel(); | ||||
|  | ||||
| 	Integer getWidth(); | ||||
|  | ||||
| 	Integer getHeight(); | ||||
|  | ||||
| 	String getResponseFormat(); // openai - url or base64 : stability ai byte[] or base64 | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,119 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| public class ImageOptionsBuilder { | ||||
|  | ||||
| 	private class ImageModelOptionsImpl implements ImageOptions { | ||||
|  | ||||
| 		private Integer n; | ||||
|  | ||||
| 		private String model; | ||||
|  | ||||
| 		private Integer width; | ||||
|  | ||||
| 		private Integer height; | ||||
|  | ||||
| 		private String responseFormat; | ||||
|  | ||||
| 		@Override | ||||
| 		public Integer getN() { | ||||
| 			return n; | ||||
| 		} | ||||
|  | ||||
| 		public void setN(Integer n) { | ||||
| 			this.n = n; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public String getModel() { | ||||
| 			return model; | ||||
| 		} | ||||
|  | ||||
| 		public void setModel(String model) { | ||||
| 			this.model = model; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public String getResponseFormat() { | ||||
| 			return responseFormat; | ||||
| 		} | ||||
|  | ||||
| 		public void setResponseFormat(String responseFormat) { | ||||
| 			this.responseFormat = responseFormat; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public Integer getWidth() { | ||||
| 			return width; | ||||
| 		} | ||||
|  | ||||
| 		public void setWidth(Integer width) { | ||||
| 			this.width = width; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public Integer getHeight() { | ||||
| 			return height; | ||||
| 		} | ||||
|  | ||||
| 		public void setHeight(Integer height) { | ||||
| 			this.height = height; | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	private final ImageModelOptionsImpl options = new ImageModelOptionsImpl(); | ||||
|  | ||||
| 	private ImageOptionsBuilder() { | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	public static ImageOptionsBuilder builder() { | ||||
| 		return new ImageOptionsBuilder(); | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptionsBuilder withN(Integer n) { | ||||
| 		options.setN(n); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptionsBuilder withModel(String model) { | ||||
| 		options.setModel(model); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptionsBuilder withResponseFormat(String responseFormat) { | ||||
| 		options.setResponseFormat(responseFormat); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptionsBuilder withWidth(Integer width) { | ||||
| 		options.setWidth(width); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptionsBuilder withHeight(Integer height) { | ||||
| 		options.setHeight(height); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public ImageOptions build() { | ||||
| 		return options; | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,84 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ModelRequest; | ||||
|  | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
|  | ||||
| /** | ||||
|  * 图片内容 | ||||
|  */ | ||||
| public class ImagePrompt implements ModelRequest<List<ImageMessage>> { | ||||
|  | ||||
| 	private final List<ImageMessage> messages; | ||||
|  | ||||
| 	private ImageOptions imageModelOptions; | ||||
|  | ||||
| 	public ImagePrompt(List<ImageMessage> messages) { | ||||
| 		this.messages = messages; | ||||
| 	} | ||||
|  | ||||
| 	public ImagePrompt(List<ImageMessage> messages, ImageOptions imageModelOptions) { | ||||
| 		this.messages = messages; | ||||
| 		this.imageModelOptions = imageModelOptions; | ||||
| 	} | ||||
|  | ||||
| 	public ImagePrompt(ImageMessage imageMessage, ImageOptions imageOptions) { | ||||
| 		this(Collections.singletonList(imageMessage), imageOptions); | ||||
| 	} | ||||
|  | ||||
| 	public ImagePrompt(String instructions, ImageOptions imageOptions) { | ||||
| 		this(new ImageMessage(instructions), imageOptions); | ||||
| 	} | ||||
|  | ||||
| 	public ImagePrompt(String instructions) { | ||||
| 		this(new ImageMessage(instructions), ImageOptionsBuilder.builder().build()); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public List<ImageMessage> getInstructions() { | ||||
| 		return messages; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public ImageOptions getOptions() { | ||||
| 		return imageModelOptions; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "NewImagePrompt{" + "messages=" + messages + ", imageModelOptions=" + imageModelOptions + '}'; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object o) { | ||||
| 		if (this == o) | ||||
| 			return true; | ||||
| 		if (!(o instanceof ImagePrompt that)) | ||||
| 			return false; | ||||
| 		return Objects.equals(messages, that.messages) && Objects.equals(imageModelOptions, that.imageModelOptions); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return Objects.hash(messages, imageModelOptions); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,75 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ModelResponse; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
|  | ||||
| public class ImageResponse implements ModelResponse<ImageGeneration> { | ||||
|  | ||||
| 	private final ImageResponseMetadata imageResponseMetadata; | ||||
|  | ||||
| 	private final List<ImageGeneration> imageGenerations; | ||||
|  | ||||
| 	public ImageResponse(List<ImageGeneration> generations) { | ||||
| 		this(generations, ImageResponseMetadata.NULL); | ||||
| 	} | ||||
|  | ||||
| 	public ImageResponse(List<ImageGeneration> generations, ImageResponseMetadata imageResponseMetadata) { | ||||
| 		this.imageResponseMetadata = imageResponseMetadata; | ||||
| 		this.imageGenerations = List.copyOf(generations); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public ImageGeneration getResult() { | ||||
| 		return imageGenerations.get(0); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public List<ImageGeneration> getResults() { | ||||
| 		return imageGenerations; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public ImageResponseMetadata getMetadata() { | ||||
| 		return imageResponseMetadata; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "ImageResponse{" + "imageResponseMetadata=" + imageResponseMetadata + ", imageGenerations=" | ||||
| 				+ imageGenerations + '}'; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object o) { | ||||
| 		if (this == o) | ||||
| 			return true; | ||||
| 		if (!(o instanceof ImageResponse that)) | ||||
| 			return false; | ||||
| 		return Objects.equals(imageResponseMetadata, that.imageResponseMetadata) | ||||
| 				&& Objects.equals(imageGenerations, that.imageGenerations); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return Objects.hash(imageResponseMetadata, imageGenerations); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,31 @@ | ||||
| /* | ||||
|  * Copyright 2024-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.image; | ||||
|  | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.model.ResponseMetadata; | ||||
|  | ||||
| public interface ImageResponseMetadata extends ResponseMetadata { | ||||
|  | ||||
| 	ImageResponseMetadata NULL = new ImageResponseMetadata() { | ||||
| 	}; | ||||
|  | ||||
| 	default Long created() { | ||||
| 		return System.currentTimeMillis(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 cherishsince
					cherishsince