From e6b36b3c575feba4ce80ac8fa80f989482509637 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Thu, 15 May 2025 16:25:19 +0800 Subject: [PATCH] 'commit' --- .../Util/Liblib/LiblibTextToImage.java | 65 +------- .../aiSupport/Util/Liblib/QueryModel.java | 140 ++++++++++++++++++ 2 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/QueryModel.java diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/LiblibTextToImage.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/LiblibTextToImage.java index 1c2c453c..842c622e 100644 --- a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/LiblibTextToImage.java +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/LiblibTextToImage.java @@ -297,70 +297,7 @@ public class LiblibTextToImage { return responseJson.getJSONObject("data"); } - - /** - * 查询生图状态和结果 - * @param generateUuid 生成任务UUID - * @return 任务状态和结果信息 - * @throws IOException 异常信息 - */ - public static JSONObject queryGenerateStatus(String generateUuid) throws IOException { - // 创建OkHttpClient - OkHttpClient client = createHttpClient(); - - // 构建请求体 - JSONObject requestBody = new JSONObject(); - requestBody.put("generateUuid", generateUuid); - - // 获取API路径 - String uri = "/api/generate/webui/status"; - - // 生成签名信息 - SignUtil.SignatureInfo signInfo = SignUtil.makeSign(uri, secretKey); - - // 构建带签名的URL - HttpUrl.Builder urlBuilder = HttpUrl.parse(QUERY_STATUS_URL).newBuilder() - .addQueryParameter("AccessKey", accessKey) - .addQueryParameter("Signature", signInfo.getSignature()) - .addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp())) - .addQueryParameter("SignatureNonce", signInfo.getSignatureNonce()); - - // 创建请求 - MediaType mediaType = MediaType.parse("application/json"); - RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString()); - Request request = new Request.Builder() - .url(urlBuilder.build()) - .method("POST", body) - .addHeader("Content-Type", "application/json") - .build(); - - // 执行请求 - log.info("查询生图状态: {}", requestBody.toJSONString()); - Response response = client.newCall(request).execute(); - - // 处理响应 - if (!response.isSuccessful()) { - String errorMsg = "查询生图状态失败,状态码: " + response.code(); - log.error(errorMsg); - throw new IOException(errorMsg); - } - - // 解析响应 - String responseBody = response.body().string(); - log.info("查询生图状态响应: {}", responseBody); - - JSONObject responseJson = JSON.parseObject(responseBody); - int code = responseJson.getIntValue("code"); - - if (code != 0) { - String errorMsg = "查询生图状态失败,错误码: " + code + ", 错误信息: " + responseJson.getString("msg"); - log.error(errorMsg); - throw new IOException(errorMsg); - } - - return responseJson.getJSONObject("data"); - } - + /** * 下载生成的图片 * @param imageUrl 图片URL diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/QueryModel.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/QueryModel.java new file mode 100644 index 00000000..4f6c43bf --- /dev/null +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/Liblib/QueryModel.java @@ -0,0 +1,140 @@ +package com.dsideal.aiSupport.Util.Liblib; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.dsideal.aiSupport.Plugin.YamlProp; +import com.jfinal.kit.Prop; +import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix; + +/** + * Liblib模型查询工具类 + */ +public class QueryModel { + + private static final String API_BASE_URL = "https://openapi.liblibai.cloud"; + private static final String MODEL_VERSION_URL = API_BASE_URL + "/api/model/version/get"; + + private static final Logger log = LoggerFactory.getLogger(QueryModel.class); + + // API访问密钥 + protected static String accessKey; + protected static String secretKey; + public static Prop PropKit; + + static { + //加载配置文件 + String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix()); + PropKit = new YamlProp(configFile); + accessKey = PropKit.get("LIBLIB.accessKey"); + secretKey = PropKit.get("LIBLIB.secretKey"); + } + + /** + * 创建OkHttpClient实例 + * @return OkHttpClient实例 + */ + private static OkHttpClient createHttpClient() { + return new OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .writeTimeout(60, TimeUnit.SECONDS) + .build(); + } + + /** + * 查询模型版本信息 + * @param versionUuid 模型版本UUID + * @return 模型版本信息 + * @throws IOException 异常信息 + */ + public static JSONObject queryModelVersion(String versionUuid) throws IOException { + // 创建OkHttpClient + OkHttpClient client = createHttpClient(); + + // 构建请求体 + JSONObject requestBody = new JSONObject(); + requestBody.put("versionUuid", versionUuid); + + // 获取API路径 + String uri = "/api/model/version/get"; + + // 生成签名信息 + SignUtil.SignatureInfo signInfo = SignUtil.makeSign(uri, secretKey); + + // 构建带签名的URL + HttpUrl.Builder urlBuilder = HttpUrl.parse(MODEL_VERSION_URL).newBuilder() + .addQueryParameter("AccessKey", accessKey) + .addQueryParameter("Signature", signInfo.getSignature()) + .addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp())) + .addQueryParameter("SignatureNonce", signInfo.getSignatureNonce()); + + // 创建请求 + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString()); + Request request = new Request.Builder() + .url(urlBuilder.build()) + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + + // 执行请求 + log.info("查询模型版本信息: {}", requestBody.toJSONString()); + log.info("请求URL: {}", urlBuilder.build()); + Response response = client.newCall(request).execute(); + + // 处理响应 + if (!response.isSuccessful()) { + String errorMsg = "查询模型版本信息失败,状态码: " + response.code(); + log.error(errorMsg); + throw new IOException(errorMsg); + } + + // 解析响应 + String responseBody = response.body().string(); + log.info("查询模型版本信息响应: {}", responseBody); + + JSONObject responseJson = JSON.parseObject(responseBody); + int code = responseJson.getIntValue("code"); + + if (code != 0) { + String errorMsg = "查询模型版本信息失败,错误码: " + code + ", 错误信息: " + responseJson.getString("msg"); + log.error(errorMsg); + throw new IOException(errorMsg); + } + + return responseJson.getJSONObject("data"); + } + + /** + * 使用示例 + */ + public static void main(String[] args) { + try { + // 查询模型版本信息 + String versionUuid = "86961315d0b34f229d58fadb7f284972"; + JSONObject modelVersionInfo = queryModelVersion(versionUuid); + + // 打印模型版本信息 + log.info("模型版本信息: {}", modelVersionInfo); + + // 提取关键信息 + String modelName = modelVersionInfo.getString("modelName"); + String versionName = modelVersionInfo.getString("versionName"); + String description = modelVersionInfo.getString("description"); + + log.info("模型名称: {}", modelName); + log.info("版本名称: {}", versionName); + log.info("描述: {}", description); + + } catch (Exception e) { + log.error("查询模型版本信息失败", e); + } + } +}