AiClient.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. //package com.backendsys.SDK.Meitu;
  2. //
  3. //import java.io.File;
  4. //import java.io.FileInputStream;
  5. //import java.io.InputStream;
  6. //import java.net.URI;
  7. //import java.net.URISyntaxException;
  8. //import java.net.URL;
  9. //import java.util.ArrayList;
  10. //import java.util.HashMap;
  11. //import java.util.Iterator;
  12. //import java.util.List;
  13. //import java.util.Map;
  14. //import java.util.concurrent.Executors;
  15. //import java.util.concurrent.ScheduledExecutorService;
  16. //import java.util.concurrent.ThreadFactory;
  17. //import java.util.concurrent.TimeUnit;
  18. //import java.util.Map.Entry;
  19. //
  20. //import com.backendsys.SDK.Meitu.model.BizConfigResponseDTO;
  21. //import com.backendsys.SDK.Meitu.model.InferenceParamsDTO;
  22. //import com.backendsys.SDK.Meitu.model.MtlabParamsDTO;
  23. //import com.backendsys.SDK.Meitu.model.SdkRequestDTO;
  24. //import com.backendsys.SDK.Meitu.model.SdkResponseDTO;
  25. //import com.backendsys.SDK.Meitu.model.BizConfigResponseDTO.BizData;
  26. //import com.backendsys.SDK.Meitu.model.SdkRequestDTO.InitImage;
  27. //import com.obs.services.ObsClient;
  28. //import com.obs.services.ObsConfiguration;
  29. //import com.obs.services.exception.ObsException;
  30. //import com.obs.services.model.PutObjectResult;
  31. //
  32. //import lombok.Data;
  33. //import lombok.Getter;
  34. //import lombok.Setter;
  35. //import lombok.extern.slf4j.Slf4j;
  36. //import shade.fasterxml.jackson.annotation.JsonProperty;
  37. //import shade.fasterxml.jackson.databind.DeserializationFeature;
  38. //import shade.fasterxml.jackson.databind.JsonNode;
  39. //import shade.fasterxml.jackson.databind.ObjectMapper;
  40. //import shade.fasterxml.jackson.databind.node.ArrayNode;
  41. //import shade.okhttp3.HttpUrl;
  42. //import shade.okhttp3.MediaType;
  43. //import shade.okhttp3.OkHttpClient;
  44. //import shade.okhttp3.Request;
  45. //import shade.okhttp3.RequestBody;
  46. //import shade.okhttp3.Response;
  47. //
  48. ///**
  49. // * ai 访问客户端
  50. // */
  51. //@Slf4j
  52. //public class AiClient {
  53. // public static final String default_strategy_type = "mtai";
  54. // public static final String default_strategy_host = "https://strategy.app.meitudata.com";
  55. //
  56. // public static final int default_obs_socket_timeout = 30; // 单位秒
  57. // public static final int default_obs_connect_timeout = 10; // 单位秒
  58. //
  59. // public static final int default_api_socket_timeout = 45; // 单位秒 sync_timeout默认30秒,请求超时需要高于这个数
  60. // public static final int default_api_connect_timeout = 5; // 单位秒
  61. //
  62. // public static final int default_status_socket_timeout = 2; // 单位秒
  63. // public static final int default_status_connect_timeout = 2; // 单位秒
  64. //
  65. // public static final int status_code_suc = 10; // 成功
  66. // public static final int status_code_timeout = 9; // 同步请求超时
  67. // public static final int status_code_fail = 2; // 失败
  68. //
  69. // public static final int get_strategy_retry_times = 3; // 获取策略重试次数
  70. // public static final int get_strategy_retry_duration = 1000; // 重试间隔时间
  71. // public static final int get_strategy_connect_timeout = 10; // 重试间隔时间
  72. // public static final int get_strategy_socket_timeout = 30; // 重试间隔时间
  73. //
  74. // public static final int get_biz_config_connect_timeout = 10; // 重试间隔时间
  75. // public static final int get_biz_config_socket_timeout = 30; // 重试间隔时间
  76. // public static final String process_type_mtlab = "mtlab";
  77. // public static final String process_type_inference = "inference";
  78. //
  79. // private String openapiHost = null;
  80. //
  81. // @Setter(lombok.AccessLevel.PROTECTED)
  82. // private String ak;
  83. // @Setter(lombok.AccessLevel.PROTECTED)
  84. // private String sk;
  85. // @Setter(lombok.AccessLevel.PROTECTED)
  86. // private String types;
  87. // @Setter(lombok.AccessLevel.PROTECTED)
  88. // private String strategyHost;
  89. //
  90. // @Getter(lombok.AccessLevel.PROTECTED)
  91. // private Map<String, List<ObsClientAware>> obsAwareMap;
  92. // @Getter(lombok.AccessLevel.PROTECTED)
  93. // private Map<String, List<ApiAware>> apiAwareMap;
  94. // private OkHttpClient strategyOkHttpClient = new OkHttpClient.Builder()
  95. // .connectTimeout(get_strategy_connect_timeout, java.util.concurrent.TimeUnit.SECONDS)
  96. // .readTimeout(get_strategy_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  97. // .writeTimeout(get_strategy_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  98. // .build();
  99. //
  100. // private OkHttpClient bziConfigOkHttpClient = new OkHttpClient.Builder()
  101. // .connectTimeout(get_biz_config_connect_timeout, java.util.concurrent.TimeUnit.SECONDS)
  102. // .readTimeout(get_strategy_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  103. // .writeTimeout(get_biz_config_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  104. // .build();
  105. //
  106. // ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {
  107. // private int count = 0;
  108. //
  109. // @Override
  110. // public Thread newThread(Runnable r) {
  111. // count++;
  112. // return new Thread(r, "refresh_strategy-" + count);
  113. // }
  114. // });
  115. //
  116. // protected AiClient() {
  117. //
  118. // }
  119. //
  120. // /**
  121. // *
  122. // * @param accessKey
  123. // * @param secret
  124. // * @param "types" 需要获取处理token类型(效果类型,创意玩法/文生图/图生图等),支持多个名称,例:type=p1,p2
  125. // */
  126. // public AiClient(String accessKey, String secret) {
  127. // this(accessKey, secret, default_strategy_type, default_strategy_host);
  128. // }
  129. //
  130. // /**
  131. // *
  132. // * @param ak
  133. // * @param sk
  134. // * @param types 需要获取处理token类型(效果类型,创意玩法/文生图/图生图等),支持多个名称,例:type=p1,p2
  135. // * @param strategyHost 策略服务地址
  136. // */
  137. // public AiClient(String ak, String sk, String types, String strategyHost) {
  138. // this.ak = ak;
  139. // this.sk = sk;
  140. // this.types = types;
  141. // this.strategyHost = strategyHost;
  142. // init();
  143. // }
  144. //
  145. // protected SdkResponseDTO.Result push(SdkRequestDTO sdkRequestDTO, String type, String processType) {
  146. // log.info("push start, sdkRequestDTO:{}", sdkRequestDTO);
  147. // if (sdkRequestDTO.getTask() == null || sdkRequestDTO.getTask().length() == 0) {
  148. // throw new AIException(AIException.code_params_err, "task is null");
  149. // }
  150. // if (sdkRequestDTO.getTaskType() == null || sdkRequestDTO.getTaskType().length() == 0) {
  151. // throw new AIException(AIException.code_params_err, "taskType is null");
  152. // }
  153. //
  154. // List<ApiAware> apiAwares = apiAwareMap.get(type);
  155. // if (apiAwares == null) {
  156. // throw new AIException(AIException.code_params_err,
  157. // String.format("type:%s is not configured", type));
  158. // }
  159. // SdkResponseDTO.Result result = retryPush(sdkRequestDTO, apiAwares, processType);
  160. //
  161. // return result;
  162. // }
  163. //
  164. // /**
  165. // * 美图设计室任务提交接口
  166. // *
  167. // * @param mtlabParamsDTO
  168. // * @return
  169. // */
  170. // public SdkResponseDTO.Result pushMtlabTask(MtlabParamsDTO mtlabParamsDTO) {
  171. // mtlabParamsDTO.setTaskType("mtlab");
  172. // for (InitImage initImage : mtlabParamsDTO.getInitImages()) {
  173. // String url = initImage.getUrl();
  174. // url = upload(default_strategy_type, url);
  175. // initImage.setUrl(url);
  176. // }
  177. // return push(mtlabParamsDTO, default_strategy_type, process_type_mtlab);
  178. // }
  179. //
  180. // /**
  181. // * 获取业务配置,业务配置是AI服务在调用推理时,用来约定用户能使用的模型、风格与图像尺寸的配置。
  182. // */
  183. // public BizData getBusinessConfig() {
  184. // String url = String.format("%s/whee/business/config.json", openapiHost);
  185. // Map<String, String> header = new HashMap<>();
  186. // Map<String, String> params = new HashMap<>();
  187. //
  188. // Request reuqest = buildGetRequest(url, params, header);
  189. //
  190. // Response response = null;
  191. // try {
  192. // response = bziConfigOkHttpClient.newCall(reuqest).execute();
  193. // } catch (Exception ex) {
  194. // throw new AIException(AIException.code_cloud_biz_config_net_err, "Request exception", ex);
  195. // }
  196. // try {
  197. // String body = "";
  198. // try {
  199. // body = response.body().string();
  200. // } catch (Exception ex) {
  201. // // 忽略异常
  202. // }
  203. //
  204. // // body反序列到BizConfigResponseDTO
  205. // ObjectMapper objectMapper = new ObjectMapper();
  206. // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  207. // BizConfigResponseDTO bizConfigResponseDTO = null;
  208. // try {
  209. // bizConfigResponseDTO = objectMapper.readValue(body, BizConfigResponseDTO.class);
  210. // } catch (Exception ex) {
  211. // throw new AIException(AIException.code_cloud_biz_config_err, "Deserialization Exception", ex);
  212. // }
  213. //
  214. // if (bizConfigResponseDTO.getCode() != 0) {
  215. // throw new AIException(AIException.code_cloud_biz_config_err,
  216. // String.format("Request exception:httpCode=%s,messge=%s", response.code(), response.message()));
  217. // }
  218. // return bizConfigResponseDTO.getData();
  219. // } finally {
  220. // if (response != null) {
  221. // response.close();
  222. // }
  223. // }
  224. //
  225. // }
  226. //
  227. // /**
  228. // * 执行推理,推理接口包含文生图与图生图
  229. // *
  230. // * @param inferenceParamsDTO
  231. // * @return
  232. // */
  233. // public SdkResponseDTO.Result inference(InferenceParamsDTO inferenceParamsDTO) {
  234. // ObjectMapper objectMapper = new ObjectMapper();
  235. // if (inferenceParamsDTO.getModel() == null || inferenceParamsDTO.getModel().length() == 0) {
  236. // throw new AIException(AIException.code_params_err, "model is null");
  237. // }
  238. // if (inferenceParamsDTO.getPrompt() == null || inferenceParamsDTO.getPrompt().length() == 0) {
  239. // throw new AIException(AIException.code_params_err, "prompt is null");
  240. // }
  241. // if (inferenceParamsDTO.getModelHash() == null || inferenceParamsDTO.getModelHash().length() == 0) {
  242. // inferenceParamsDTO.setModelHash("XZKFKEVaKRPSDC5PHl2Fi0xBbYts25xnfA6hsfEp");
  243. // }
  244. // if (inferenceParamsDTO.getModelPlatform() == null || inferenceParamsDTO.getModelPlatform().length() == 0) {
  245. // throw new AIException(AIException.code_params_err, "model platform is null");
  246. // }
  247. //
  248. // log.info("inference start, inferenceParamsDTO:{}", jsonToString(inferenceParamsDTO, objectMapper));
  249. // long start = System.currentTimeMillis();
  250. // SdkResponseDTO.Result result = doInference(inferenceParamsDTO, default_strategy_type);
  251. //
  252. // log.info("inference end, result:{} latency:{}", jsonToString(result, objectMapper),
  253. // System.currentTimeMillis() - start);
  254. // return result;
  255. //
  256. // }
  257. //
  258. // /**
  259. // * 推理接口,如果initImages有传入为图生图,没有传入为文生图
  260. // *
  261. // * @param inferenceParamsDTO
  262. // * @param type
  263. // */
  264. // private SdkResponseDTO.Result doInference(InferenceParamsDTO inferenceParamsDTO, String type) {
  265. // List<InitImage> initImageObjectList = new ArrayList<>();
  266. // String[] initImage = inferenceParamsDTO.getInitImages();
  267. // String task;
  268. // if (initImage != null && initImage.length > 0) {
  269. // String[] newInitImage = new String[initImage.length];
  270. // for (int i = 0; i < initImage.length; i++) {
  271. // String url = upload(type, initImage[i]);
  272. // newInitImage[i] = url;
  273. // InitImage image = new InitImage();
  274. // image.setUrl(url);
  275. // initImageObjectList.add(image);
  276. // }
  277. // task = "img2img";
  278. // inferenceParamsDTO.setInitImages(newInitImage);
  279. // } else {
  280. // task = "txt2img";
  281. // }
  282. //
  283. // SdkRequestDTO request = new SdkRequestDTO();
  284. // request.setInitImages(initImageObjectList);
  285. // request.setTaskType("inference");
  286. // request.setTask(task);
  287. // request.setParams(jsonToString(inferenceParamsDTO, null));
  288. // SdkResponseDTO.Result result = push(request, type, process_type_inference);
  289. // return result;
  290. //
  291. // }
  292. //
  293. // /**
  294. // * 重试的机制放在AiClientRefreshStrategyTask
  295. // */
  296. // private void init() {
  297. // String configStr = getStrategy();
  298. // log.info("Get Strategy Success, config:{}", configStr);
  299. // ConfigAware configAware = parseTockenConfig(configStr);
  300. // initObsClient(configAware.getObsConfigMap());
  301. // initApiConfig(configAware.getApiConfigMap());
  302. // int minTTL = configAware.getMinTTL();
  303. // if (minTTL <= 0) {
  304. // minTTL = 3600;
  305. // }
  306. // long delay = minTTL / 2;
  307. // log.info("Add refresh strategy task, delay={} ttl={}", delay, minTTL);
  308. // scheduledExecutorService.schedule(new AiClientRefreshStrategyTask(this), delay, TimeUnit.SECONDS);
  309. // }
  310. //
  311. // protected Request buildGetRequest(String url, Map<String, String> params, Map<String, String> headers) {
  312. // HttpUrl.Builder httpUrlBuild = HttpUrl
  313. // .parse(url)
  314. // .newBuilder();
  315. //
  316. // for (Entry<String, String> entry : params.entrySet()) {
  317. // httpUrlBuild.addQueryParameter(entry.getKey(), entry.getValue());
  318. // }
  319. //
  320. // HttpUrl httpUrl = httpUrlBuild.build();
  321. //
  322. // Signer signer = new Signer(ak, sk);
  323. // Map<String, String> sigHeader = null;
  324. //
  325. // try {
  326. // sigHeader = signer.sign(httpUrl.toString(), "GET", headers, "");
  327. // } catch (URISyntaxException ex) {
  328. // throw new AIException(AIException.code_sig_err, "Sign exception", ex);
  329. // }
  330. //
  331. // log.info("get request, url={} header={}", httpUrl.toString(), jsonToString(sigHeader, null));
  332. //
  333. // Request.Builder request = new Request.Builder()
  334. // .url(httpUrlBuild.build()).get();
  335. //
  336. // sigHeader.forEach((key, value) -> {
  337. // request.addHeader(key, value);
  338. // });
  339. // return request.build();
  340. // }
  341. //
  342. // /**
  343. // * 关闭客户端
  344. // */
  345. // public void close() {
  346. // log.info("close ai client");
  347. //
  348. // scheduledExecutorService.shutdownNow();
  349. //
  350. // obsAwareMap.forEach((s, clientList) -> {
  351. // for (ObsClientAware obsClientAware : clientList) {
  352. // try {
  353. // obsClientAware.getObsClient().close();
  354. // } catch (Exception ex) {
  355. // throw new AIException(AIException.code_obs_close_err, "Close obs client exception", ex);
  356. // }
  357. //
  358. // }
  359. // });
  360. //
  361. // bziConfigOkHttpClient.dispatcher().executorService().shutdownNow();
  362. //
  363. // strategyOkHttpClient.dispatcher().executorService().shutdownNow();
  364. //
  365. // apiAwareMap.forEach((k, clientList) -> {
  366. // for (ApiAware apiAware : clientList) {
  367. // if (!apiAware.getPushClient().dispatcher().executorService().isShutdown()) {
  368. // apiAware.getPushClient().dispatcher().executorService().shutdownNow();
  369. // }
  370. // if (!apiAware.getStatusClient().dispatcher().executorService().isShutdown()) {
  371. // apiAware.getStatusClient().dispatcher().executorService().shutdownNow();
  372. // }
  373. // }
  374. // });
  375. // }
  376. //
  377. // /**
  378. // * 请求策略服务获取策略配置
  379. // *
  380. // * @return 策略配置
  381. // */
  382. // protected String getStrategy() {
  383. // long start = System.currentTimeMillis();
  384. // String url = String.format("%s/ai/token_policy", strategyHost);
  385. // Map<String, String> params = new HashMap<>();
  386. // params.put("type", types);
  387. // URI uri = URI.create(url);
  388. // Map<String, String> headMap = new HashMap<>();
  389. //
  390. // Request request = buildGetRequest(url, params, headMap);
  391. //
  392. // log.info("get strategy request, url={}", request.url().toString());
  393. //
  394. // Response response = null;
  395. // try {
  396. // response = strategyOkHttpClient.newCall(request).execute();
  397. // } catch (Exception ex) {
  398. // throw new AIException(AIException.code_strategy_net_error, "Request exception", ex);
  399. // }
  400. // try {
  401. //
  402. // String body = "";
  403. // try {
  404. // body = response.body().string();
  405. // } catch (Exception ex) {
  406. // // 忽略异常
  407. // }
  408. //
  409. // log.info("get strategy response, latency={} url={} httpcode={} body={}", System.currentTimeMillis() - start,
  410. // url, response.code(), body);
  411. //
  412. // if (!response.isSuccessful()) {
  413. // throw new AIException(AIException.code_strategy_err,
  414. // String.format("Request exception:httpCode=%s,messge=%s", response.code(), response.message()));
  415. // }
  416. // return body;
  417. //
  418. // } finally {
  419. // response.close();
  420. // }
  421. // }
  422. //
  423. // /**
  424. // * 解析返回后端返回的参数
  425. // *
  426. // * @param json
  427. // * @return
  428. // */
  429. // protected ConfigAware parseTockenConfig(String jsonString) {
  430. // ConfigAware configAware = new ConfigAware();
  431. // ObjectMapper objectMapper = new ObjectMapper();
  432. // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  433. // JsonNode jsonNode = null;
  434. // try {
  435. // jsonNode = objectMapper.readTree(jsonString);
  436. // } catch (Exception ex) {
  437. // throw new AIException(AIException.code_strategy_err, "反序列化异常", ex);
  438. // }
  439. //
  440. // int code = jsonNode.get("code").asInt();
  441. //
  442. // if (code != 0) {
  443. // int errorCode = jsonNode.get("error_code").asInt();
  444. // String message = jsonNode.get("message").asText();
  445. // throw new AIException(AIException.code_strategy_err, String.format("%s:%s", errorCode, message));
  446. // }
  447. // JsonNode dataNode = jsonNode.get("data");
  448. // if (dataNode == null || dataNode.isNull()) {
  449. // throw new AIException(AIException.code_strategy_err, "data is null");
  450. // }
  451. // Map<String, List<ObsConfig>> obsConfigMap = new HashMap<>();
  452. // Map<String, List<APIConfig>> apiConfigMap = new HashMap<>();
  453. // configAware.setApiConfigMap(apiConfigMap);
  454. // configAware.setObsConfigMap(obsConfigMap);
  455. // Iterator<Entry<String, JsonNode>> fields = dataNode.fields();
  456. // int minTTL = Integer.MAX_VALUE;
  457. // while (fields.hasNext()) {
  458. // Entry<String, JsonNode> field = fields.next();
  459. // String bizName = field.getKey();
  460. // JsonNode bizConfig = field.getValue();
  461. // List<ObsConfig> obsConfigs = obsConfigMap.get(bizName);
  462. // if (obsConfigs == null) {
  463. // obsConfigs = new ArrayList<>();
  464. // obsConfigMap.put(bizName, obsConfigs);
  465. // }
  466. //
  467. // List<APIConfig> apiConfigs = apiConfigMap.get(bizName);
  468. // if (apiConfigs == null) {
  469. // apiConfigs = new ArrayList<>();
  470. // apiConfigMap.put(bizName, apiConfigs);
  471. // }
  472. //
  473. // int ttl = parseObsConfig(objectMapper, bizConfig.get("upload"), obsConfigs);
  474. // if (minTTL > ttl) {
  475. // minTTL = ttl;
  476. // }
  477. // parseApiConfig(objectMapper, bizConfig.get("api"), apiConfigs);
  478. // }
  479. //
  480. // configAware.setMinTTL(minTTL);
  481. //
  482. // return configAware;
  483. // }
  484. //
  485. // private void parseApiConfig(ObjectMapper objectMapper, JsonNode apiConfigJSON, List<APIConfig> apiConfigs) {
  486. // List<JsonNode> configJSONArr = parseConfigJsonNodeArr(apiConfigJSON);
  487. // for (JsonNode curConfigJSON : configJSONArr) {
  488. // APIConfig apiConfig = null;
  489. // try {
  490. // apiConfig = objectMapper.treeToValue(curConfigJSON, APIConfig.class);
  491. // } catch (Exception ex) {
  492. // throw new AIException(AIException.code_strategy_err, "Deserialization Exception", ex);
  493. // }
  494. // if (openapiHost == null && apiConfig.getUrl() != null) {
  495. // openapiHost = apiConfig.getUrl();
  496. // }
  497. //
  498. // apiConfigs.add(apiConfig);
  499. // }
  500. // }
  501. //
  502. // /**
  503. // *
  504. // * @param objectMapper
  505. // * @param obsConfigJSON
  506. // * @param obsConfigs
  507. // * @return 最小的ttl
  508. // * @throws Exception
  509. // */
  510. // private int parseObsConfig(ObjectMapper objectMapper, JsonNode obsConfigJSON, List<ObsConfig> obsConfigs) {
  511. // int minTTL = Integer.MAX_VALUE;
  512. // List<JsonNode> configJSONArr = parseConfigJsonNodeArr(obsConfigJSON);
  513. // for (JsonNode curConfigJSON : configJSONArr) {
  514. // ObsConfig apiConfig = null;
  515. // try {
  516. // apiConfig = objectMapper.treeToValue(curConfigJSON, ObsConfig.class);
  517. // } catch (Exception ex) {
  518. // throw new AIException(AIException.code_strategy_err, "Deserialization Exception", ex);
  519. // }
  520. // if (minTTL > apiConfig.getTtl()) {
  521. // minTTL = apiConfig.getTtl();
  522. // }
  523. // obsConfigs.add(apiConfig);
  524. // }
  525. // return minTTL;
  526. // }
  527. //
  528. // private List<JsonNode> parseConfigJsonNodeArr(JsonNode configJSON) {
  529. // List<JsonNode> resultList = new ArrayList<>();
  530. // ArrayNode orderJSONArr = configJSON.withArray("order");
  531. // for (JsonNode jsonNode : orderJSONArr) {
  532. // String order = jsonNode.asText();
  533. // JsonNode curConfigJSON = configJSON.get(order);
  534. // if (curConfigJSON == null || curConfigJSON.isNull()) {
  535. // continue;
  536. // }
  537. // resultList.add(curConfigJSON);
  538. // }
  539. //
  540. // return resultList;
  541. //
  542. // }
  543. //
  544. // /**
  545. // * 初始化api访问配置
  546. // */
  547. // protected void initApiConfig(Map<String, List<APIConfig>> apiConfigMap) {
  548. // // 为了允许多个type有多个配置
  549. // Map<String, List<ApiAware>> apiAwareMap = new HashMap<>();
  550. // // 当前status是一个不可变更的值
  551. // OkHttpClient statusClient = new OkHttpClient.Builder()
  552. // .connectTimeout(default_status_connect_timeout, java.util.concurrent.TimeUnit.SECONDS)
  553. // .writeTimeout(default_status_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  554. // .readTimeout(default_status_socket_timeout, java.util.concurrent.TimeUnit.SECONDS)
  555. // .build();
  556. //
  557. // for (Entry<String, List<APIConfig>> entry : apiConfigMap.entrySet()) {
  558. // List<APIConfig> apiConfigs = entry.getValue();
  559. // String type = entry.getKey();
  560. // List<ApiAware> apiAwaress = apiAwareMap.get(type);
  561. // if (apiAwaress == null) {
  562. // apiAwaress = new ArrayList<>();
  563. // apiAwareMap.put(type, apiAwaress);
  564. // }
  565. // for (APIConfig apiConfig : apiConfigs) {
  566. // OkHttpClient httpClient = new OkHttpClient.Builder()
  567. // .connectTimeout(apiConfig.getConnectTimeout() <= 0 ? default_api_connect_timeout
  568. // : apiConfig.getConnectTimeout(), java.util.concurrent.TimeUnit.SECONDS)
  569. // .readTimeout(apiConfig.getSocketTimeout() <= 0 ? default_api_socket_timeout
  570. // : apiConfig.getSocketTimeout(), java.util.concurrent.TimeUnit.SECONDS)
  571. // .build();
  572. //
  573. // apiAwaress.add(new ApiAware(httpClient, statusClient, apiConfig, apiConfig.getUrl()));
  574. // if (apiConfig.getBackupUrl() != null && apiConfig.getBackupUrl().length() > 0) {
  575. // apiAwaress.add(new ApiAware(httpClient, statusClient, apiConfig, apiConfig.getBackupUrl()));
  576. // }
  577. // }
  578. // }
  579. // this.apiAwareMap = apiAwareMap;
  580. //
  581. // }
  582. //
  583. // /**
  584. // * 根据下发的obsConfig初始化obs客户端
  585. // */
  586. // protected void initObsClient(Map<String, List<ObsConfig>> obsConfigMap) {
  587. // Map<String, List<ObsClientAware>> obsClientAwareMap = new HashMap<>();
  588. // for (Entry<String, List<ObsConfig>> entry : obsConfigMap.entrySet()) {
  589. // List<ObsConfig> obsConfigs = entry.getValue();
  590. // String type = entry.getKey();
  591. // List<ObsClientAware> obsClientAwares = obsClientAwareMap.get(type);
  592. // if (obsClientAwares == null) {
  593. // obsClientAwares = new ArrayList<>();
  594. // obsClientAwareMap.put(type, obsClientAwares);
  595. // }
  596. // for (ObsConfig obsConfig : obsConfigs) {
  597. //
  598. // ObsConfiguration config = buildObjConfiguration(obsConfig, obsConfig.getUrl());
  599. // ObsClient obsClient = new ObsClient(obsConfig.getCredentials().getAccessKey(),
  600. // obsConfig.getCredentials().getSecretKey(),
  601. // obsConfig.getCredentials().getSessionToken(),
  602. // config);
  603. // obsClientAwares.add(new ObsClientAware(obsClient, obsConfig));
  604. //
  605. // if (obsConfig.getBackupUrl() != null && obsConfig.getBackupUrl().length() > 0) {
  606. // ObsConfiguration backupConfig = buildObjConfiguration(obsConfig, obsConfig.getBackupUrl());
  607. // ObsClient bakcupClient = new ObsClient(obsConfig.getCredentials().getAccessKey(),
  608. // obsConfig.getCredentials().getSecretKey(),
  609. // obsConfig.getCredentials().getSessionToken(),
  610. // backupConfig);
  611. // obsClientAwares.add(new ObsClientAware(bakcupClient, obsConfig));
  612. // }
  613. //
  614. // }
  615. // }
  616. // this.obsAwareMap = obsClientAwareMap;
  617. // }
  618. //
  619. // private ObsConfiguration buildObjConfiguration(ObsConfig obsConfig, String url) {
  620. // ObsConfiguration config = new ObsConfiguration();
  621. // if (obsConfig.getSocketTimeout() <= 0) {
  622. // config.setSocketTimeout(default_obs_socket_timeout * 1000);
  623. // } else {
  624. // config.setSocketTimeout(obsConfig.getSocketTimeout() * 1000);
  625. // }
  626. //
  627. // if (obsConfig.getConnectTimeout() <= 0) {
  628. // config.setConnectionTimeout(default_obs_connect_timeout * 1000);
  629. // } else {
  630. // config.setConnectionTimeout(obsConfig.getConnectTimeout() * 1000);
  631. // }
  632. //
  633. // config.setEndPoint(url);
  634. // return config;
  635. // }
  636. //
  637. // /**
  638. // *
  639. // * @throws Exception
  640. // */
  641. // protected String upload(String type, String uri) {
  642. // InputStream inputStream = null;
  643. // // 允许上传http的资源链接,因为需要从网络现在后上传到obs,需要控制资源大小在10M以内
  644. // if (uri.startsWith("https://") || uri.startsWith("http://")) {
  645. // try {
  646. // URL url = new URL(uri);
  647. // inputStream = url.openStream();
  648. // } catch (Exception ex) {
  649. // throw new AIException(AIException.code_params_resource,
  650. // String.format("Resource access errorm, file:%s", uri), ex);
  651. // }
  652. // } else {
  653. // File file = new File(uri);
  654. // if (!file.exists()) {
  655. // throw new AIException(AIException.code_params_resource_notfound,
  656. // (String.format("file:%s is not exists", uri)));
  657. // }
  658. // try {
  659. // inputStream = new FileInputStream(file);
  660. // } catch (Exception ex) {
  661. // throw new AIException(AIException.code_params_resource,
  662. // String.format("Resource access errorm, file:%s", uri), ex);
  663. // }
  664. // }
  665. //
  666. // List<ObsClientAware> obsClientAwares = obsAwareMap.get(type);
  667. // if (obsClientAwares == null) {
  668. // throw new AIException(AIException.code_params_err, String.format("type:%s is not configured", type));
  669. // }
  670. //
  671. // Exception ex = null;
  672. //
  673. // for (ObsClientAware obsClientAware : obsClientAwares) {
  674. // try {
  675. // log.info("upload start, type={} uri={} key={} bucket={} client={}", type, uri,
  676. // obsClientAware.getObsConfig().getKey(),
  677. // obsClientAware.getObsConfig().getBucket(),
  678. // obsClientAware.getObsClient().toString());
  679. //
  680. // PutObjectResult result = obsClientAware.getObsClient()
  681. // .putObject(obsClientAware.getObsConfig().getBucket(),
  682. // obsClientAware.getObsConfig().getKey(),
  683. // inputStream);
  684. // String objUrl = result.getObjectUrl();
  685. // return objUrl;
  686. // } catch (ObsException obsException) {
  687. // log.warn(String.format("Upload OBS exception,code:%s,message:%s", obsException.getResponseCode(),
  688. // obsException.getLocalizedMessage()), obsException);
  689. // ex = obsException;
  690. // }
  691. // }
  692. // throw new AIException(AIException.code_upload_err, "Upload OBS exception", ex);
  693. // }
  694. //
  695. // /**
  696. // * 重试创建AI处理任务
  697. // *
  698. // * @param sdkRequestDTO
  699. // * @return
  700. // * @throws Exception
  701. // */
  702. // private SdkResponseDTO.Result retryPush(SdkRequestDTO sdkRequestDTO, List<ApiAware> apiAwares, String processType) {
  703. // AIException finalEx = null;
  704. // for (ApiAware apiAware : apiAwares) {
  705. // APIConfig apConfig = apiAware.getApiConfig();
  706. // for (int i = 0; i < apConfig.getRetry().times; i++) {
  707. // sleepRetry(i, apConfig.getRetry());
  708. // SdkResponseDTO response = null;
  709. // try {
  710. // response = doPush(sdkRequestDTO, apiAware, processType);
  711. // } catch (AIException ex) {
  712. // log.info("push retry, times=" + i, ex);
  713. // // 权限问题直接返回
  714. // if (ex.getErrorCode() == AIException.code_cloud_process_unauthorized
  715. // || ex.getErrorCode() == AIException.code_params_err) {
  716. // throw ex;
  717. // }
  718. // finalEx = ex;
  719. // continue;
  720. // } catch (Exception ex) {
  721. // log.info("push retry, times=" + i, ex);
  722. // finalEx = new AIException(AIException.code_cloud_process_err, "Request exception", ex);
  723. // continue;
  724. // }
  725. // if (response == null) {
  726. // continue;
  727. // }
  728. //
  729. // // 使用status异步获取结果
  730. // if (response.getCode() == 0 && response.getData().getStatus() == status_code_suc) {
  731. // return response.getData().getResult();
  732. // }
  733. // if (response.getCode() == 0 && response.getData().getStatus() == status_code_timeout) {
  734. // if (response.getData() == null || response.getData().getResult() == null
  735. // || response.getData().getResult().getId() == null) {
  736. // throw new AIException(AIException.code_cloud_process_err,
  737. // "Task ID not found in backend request");
  738. // }
  739. // return getResultStatus(sdkRequestDTO, response.getData().getResult().getId(), apiAware);
  740. // } else {
  741. // // 后端出现异常,不在进行循环
  742. // throw new AIException(response.getCode(),
  743. // response.getMessage());
  744. // }
  745. // }
  746. // }
  747. // throw finalEx;
  748. // }
  749. //
  750. // /**
  751. // * 重新获取状态
  752. // *
  753. // * @param taskId
  754. // * @return
  755. // * @throws Exception
  756. // */
  757. // private SdkResponseDTO.Result getResultStatus(SdkRequestDTO requestDTO, String taskId, ApiAware apiAware) {
  758. // AIException finalEx = null;
  759. // StatusQuery statusQuery = apiAware.getApiConfig().getStatusQuery();
  760. // String durations = statusQuery.durations;
  761. // if (durations == null || durations.length() == 0) {
  762. // durations = String.valueOf(statusQuery.defaultDuration);
  763. // }
  764. //
  765. // String[] durationArr = durations.split(",");
  766. // for (String duration : durationArr) {
  767. // try {
  768. // Thread.sleep(Integer.parseInt(duration));
  769. // } catch (InterruptedException e) {
  770. // log.warn("sleep异常", e);
  771. // }
  772. // SdkResponseDTO responseDTO = null;
  773. // try {
  774. // responseDTO = doResultStatus(requestDTO.getContext(), taskId, apiAware);
  775. // } catch (AIException ex) {
  776. // finalEx = ex;
  777. // continue;
  778. // } catch (Exception ex) {
  779. // finalEx = new AIException(AIException.code_cloud_get_status_err, "Get status exception", ex);
  780. // continue;
  781. //
  782. // }
  783. //
  784. // if (responseDTO == null) {
  785. // continue;
  786. // }
  787. //
  788. // if (responseDTO.getCode() == 0 && responseDTO.getData().getStatus() == status_code_suc) {
  789. // // 使用status异步获取结果
  790. // return responseDTO.getData().getResult();
  791. // } else if (responseDTO.getCode() == 0 && responseDTO.getData().getStatus() == status_code_fail) {
  792. // // 后端出现异常,不在进行循环
  793. // throw new AIException(responseDTO.getCode(),
  794. // responseDTO.getMessage());
  795. // } else if (responseDTO.getCode() != 0) {
  796. // throw new AIException(responseDTO.getCode(),
  797. // responseDTO.getMessage());
  798. // }
  799. //
  800. // }
  801. //
  802. // if (finalEx != null) {
  803. // throw finalEx;
  804. // } else {
  805. // throw new AIException(AIException.code_cloud_process_timeout, "Process timeout");
  806. // }
  807. // }
  808. //
  809. // /**
  810. // * 获取
  811. // *
  812. // * @param requestDTO
  813. // * @param taskId
  814. // * @param apiAware
  815. // * @return
  816. // */
  817. // private SdkResponseDTO doResultStatus(String contextParam, String taskId, ApiAware apiAware) {
  818. // long start = System.currentTimeMillis();
  819. // ObjectMapper objectMapper = new ObjectMapper();
  820. // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  821. // Map<String, String> headers = new HashMap<>();
  822. // Map<String, String> params = new HashMap<>();
  823. // params.put("task_id", taskId);
  824. // if (contextParam != null && contextParam.length() > 0) {
  825. // params.put("context", contextParam);
  826. // }
  827. //
  828. // Request request = buildGetRequest(apiAware.getStatusUrl(), params, headers);
  829. //
  830. // Response response = null;
  831. // try {
  832. // response = apiAware.getPushClient().newCall(request).execute();
  833. // } catch (Exception ex) {
  834. // throw new AIException(AIException.code_cloud_process_net_err, "Request exception", ex);
  835. // }
  836. //
  837. // try {
  838. // String body = "";
  839. // try {
  840. // body = response.body().string();
  841. // } catch (Exception ex) {
  842. // // 忽略异常
  843. // }
  844. //
  845. // log.info("get status response, latency={} url={} httpcode={} body={}", System.currentTimeMillis() - start,
  846. // apiAware.getPushUrl(), response.code(), body);
  847. //
  848. // if (!response.isSuccessful()) {
  849. // throw new AIException(AIException.code_cloud_process_err,
  850. // String.format("Request exception:httpCode:", response.code()));
  851. // }
  852. //
  853. // try {
  854. // SdkResponseDTO responseDTO = objectMapper.readValue(body, SdkResponseDTO.class);
  855. // return responseDTO;
  856. // } catch (Exception ex) {
  857. // throw new AIException(AIException.code_cloud_process_err, "Result deserialization exception", ex);
  858. // }
  859. // } finally {
  860. // response.close();
  861. // }
  862. // }
  863. //
  864. // private void sleepRetry(int index, RetryConfig retryConfig) {
  865. // if (index == 0) {
  866. // return;
  867. // }
  868. // try {
  869. // Thread.sleep(retryConfig.duration);
  870. // } catch (InterruptedException e) {
  871. // log.warn("sleep异常", e);
  872. // }
  873. // }
  874. //
  875. // /**
  876. // * 提交ai 任务
  877. // *
  878. // * @param sdkRequestDTO
  879. // * @return
  880. // */
  881. // protected SdkResponseDTO doPush(SdkRequestDTO sdkRequestDTO, ApiAware apiAware, String processType) {
  882. // String url = apiAware.getPushUrl();
  883. // if (process_type_inference.equals(processType)) {
  884. // url = apiAware.getInferenceUrl();
  885. // }
  886. // long start = System.currentTimeMillis();
  887. // String json = null;
  888. // ObjectMapper objectMapper = new ObjectMapper();
  889. // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  890. // try {
  891. // json = objectMapper.writeValueAsString(sdkRequestDTO);
  892. // } catch (Exception ex) {
  893. // throw new AIException(AIException.code_serialization_err, "Json exception", ex);
  894. // }
  895. // URI uri = URI.create(url);
  896. // String host = uri.getHost();
  897. // Map<String, String> headers = new HashMap<>();
  898. // headers.put(Signer.HeaderHost, host);
  899. //
  900. // Signer signer = new Signer(ak, sk);
  901. // Map<String, String> sigHeader = null;
  902. // try {
  903. // sigHeader = signer.sign(url, "POST", headers, json);
  904. // } catch (URISyntaxException ex) {
  905. // throw new AIException(AIException.code_cloud_process_err, "Sign exception", ex);
  906. // }
  907. //
  908. // Request.Builder request = new Request.Builder()
  909. // .post(RequestBody.create(json, MediaType.parse("application/json")))
  910. // .url(url);
  911. //
  912. // sigHeader.forEach((key, value) -> {
  913. // if (Signer.HeaderHost.equals(key)) {
  914. // return;
  915. // }
  916. // request.addHeader(key, value);
  917. // });
  918. //
  919. // log.info("push request, url={} body={} header={}", url, json,
  920. // jsonToString(sigHeader, objectMapper));
  921. //
  922. // OkHttpClient okHttpClient = apiAware.getPushClient();
  923. //
  924. // /**
  925. // * 设置同步超时时间
  926. // */
  927. // if (sdkRequestDTO.getSyncTimeout() > 0) {
  928. // OkHttpClient.Builder builder = apiAware.getPushClient().newBuilder();
  929. // int socketTimeout = sdkRequestDTO.getSyncTimeout() * 2;
  930. // if (sdkRequestDTO.getSyncTimeout() > 0) {
  931. // builder.readTimeout(socketTimeout, TimeUnit.SECONDS);
  932. // builder.writeTimeout(socketTimeout, TimeUnit.SECONDS);
  933. // }
  934. // okHttpClient = builder.build();
  935. // }
  936. //
  937. // Response response = null;
  938. // try {
  939. // response = okHttpClient.newCall(request.build()).execute();
  940. // } catch (Exception ex) {
  941. // throw new AIException(AIException.code_cloud_process_net_err, "Request exception", ex);
  942. // }
  943. //
  944. // try {
  945. // String body = null;
  946. // try {
  947. // body = response.body().string();
  948. // } catch (Exception ex) {
  949. // // 忽略异常
  950. // }
  951. //
  952. // log.info("push response, latency={} url={} httpcode={} body={}", System.currentTimeMillis() - start,
  953. // url, response.code(), body);
  954. //
  955. // if (!response.isSuccessful()) {
  956. // if (response.code() == 400) {
  957. // int code = response.code();
  958. // String msg = response.message();
  959. // try {
  960. // SdkResponseDTO responseDTO = objectMapper.readValue(body, SdkResponseDTO.class);
  961. // code = responseDTO.getCode();
  962. // msg = responseDTO.getMessage();
  963. // } catch (Exception ex) {
  964. // // 忽略错误,出现错误也是正常情况。
  965. // }
  966. // throw new AIException(AIException.code_params_err,
  967. // String.format("Request exception:code=%s, message=%s", code, msg));
  968. // } else if (response.code() == 401 || response.code() == 403) {
  969. // throw new AIException(AIException.code_cloud_process_unauthorized,
  970. // String.format("Request exception:httpCode=%s, message=%s", response.code(),
  971. // response.message()));
  972. // } else {
  973. // throw new AIException(AIException.code_cloud_process_err,
  974. // String.format("Request exception:httpCode=%s, message=%s", response.code(),
  975. // response.message()));
  976. // }
  977. //
  978. // }
  979. //
  980. // try {
  981. // SdkResponseDTO responseDTO = objectMapper.readValue(body, SdkResponseDTO.class);
  982. // return responseDTO;
  983. // } catch (Exception ex) {
  984. // throw new AIException(AIException.code_cloud_process_err, "Result deserialization exception", ex);
  985. // }
  986. // } finally {
  987. // response.close();
  988. // }
  989. // }
  990. //
  991. // protected String jsonToString(Object obj, ObjectMapper objectMapper) {
  992. // if (objectMapper == null) {
  993. // objectMapper = new ObjectMapper();
  994. // }
  995. //
  996. // try {
  997. // return objectMapper.writeValueAsString(obj);
  998. // } catch (Exception ex) {
  999. // throw new AIException(AIException.code_serialization_err, "Json exception", ex);
  1000. // }
  1001. // }
  1002. //
  1003. // @Data
  1004. // protected static class ConfigAware {
  1005. // private Map<String, List<ObsConfig>> obsConfigMap;
  1006. // private Map<String, List<APIConfig>> apiConfigMap;
  1007. // private int minTTL; // token最小存活时间,单位秒。所有obsconfig中ttl最小的
  1008. // }
  1009. //
  1010. // @Data
  1011. // protected static class ObsConfig {
  1012. // @JsonProperty("socket_timeout")
  1013. // private int socketTimeout = default_obs_socket_timeout; // 单位秒
  1014. // @JsonProperty("connect_timeout")
  1015. // private int connectTimeout = default_obs_connect_timeout; // 单位秒
  1016. //
  1017. // private String url;
  1018. //
  1019. // @JsonProperty("backup_url")
  1020. // private String backupUrl;
  1021. //
  1022. // private Credentials credentials;
  1023. //
  1024. // private String key;
  1025. //
  1026. // private String bucket;
  1027. //
  1028. // /**
  1029. // * token存活时间,单位秒
  1030. // */
  1031. // private int ttl;
  1032. // }
  1033. //
  1034. // @Data
  1035. // protected static class Credentials {
  1036. // @JsonProperty("access_key")
  1037. // private String accessKey;
  1038. // @JsonProperty("secret_key")
  1039. // private String secretKey;
  1040. // @JsonProperty("session_token")
  1041. // private String sessionToken;
  1042. // }
  1043. //
  1044. // @Data
  1045. // protected static class ObsClientAware {
  1046. // private ObsClient obsClient;
  1047. // private ObsConfig obsConfig;
  1048. //
  1049. // public ObsClientAware(ObsClient obsClient, ObsConfig obsConfig) {
  1050. // this.obsClient = obsClient;
  1051. // this.obsConfig = obsConfig;
  1052. // }
  1053. // }
  1054. //
  1055. // @Data
  1056. // protected static class APIConfig {
  1057. // private String url;
  1058. // @JsonProperty("backup_url")
  1059. // private String backupUrl;
  1060. // @JsonProperty("push_path")
  1061. // private String pushPath;
  1062. // @JsonProperty("status_path")
  1063. // private String statusPath;
  1064. // private RetryConfig retry;
  1065. // @JsonProperty("connect_timeout")
  1066. // private int connectTimeout = 5; // 单位秒
  1067. // @JsonProperty("socket_timeout")
  1068. // private int socketTimeout = 10; // 单位秒
  1069. // @JsonProperty("status_query")
  1070. // private StatusQuery statusQuery;
  1071. // private String type;
  1072. // }
  1073. //
  1074. // @Data
  1075. // protected static class RetryConfig {
  1076. // private String strategy;
  1077. // private int duration; // 单位毫秒
  1078. // private int times; // 重试次数
  1079. // }
  1080. //
  1081. // @Data
  1082. // protected static class StatusQuery {
  1083. // private String path;
  1084. // private String durations; // "2000,2000,2000,1000,1000,500,500" 点位毫秒
  1085. // @JsonProperty("default")
  1086. // private String defaultDuration;
  1087. //
  1088. // }
  1089. //
  1090. // @Data
  1091. // protected static class ApiAware {
  1092. // private OkHttpClient pushClient;
  1093. // private OkHttpClient statusClient;
  1094. // private APIConfig apiConfig;
  1095. // private String pushUrl;
  1096. // private String statusUrl;
  1097. // private String inferenceUrl;
  1098. //
  1099. // public ApiAware(OkHttpClient pushClient, OkHttpClient statusClient, APIConfig apiConfig, String host) {
  1100. // this.pushClient = pushClient;
  1101. // this.statusClient = statusClient;
  1102. // this.apiConfig = apiConfig;
  1103. // this.pushUrl = host + "/" + apiConfig.pushPath;
  1104. // this.statusUrl = host + "/" + apiConfig.statusPath;
  1105. // this.inferenceUrl = host + "/whee/business/push.json";
  1106. //
  1107. // }
  1108. // }
  1109. //
  1110. // /**
  1111. // * 自动刷新策略任务,如果无法获取策略将进行轮询获取,知道获取到策略为止
  1112. // */
  1113. // private static class AiClientRefreshStrategyTask implements Runnable {
  1114. // private AiClient aiClient;
  1115. //
  1116. // public AiClientRefreshStrategyTask(AiClient aiClient) {
  1117. // this.aiClient = aiClient;
  1118. // }
  1119. //
  1120. // public void run() {
  1121. // log.info("refresh strategy task start");
  1122. // Exception finalEx = null;
  1123. // for (int i = 0; i < get_strategy_retry_times; i++) {
  1124. // try {
  1125. // aiClient.init();
  1126. // return;
  1127. // } catch (Exception ex) {
  1128. // log.info("Retry getting strategy exception", ex);
  1129. // finalEx = ex;
  1130. // continue;
  1131. // }
  1132. // }
  1133. //
  1134. // if (finalEx != null) {
  1135. // log.error("Getting strategy exception", finalEx);
  1136. // }
  1137. //
  1138. // }
  1139. // }
  1140. //}