parent
62f9749254
commit
e6b36b3c57
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue