初始化
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user