初始化
This commit is contained in:
33
easy-agents-image/easy-agents-image-gitee/pom.xml
Normal file
33
easy-agents-image/easy-agents-image-gitee/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-gitee</name>
|
||||
<artifactId>easy-agents-image-gitee</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.gitee;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GiteeImageModel implements ImageModel {
|
||||
private GiteeImageModelConfig config;
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public GiteeImageModel(GiteeImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer " + config.getApiKey());
|
||||
|
||||
String payload = Maps.of("model", config.getModel())
|
||||
.set("prompt", request.getPrompt())
|
||||
.setIfNotNull("n", request.getN())
|
||||
.set("size", request.getSize())
|
||||
.set("response_format", "url")
|
||||
.toJSON();
|
||||
|
||||
String url = config.getEndpoint() + "/v1/images/generations";
|
||||
String responseJson = httpClient.post(url, headers, payload);
|
||||
|
||||
if (StringUtil.noText(responseJson)) {
|
||||
return ImageResponse.error("response is no text");
|
||||
}
|
||||
|
||||
JSONObject root = JSON.parseObject(responseJson);
|
||||
JSONArray images = root.getJSONArray("data");
|
||||
if (images == null || images.isEmpty()) {
|
||||
return ImageResponse.error("image data is empty: " + responseJson);
|
||||
}
|
||||
ImageResponse response = new ImageResponse();
|
||||
for (int i = 0; i < images.size(); i++) {
|
||||
JSONObject imageObj = images.getJSONObject(i);
|
||||
response.addImage(imageObj.getString("url"));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new UnsupportedOperationException("GiteeImageModel Can not support edit image.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new UnsupportedOperationException("GiteeImageModel Can not support vary image.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.gitee;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class GiteeImageModelConfig implements Serializable {
|
||||
private String endpoint = "https://ai.gitee.com";
|
||||
private String model = "flux-1-schnell";
|
||||
private String apiKey;
|
||||
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.Image;
|
||||
import com.easyagents.core.model.image.ImageModel;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.gitee.GiteeImageModel;
|
||||
import com.easyagents.image.gitee.GiteeImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class GiteeImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
GiteeImageModelConfig config = new GiteeImageModelConfig();
|
||||
config.setApiKey("****");
|
||||
|
||||
ImageModel imageModel = new GiteeImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("A cute little tiger standing in the high-speed train");
|
||||
request.setSize(1024,1024);
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
if (generate != null && generate.getImages() != null){
|
||||
int index = 0;
|
||||
for (Image image : generate.getImages()) {
|
||||
image.writeToFile(new File("/Users/michael/Desktop/test/image"+(index++)+".jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
}
|
||||
33
easy-agents-image/easy-agents-image-openai/pom.xml
Normal file
33
easy-agents-image/easy-agents-image-openai/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-openai</name>
|
||||
<artifactId>easy-agents-image-openai</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.openai;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class OpenAIImageModel extends BaseImageModel<OpenAIImageModelConfig> {
|
||||
|
||||
private OpenAIImageModelConfig config;
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public OpenAIImageModel(OpenAIImageModelConfig config) {
|
||||
super(config);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer " + config.getApiKey());
|
||||
|
||||
String payload = Maps.of("model", config.getModel())
|
||||
.set("prompt", request.getPrompt())
|
||||
.setIfNotNull("n", request.getN())
|
||||
.set("size", request.getSize())
|
||||
.toJSON();
|
||||
|
||||
|
||||
String url = config.getEndpoint() + "/v1/images/generations";
|
||||
String responseJson = httpClient.post(url, headers, payload);
|
||||
|
||||
if (StringUtil.noText(responseJson)) {
|
||||
return ImageResponse.error("response is no text");
|
||||
}
|
||||
|
||||
JSONObject root = JSON.parseObject(responseJson);
|
||||
JSONArray images = root.getJSONArray("data");
|
||||
if (images == null || images.isEmpty()) {
|
||||
return ImageResponse.error("image data is empty: " + responseJson);
|
||||
}
|
||||
ImageResponse response = new ImageResponse();
|
||||
for (int i = 0; i < images.size(); i++) {
|
||||
JSONObject imageObj = images.getJSONObject(i);
|
||||
response.addImage(imageObj.getString("url"));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support edit image");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support vary image");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.openai;
|
||||
|
||||
import com.easyagents.core.model.config.BaseModelConfig;
|
||||
|
||||
public class OpenAIImageModelConfig extends BaseModelConfig {
|
||||
private static final String endpoint = "https://api.openai.com";
|
||||
private static final String model = "dall-e-3";
|
||||
|
||||
public OpenAIImageModelConfig() {
|
||||
setEndpoint(endpoint);
|
||||
setModel(model);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.openai.OpenAIImageModel;
|
||||
import com.easyagents.image.openai.OpenAIImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OpenAIImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
OpenAIImageModelConfig config = new OpenAIImageModelConfig();
|
||||
config.setApiKey("sk-5gqOclb****");
|
||||
|
||||
OpenAIImageModel imageModel = new OpenAIImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("A cute little tiger standing in the high-speed train");
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
}
|
||||
}
|
||||
45
easy-agents-image/easy-agents-image-qianfan/pom.xml
Normal file
45
easy-agents-image/easy-agents-image-qianfan/pom.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>easy-agents-image-qianfan</artifactId>
|
||||
<name>easy-agents-image-qianfan</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>33.5.0-jre</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.easyagents.image.qianfan;
|
||||
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
public class QianfanImageModel implements ImageModel {
|
||||
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
private QianfanImageModelConfig config;
|
||||
|
||||
public QianfanImageModel(QianfanImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
ImageResponse responseImage = new ImageResponse();
|
||||
try {
|
||||
request.setModel(config.getModels());
|
||||
String payload = promptToPayload(request);
|
||||
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), payload);
|
||||
Request requestQianfan = new Request.Builder()
|
||||
.url(config.getEndpoint() + config.getEndpointGenerations())
|
||||
.post(body)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("Authorization", "Bearer " + config.getApiKey())
|
||||
.build();
|
||||
|
||||
Response response = HTTP_CLIENT.newCall(requestQianfan).execute();
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Unexpected code " + response);
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
JSONArray dataArray = jsonObject.getJSONArray("data");
|
||||
for (int i = 0; i < dataArray.size(); i++) {
|
||||
responseImage.addImage(dataArray.getJSONObject(i).getString("url"));
|
||||
}
|
||||
|
||||
return responseImage;
|
||||
} catch (IOException e) {
|
||||
ImageResponse.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
return responseImage;
|
||||
} catch (Exception e) {
|
||||
ImageResponse.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
return responseImage;
|
||||
}
|
||||
}
|
||||
|
||||
public static String promptToPayload(GenerateImageRequest request) {
|
||||
return Maps.of("Prompt", request.getPrompt())
|
||||
.setIfNotEmpty("model", request.getModel())
|
||||
.toJSON();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.easyagents.image.qianfan;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class QianfanImageModelConfig {
|
||||
|
||||
private String endpoint = "https://qianfan.baidubce.com/v2";
|
||||
private String endpointGenerations = "/images/generations";
|
||||
private String models="irag-1.0";
|
||||
private String apiKey;
|
||||
private Consumer<Map<String, String>> headersConfig;
|
||||
|
||||
public Consumer<Map<String, String>> getHeadersConfig() {
|
||||
return headersConfig;
|
||||
}
|
||||
|
||||
public void setHeadersConfig(Consumer<Map<String, String>> headersConfig) {
|
||||
this.headersConfig = headersConfig;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public String getEndpointGenerations() {
|
||||
return endpointGenerations;
|
||||
}
|
||||
|
||||
public void setEndpointGenerations(String endpointGenerations) {
|
||||
this.endpointGenerations = endpointGenerations;
|
||||
}
|
||||
|
||||
public String getModels() {
|
||||
return models;
|
||||
}
|
||||
|
||||
public void setModels(String models) {
|
||||
this.models = models;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "QianfanImageModelConfig{" +
|
||||
"endpoint='" + endpoint + '\'' +
|
||||
", endpointGenerations='" + endpointGenerations + '\'' +
|
||||
", models='" + models + '\'' +
|
||||
", apiKey='" + apiKey + '\'' +
|
||||
", headersConfig=" + headersConfig +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.qianfan.QianfanImageModel;
|
||||
import com.easyagents.image.qianfan.QianfanImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QianfanImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenerate() throws InterruptedException {
|
||||
QianfanImageModelConfig config = new QianfanImageModelConfig();
|
||||
config.setApiKey("*************");
|
||||
QianfanImageModel imageModel = new QianfanImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("画一个职场性感女生图片");
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
}
|
||||
}
|
||||
38
easy-agents-image/easy-agents-image-qwen/pom.xml
Normal file
38
easy-agents-image/easy-agents-image-qwen/pom.xml
Normal file
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-qwen</name>
|
||||
<artifactId>easy-agents-image-qwen</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dashscope-sdk-java</artifactId>
|
||||
<!-- 请将 'the-latest-version' 替换为最新版本号:https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
|
||||
<version>2.18.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.qwen;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class QwenImageModel implements ImageModel {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(QwenImageModel.class);
|
||||
private final QwenImageModelConfig config;
|
||||
private final HttpClient httpClient = new HttpClient();
|
||||
|
||||
public QwenImageModel(QwenImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
try {
|
||||
ImageSynthesis is = new ImageSynthesis();
|
||||
ImageSynthesisParam param =
|
||||
ImageSynthesisParam.builder()
|
||||
.apiKey(config.getApiKey())
|
||||
.model(null != request.getModel() ? request.getModel() : config.getModel())
|
||||
.size(request.getSize())
|
||||
.prompt(request.getPrompt())
|
||||
.seed(Integer.valueOf(String.valueOf(request.getOptionOrDefault("seed",1))))
|
||||
.build();
|
||||
ImageSynthesisResult result = is.call(param);
|
||||
if (Objects.isNull(result.getOutput().getResults())){
|
||||
return ImageResponse.error(result.getOutput().getMessage());
|
||||
}
|
||||
ImageResponse imageResponse = new ImageResponse();
|
||||
for(Map<String, String> item :result.getOutput().getResults()) {
|
||||
imageResponse.addImage(item.get("url"));
|
||||
}
|
||||
return imageResponse;
|
||||
} catch (Exception e) {
|
||||
return ImageResponse.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
throw new IllegalStateException("QwenImageModel Can not support img2imggenerate.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new IllegalStateException("QwenImageModel Can not support edit image.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new IllegalStateException("QwenImageModel Can not support vary image.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.qwen;
|
||||
|
||||
public class QwenImageModelConfig {
|
||||
private String endpoint = "https://dashscope.aliyuncs.com";
|
||||
|
||||
private String model = "flux-schnell";
|
||||
|
||||
private String apiKey;
|
||||
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageModel;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.qwen.QwenImageModel;
|
||||
import com.easyagents.image.qwen.QwenImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QwenImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage() throws InterruptedException {
|
||||
Thread thread = new Thread(() -> {
|
||||
QwenImageModelConfig config = new QwenImageModelConfig();
|
||||
config.setApiKey("******************");
|
||||
ImageModel imageModel = new QwenImageModel(config);
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("雨中, 竹林, 小路");
|
||||
request.setModel("flux-schnell");
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
});
|
||||
|
||||
// Thread thread2 = new Thread(() -> {
|
||||
// QwenImageModelConfig config = new QwenImageModelConfig();
|
||||
// config.setApiKey("******************");
|
||||
// ImageModel imageModel = new QwenImageModel(config);
|
||||
// GenerateImageRequest request = new GenerateImageRequest();
|
||||
// request.setPrompt("雨中, 竹林, 小路");
|
||||
// request.setModel("flux-schnell");
|
||||
// ImageResponse generate = imageModel.generate(request);
|
||||
// });
|
||||
thread.start();
|
||||
// thread2.start();
|
||||
thread.join();
|
||||
// thread2.join();
|
||||
}
|
||||
}
|
||||
35
easy-agents-image/easy-agents-image-siliconflow/pom.xml
Normal file
35
easy-agents-image/easy-agents-image-siliconflow/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-siliconflow</name>
|
||||
<artifactId>easy-agents-image-siliconflow</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.siliconflow;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SiliconImageModel implements ImageModel {
|
||||
private SiliconflowImageModelConfig config;
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public SiliconImageModel(SiliconflowImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer " + config.getApiKey());
|
||||
|
||||
String payload = Maps.of("prompt", request.getPrompt())
|
||||
.setIfNotEmpty("negative_prompt", request.getNegativePrompt())
|
||||
.setOrDefault("image_size", request.getSize(), config.getImageSize())
|
||||
.setOrDefault("batch_size", request.getN(), 1)
|
||||
.setOrDefault("num_inference_steps", request.getOption("num_inference_steps"), config.getNumInferenceSteps())
|
||||
.setOrDefault("guidance_scale", request.getOption("guidance_scale"), config.getGuidanceScale())
|
||||
.toJSON();
|
||||
|
||||
String url = config.getEndpoint() + SiliconflowImageModels.getPath(config.getModel());
|
||||
String response = httpClient.post(url, headers, payload);
|
||||
if (StringUtil.noText(response)) {
|
||||
return ImageResponse.error("response is no text");
|
||||
}
|
||||
|
||||
if (StringUtil.notJsonObject(response)) {
|
||||
return ImageResponse.error(response);
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(response);
|
||||
JSONArray imagesArray = jsonObject.getJSONArray("images");
|
||||
if (imagesArray == null || imagesArray.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ImageResponse imageResponse = new ImageResponse();
|
||||
for (int i = 0; i < imagesArray.size(); i++) {
|
||||
JSONObject imageObject = imagesArray.getJSONObject(i);
|
||||
imageResponse.addImage(imageObject.getString("url"));
|
||||
}
|
||||
|
||||
return imageResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new IllegalStateException("SiliconImageModel Can not support edit image.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new IllegalStateException("SiliconImageModel Can not support vary image.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.siliconflow;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SiliconflowImageModelConfig implements Serializable {
|
||||
private String endpoint = "https://api.siliconflow.cn";
|
||||
private String model = SiliconflowImageModels.flux_1_schnell;
|
||||
private String apiKey;
|
||||
private Integer numInferenceSteps = 20;
|
||||
private Integer guidanceScale = 7;
|
||||
private String imageSize = "1024x1024";
|
||||
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public Integer getNumInferenceSteps() {
|
||||
return numInferenceSteps;
|
||||
}
|
||||
|
||||
public void setNumInferenceSteps(Integer numInferenceSteps) {
|
||||
this.numInferenceSteps = numInferenceSteps;
|
||||
}
|
||||
|
||||
public Integer getGuidanceScale() {
|
||||
return guidanceScale;
|
||||
}
|
||||
|
||||
public void setGuidanceScale(Integer guidanceScale) {
|
||||
this.guidanceScale = guidanceScale;
|
||||
}
|
||||
|
||||
public String getImageSize() {
|
||||
return imageSize;
|
||||
}
|
||||
|
||||
public void setImageSize(String imageSize) {
|
||||
this.imageSize = imageSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.easyagents.image.siliconflow;
|
||||
|
||||
import com.easyagents.core.util.Maps;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SiliconflowImageModels {
|
||||
|
||||
|
||||
/**
|
||||
* 由 Black Forest Labs 开发的 120 亿参数文生图模型,采用潜在对抗扩散蒸馏技术,能够在 1 到 4 步内生成高质量图像。该模型性能媲美闭源替代品,并在 Apache-2.0 许可证下发布,适用于个人、科研和商业用途。
|
||||
*/
|
||||
public static final String flux_1_schnell = "FLUX.1-schnell";
|
||||
|
||||
/**
|
||||
* 由 Stability AI 开发并开源的文生图大模型,其创意图像生成能力位居行业前列。具备出色的指令理解能力,能够支持反向 Prompt 定义来精确生成内容。
|
||||
*/
|
||||
public static final String Stable_Diffusion_3 = "Stable Diffusion 3";
|
||||
public static final String Stable_Diffusion_XL = "Stable Diffusion XL";
|
||||
public static final String Stable_Diffusion_2_1 = "Stable Diffusion 2.1";
|
||||
public static final String Stable_Diffusion_Turbo = "Stable Diffusion Turbo";
|
||||
public static final String Stable_Diffusion_XL_Turbo = "Stable Diffusion XL Turbo";
|
||||
public static final String Stable_Diffusion_XL_Lighting = "Stable Diffusion XL Lighting";
|
||||
|
||||
|
||||
private static Map<String, Object> modelsPathMapping = Maps
|
||||
.of(flux_1_schnell, "/v1/black-forest-labs/FLUX.1-schnell/text-to-image")
|
||||
.set(Stable_Diffusion_3, "/v1/stabilityai/stable-diffusion-3-medium/text-to-image")
|
||||
.set(Stable_Diffusion_XL, "/v1/stabilityai/stable-diffusion-xl-base-1.0/text-to-image")
|
||||
.set(Stable_Diffusion_2_1, "/v1/stabilityai/stable-diffusion-2-1/text-to-image")
|
||||
.set(Stable_Diffusion_Turbo, "/v1/stabilityai/sd-turbo/text-to-image")
|
||||
.set(Stable_Diffusion_XL_Turbo, "/v1/stabilityai/sdxl-turbo/text-to-image")
|
||||
.set(Stable_Diffusion_XL_Lighting, "/v1/ByteDance/SDXL-Lightning/text-to-image")
|
||||
;
|
||||
|
||||
public static String getPath(String model) {
|
||||
return (String) modelsPathMapping.get(model);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.siliconflow.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.Image;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.siliconflow.SiliconImageModel;
|
||||
import com.easyagents.image.siliconflow.SiliconflowImageModelConfig;
|
||||
import com.easyagents.image.siliconflow.SiliconflowImageModels;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class SiliconflowImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
SiliconflowImageModelConfig config = new SiliconflowImageModelConfig();
|
||||
config.setModel(SiliconflowImageModels.Stable_Diffusion_XL);
|
||||
config.setApiKey("sk-****");
|
||||
|
||||
SiliconImageModel imageModel = new SiliconImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("A cute little tiger standing in the high-speed train");
|
||||
request.setSize(1024,1024);
|
||||
request.setN(4);
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
if (generate != null && generate.getImages() != null){
|
||||
int index = 0;
|
||||
for (Image image : generate.getImages()) {
|
||||
image.writeToFile(new File("/Users/michael/Desktop/test/image"+(index++)+".jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
}
|
||||
32
easy-agents-image/easy-agents-image-stability/pom.xml
Normal file
32
easy-agents-image/easy-agents-image-stability/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-stability</name>
|
||||
<artifactId>easy-agents-image-stability</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.stability;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class StabilityImageModel implements ImageModel {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(StabilityImageModel.class);
|
||||
private StabilityImageModelConfig config;
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public StabilityImageModel(StabilityImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("accept", "image/*");
|
||||
headers.put("Authorization", "Bearer " + config.getApiKey());
|
||||
|
||||
Map<String, Object> payload = Maps.of("prompt", request.getPrompt())
|
||||
.setIfNotNull("output_format", "jpeg");
|
||||
|
||||
String url = config.getEndpoint() + "/v2beta/stable-image/generate/sd3";
|
||||
|
||||
try (Response response = httpClient.multipart(url, headers, payload);
|
||||
ResponseBody body = response.body()) {
|
||||
if (response.isSuccessful() && body != null) {
|
||||
ImageResponse imageResponse = new ImageResponse();
|
||||
imageResponse.addImage(body.bytes());
|
||||
return imageResponse;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.toString(), e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.stability;
|
||||
|
||||
public class StabilityImageModelConfig {
|
||||
private String endpoint = "https://api.stability.ai/";
|
||||
private String apiKey;
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.stability.StabilityImageModel;
|
||||
import com.easyagents.image.stability.StabilityImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StabilityImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
StabilityImageModelConfig config = new StabilityImageModelConfig();
|
||||
config.setApiKey("sk-5gqOcl*****");
|
||||
|
||||
StabilityImageModel imageModel = new StabilityImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("A cute little tiger standing in the high-speed train");
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
}
|
||||
}
|
||||
37
easy-agents-image/easy-agents-image-tencent/pom.xml
Normal file
37
easy-agents-image/easy-agents-image-tencent/pom.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-tencent</name>
|
||||
<artifactId>easy-agents-image-tencent</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-common</artifactId>
|
||||
<version>3.1.1261</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.tencent;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.tencentcloudapi.common.DatatypeConverter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class TencentImageModel implements ImageModel {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TencentImageModel.class);
|
||||
private final TencentImageModelConfig config;
|
||||
private final HttpClient httpClient = new HttpClient();
|
||||
|
||||
public TencentImageModel(TencentImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
try {
|
||||
String payload = promptToPayload(request);
|
||||
Map<String, String> headers = createAuthorizationToken("SubmitHunyuanImageJob", payload);
|
||||
String response = httpClient.post(config.getEndpoint(), headers, payload);
|
||||
JSONObject jsonObject = JSON.parseObject(response);
|
||||
JSONObject error = jsonObject.getJSONObject("Response").getJSONObject("Error");
|
||||
if (error != null && !error.isEmpty()) {
|
||||
return ImageResponse.error(error.getString("Message"));
|
||||
}
|
||||
Object jobId = jsonObject.getJSONObject("Response").get("JobId");
|
||||
if (Objects.isNull(jobId)) {
|
||||
return ImageResponse.error("response is no jobId");
|
||||
}
|
||||
String id = (String) jobId;
|
||||
return getImage(id);
|
||||
} catch (Exception e) {
|
||||
return ImageResponse.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new IllegalStateException("TencentImageModel Can not support edit image.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new IllegalStateException("TencentImageModel Can not support vary image.");
|
||||
}
|
||||
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
private ImageResponse getImage(String jobId) {
|
||||
ImageResponse imageResponse = null;
|
||||
while (true) {
|
||||
synchronized (LOCK) {
|
||||
imageResponse = callService(jobId);
|
||||
if (!Objects.isNull(imageResponse)) {
|
||||
break;
|
||||
}
|
||||
// 等待一段时间再重试
|
||||
try {
|
||||
LOCK.wait(1000);
|
||||
} catch (InterruptedException e) {
|
||||
// 线程在等待时被中断
|
||||
Thread.currentThread().interrupt();
|
||||
imageResponse = ImageResponse.error(e.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return imageResponse;
|
||||
}
|
||||
|
||||
|
||||
public ImageResponse callService(String jobId) {
|
||||
try {
|
||||
String payload = Maps.of("JobId", jobId).toJSON();
|
||||
Map<String, String> headers = createAuthorizationToken("QueryHunyuanImageJob", payload);
|
||||
String resp = httpClient.post(config.getEndpoint(), headers, payload);
|
||||
JSONObject resultJson = JSONObject.parseObject(resp).getJSONObject("Response");
|
||||
JSONObject error = resultJson.getJSONObject("Error");
|
||||
if (error != null && !error.isEmpty()) {
|
||||
return ImageResponse.error(error.getString("Message"));
|
||||
}
|
||||
if (Objects.isNull(resultJson.get("JobStatusCode"))) {
|
||||
return ImageResponse.error("response is no JobStatusCode");
|
||||
}
|
||||
Integer jobStatusCode = resultJson.getInteger("JobStatusCode");
|
||||
if (Objects.equals(5, jobStatusCode)) {
|
||||
//处理完成
|
||||
if (Objects.isNull(resultJson.get("ResultImage"))) {
|
||||
return ImageResponse.error("response is no ResultImage");
|
||||
}
|
||||
JSONArray imagesArray = resultJson.getJSONArray("ResultImage");
|
||||
ImageResponse response = new ImageResponse();
|
||||
for (int i = 0; i < imagesArray.size(); i++) {
|
||||
String imageObj = imagesArray.getString(i);
|
||||
response.addImage(imageObj);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
if (Objects.equals(4, jobStatusCode)) {
|
||||
//处理错误
|
||||
return ImageResponse.error(resultJson.getString("JobErrorMsg"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ImageResponse.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static String promptToPayload(GenerateImageRequest request) {
|
||||
return Maps.of("Prompt", request.getPrompt())
|
||||
.setIfNotEmpty("NegativePrompt", request.getNegativePrompt())
|
||||
.setIfNotEmpty("Style", request.getSize())
|
||||
.setIfNotEmpty("Resolution", request.getQuality())
|
||||
.setIfNotEmpty("Num", request.getN())
|
||||
.setIfNotEmpty(request.getOptions())
|
||||
.toJSON();
|
||||
}
|
||||
|
||||
|
||||
private final static Charset UTF8 = StandardCharsets.UTF_8;
|
||||
private final static String CT_JSON = "application/json; charset=utf-8";
|
||||
|
||||
public static byte[] hmac256(byte[] key, String msg) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm());
|
||||
mac.init(secretKeySpec);
|
||||
return mac.doFinal(msg.getBytes(UTF8));
|
||||
}
|
||||
|
||||
public static String sha256Hex(String s) throws Exception {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] d = md.digest(s.getBytes(UTF8));
|
||||
return DatatypeConverter.printHexBinary(d).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return java.util.Map<java.lang.String, java.lang.String>
|
||||
* @Author sunch
|
||||
* @Description 封装参数
|
||||
* @Date 17:34 2025/3/5
|
||||
* @Param [action, payload]
|
||||
*/
|
||||
public Map<String, String> createAuthorizationToken(String action, String payload) {
|
||||
try {
|
||||
String service = config.getService();
|
||||
String host = config.getHost();
|
||||
String version = "2023-09-01";
|
||||
String algorithm = "TC3-HMAC-SHA256";
|
||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
// 注意时区,否则容易出错
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
String date = sdf.format(new Date(Long.parseLong(timestamp + "000")));
|
||||
|
||||
// ************* 步骤 1:拼接规范请求串 *************
|
||||
String httpRequestMethod = "POST";
|
||||
String canonicalUri = "/";
|
||||
String canonicalQueryString = "";
|
||||
String canonicalHeaders = "content-type:application/json; charset=utf-8\n"
|
||||
+ "host:" + host + "\n" + "x-tc-action:" + action.toLowerCase() + "\n";
|
||||
String signedHeaders = "content-type;host;x-tc-action";
|
||||
|
||||
String hashedRequestPayload = sha256Hex(payload);
|
||||
String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
|
||||
+ canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestPayload;
|
||||
// System.out.println(canonicalRequest);
|
||||
|
||||
// ************* 步骤 2:拼接待签名字符串 *************
|
||||
String credentialScope = date + "/" + service + "/" + "tc3_request";
|
||||
String hashedCanonicalRequest = sha256Hex(canonicalRequest);
|
||||
String stringToSign = algorithm + "\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
|
||||
// System.out.println(stringToSign);
|
||||
|
||||
// ************* 步骤 3:计算签名 *************
|
||||
byte[] secretDate = hmac256(("TC3" + config.getApiKey()).getBytes(UTF8), date);
|
||||
byte[] secretService = hmac256(secretDate, service);
|
||||
byte[] secretSigning = hmac256(secretService, "tc3_request");
|
||||
String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase();
|
||||
// System.out.println(signature);
|
||||
|
||||
// ************* 步骤 4:拼接 Authorization *************
|
||||
String authorization = algorithm + " " + "Credential=" + config.getApiSecret() + "/" + credentialScope + ", "
|
||||
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
|
||||
// System.out.println(authorization);
|
||||
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Authorization", authorization);
|
||||
headers.put("Content-Type", CT_JSON);
|
||||
headers.put("Host", host);
|
||||
headers.put("X-TC-Action", action);
|
||||
headers.put("X-TC-Timestamp", timestamp);
|
||||
headers.put("X-TC-Version", version);
|
||||
headers.put("X-TC-Region", config.getRegion());
|
||||
return headers;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.tencent;
|
||||
|
||||
public class TencentImageModelConfig {
|
||||
private String endpoint = "https://hunyuan.tencentcloudapi.com";
|
||||
private String apiKey;
|
||||
|
||||
private String apiSecret;
|
||||
|
||||
private String service = "hunyuan";
|
||||
|
||||
private String region = "ap-guangzhou";
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public String getApiSecret() {
|
||||
return apiSecret;
|
||||
}
|
||||
|
||||
public void setApiSecret(String apiSecret) {
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
String endpoint = getEndpoint();
|
||||
if (endpoint.toLowerCase().startsWith("https://")) {
|
||||
endpoint = endpoint.substring(8);
|
||||
} else if (endpoint.toLowerCase().startsWith("http://")) {
|
||||
endpoint = endpoint.substring(7);
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageModel;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.tencent.TencentImageModel;
|
||||
import com.easyagents.image.tencent.TencentImageModelConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TencentImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage() throws InterruptedException {
|
||||
Thread thread = new Thread(() -> {
|
||||
TencentImageModelConfig config = new TencentImageModelConfig();
|
||||
config.setApiSecret("*****************");
|
||||
config.setApiKey("*****************");
|
||||
ImageModel imageModel = new TencentImageModel(config);
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("雨中, 竹林, 小路");
|
||||
request.setN(1);
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
System.out.flush();
|
||||
});
|
||||
|
||||
Thread thread2 = new Thread(() -> {
|
||||
TencentImageModelConfig config = new TencentImageModelConfig();
|
||||
config.setApiSecret("*****************");
|
||||
config.setApiKey("*****************");
|
||||
ImageModel imageModel = new TencentImageModel(config);
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("雨中, 竹林, 小路, 人生");
|
||||
request.setN(1);
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
System.out.flush();
|
||||
});
|
||||
thread.start();
|
||||
thread2.start();
|
||||
thread.join();
|
||||
thread2.join();
|
||||
}
|
||||
}
|
||||
54
easy-agents-image/easy-agents-image-volcengine/pom.xml
Normal file
54
easy-agents-image/easy-agents-image-volcengine/pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>easy-agents-image-volcengine</artifactId>
|
||||
<name>easy-agents-image-volcengine</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--火山方舟 Java SDK-->
|
||||
<dependency>
|
||||
<groupId>com.volcengine</groupId>
|
||||
<artifactId>volcengine-java-sdk-ark-runtime</artifactId>
|
||||
<version>0.2.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.volcengine</groupId>
|
||||
<artifactId>volc-sdk-java</artifactId>
|
||||
<version>1.0.221</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>33.5.0-jre</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.volcengine;
|
||||
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.volcengine.service.visual.IVisualService;
|
||||
import com.volcengine.service.visual.impl.VisualServiceImpl;
|
||||
|
||||
public class VolcengineImageModel implements ImageModel {
|
||||
|
||||
private VolcengineImageModelConfig config;
|
||||
private IVisualService visualService = VisualServiceImpl.getInstance();
|
||||
|
||||
public VolcengineImageModel(VolcengineImageModelConfig config) {
|
||||
this.config = config;
|
||||
visualService.setAccessKey(config.getAccessKey());
|
||||
visualService.setSecretKey(config.getSecretKey());
|
||||
}
|
||||
|
||||
private ImageResponse processImageRequest(GenerateImageRequest request) {
|
||||
JSONObject req = new JSONObject(request.getOptions());
|
||||
ImageResponse responseimage = new ImageResponse();
|
||||
try {
|
||||
Object response = visualService.cvProcess(req);
|
||||
if (response instanceof JSONObject) {
|
||||
JSONObject jsonResponse = (JSONObject) response;
|
||||
JSONObject dataObject = jsonResponse.getJSONObject("data");
|
||||
JSONArray imageUrlsArray = dataObject.getJSONArray("image_urls");
|
||||
for (int i = 0; i < imageUrlsArray.size(); i++) {
|
||||
responseimage.addImage(imageUrlsArray.getString(i));
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Unexpected response type: " + response.getClass().getName());
|
||||
}
|
||||
return responseimage;
|
||||
} catch (Exception e) {
|
||||
ImageResponse.error(e.getMessage());
|
||||
e.printStackTrace(); // 记录堆栈跟踪方便调试
|
||||
return responseimage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
return processImageRequest(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return processImageRequest(request);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support edit image");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support vary image");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.image.volcengine;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class VolcengineImageModelConfig implements Serializable {
|
||||
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.volcengine.VolcengineImageModel;
|
||||
import com.easyagents.image.volcengine.VolcengineImageModelConfig;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class VolcengineImageTest {
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
VolcengineImageModelConfig config = new VolcengineImageModelConfig();
|
||||
config.setAccessKey("*********************");
|
||||
config.setSecretKey("*********************");
|
||||
|
||||
VolcengineImageModel imageModel = new VolcengineImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
//请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)参考通用2.1-文生图
|
||||
req.put("req_key","high_aes_general_v21_L");
|
||||
req.put("prompt","千军万马");
|
||||
req.put("model_version","general_v2.1_L");
|
||||
req.put("req_schedule_conf","general_v20_9B_pe");
|
||||
req.put("llm_seed",-1);
|
||||
req.put("seed",-1);
|
||||
req.put("scale",3.5);
|
||||
req.put("ddim_steps",25);
|
||||
req.put("width",512);
|
||||
req.put("height",512);
|
||||
req.put("use_pre_llm",true);
|
||||
req.put("use_sr",true);
|
||||
req.put("sr_seed",-1);
|
||||
req.put("sr_strength",0.4);
|
||||
req.put("sr_scale",3.5);
|
||||
req.put("sr_steps",20);
|
||||
req.put("is_only_sr",false);
|
||||
req.put("return_url",true);
|
||||
// 创建子级JSONObject
|
||||
JSONObject subData = new JSONObject();
|
||||
subData.put("add_logo", true);
|
||||
subData.put("position", 2);
|
||||
subData.put("language", 0);
|
||||
subData.put("opacity", 0.3);
|
||||
subData.put("logo_text_content", "wangyangyang");
|
||||
req.put("logo_info",subData);
|
||||
|
||||
request.setOptions(req);
|
||||
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test( )
|
||||
public void testImg2ImgXLSft() throws IOException {
|
||||
VolcengineImageModelConfig config = new VolcengineImageModelConfig();
|
||||
config.setAccessKey("*********************");
|
||||
config.setSecretKey("*********************");
|
||||
|
||||
VolcengineImageModel imageModel = new VolcengineImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
req.put("req_key","i2i_xl_sft");
|
||||
List<String> images=new ArrayList<>();
|
||||
|
||||
File file = new File(System.getProperty("user.dir"), "../../testresource/ark_demo_img_1.png");
|
||||
// 将图片读取为字节数组
|
||||
byte[] imageBytes = Files.readAllBytes(Paths.get(file.toURI()));
|
||||
|
||||
// 将字节数组编码为Base64
|
||||
String base64String = Base64.getEncoder().encodeToString(imageBytes);
|
||||
|
||||
images.add(base64String);
|
||||
// images.add("https://ark-project.tos-cn-beijing.volces.com/doc_image/ark_demo_img_1.png");
|
||||
req.put("binary_data_base64",images);
|
||||
req.put("prompt","根据图片内容生成风格、服装及发型一样的亚洲美女图片");
|
||||
req.put("return_url",true);
|
||||
request.setOptions(req);
|
||||
|
||||
ImageResponse generate = imageModel.img2imggenerate(request);
|
||||
System.out.println(generate);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.volcengine.service.visual.IVisualService;
|
||||
import com.volcengine.service.visual.impl.VisualServiceImpl;
|
||||
|
||||
public class VolcengineTest {
|
||||
//以下是同步调用直接返回结果的方法,按照实际需求可调用sdk同步及异步相关方法;
|
||||
public static void main(String[] args) {
|
||||
IVisualService visualService = VisualServiceImpl.getInstance();
|
||||
// call below method if you dont set ak and sk in ~/.vcloud/config
|
||||
visualService.setAccessKey("AKLTNmU0M2RkNWZkMmZmNDQwYWI2NTZiMjA1ODYxY2M3MjE");
|
||||
visualService.setSecretKey("TkdObFpXSTFZMlJtWldReE5EVTRPRGt4TkRsaE1EVTRaalpsTnpnMllURQ==");
|
||||
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
//请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)
|
||||
req.put("req_key","high_aes_general_v21_L");
|
||||
req.put("prompt","千军万马");
|
||||
req.put("model_version","general_v2.1_L");
|
||||
req.put("req_schedule_conf","general_v20_9B_pe");
|
||||
req.put("llm_seed",-1);
|
||||
req.put("seed",-1);
|
||||
req.put("scale",3.5);
|
||||
req.put("ddim_steps",25);
|
||||
req.put("width",512);
|
||||
req.put("height",512);
|
||||
req.put("use_pre_llm",true);
|
||||
req.put("use_sr",true);
|
||||
req.put("sr_seed",-1);
|
||||
req.put("sr_strength",0.4);
|
||||
req.put("sr_scale",3.5);
|
||||
req.put("sr_steps",20);
|
||||
req.put("is_only_sr",false);
|
||||
req.put("return_url",true);
|
||||
//创建子级JSONObject
|
||||
JSONObject subData = new JSONObject();
|
||||
subData.put("add_logo", true);
|
||||
subData.put("position", 2);
|
||||
subData.put("language", 0);
|
||||
subData.put("opacity", 0.3);
|
||||
subData.put("logo_text_content", "wangyangyang");
|
||||
req.put("logo_info",subData);
|
||||
|
||||
|
||||
try {
|
||||
Object response = visualService.cvProcess(req);
|
||||
System.out.println(JSON.toJSONString(response));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
easy-agents-image/pom.xml
Normal file
32
easy-agents-image/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-parent</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image</name>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>easy-agents-image-openai</module>
|
||||
<module>easy-agents-image-stability</module>
|
||||
<module>easy-agents-image-gitee</module>
|
||||
<module>easy-agents-image-siliconflow</module>
|
||||
<module>easy-agents-image-tencent</module>
|
||||
<module>easy-agents-image-volcengine</module>
|
||||
<module>easy-agents-image-qwen</module>
|
||||
<module>easy-agents-image-qianfan</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user