[optimization] 通义千问代码优化01
This commit is contained in:
parent
2a3714dadc
commit
fc2e12b51c
@ -1,39 +0,0 @@
|
|||||||
package com.qiuguo.iot.data.request;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class QGResponse {
|
|
||||||
@JsonProperty("status_code")
|
|
||||||
private int statusCode;
|
|
||||||
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
private Data data;
|
|
||||||
|
|
||||||
@JsonProperty("task_type")
|
|
||||||
private String taskType;
|
|
||||||
|
|
||||||
@JsonProperty("run_time")
|
|
||||||
private double runTime;
|
|
||||||
|
|
||||||
private String version;
|
|
||||||
|
|
||||||
// 添加构造方法、getter和setter方法
|
|
||||||
public static class Data {
|
|
||||||
private String resut;
|
|
||||||
|
|
||||||
private int code;
|
|
||||||
|
|
||||||
public String getResut() {
|
|
||||||
return resut;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setResut(String resut) {
|
|
||||||
this.resut = resut;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加getter和setter方法
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.qiuguo.iot.data.request.qwen;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class QGResponse<T> {
|
||||||
|
@JsonProperty("status_code")
|
||||||
|
private int statusCode;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
@JsonProperty("task_type")
|
||||||
|
private String taskType;
|
||||||
|
|
||||||
|
@JsonProperty("run_time")
|
||||||
|
private double runTime;
|
||||||
|
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.qiuguo.iot.data.request;
|
package com.qiuguo.iot.data.request.qwen;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
package com.qiuguo.iot.data.resp.qg.algorithm;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
@Data
|
||||||
|
public class QWenReplyRequest {
|
||||||
|
private String resut;
|
||||||
|
|
||||||
|
private int code;
|
||||||
|
|
||||||
|
}
|
||||||
@ -5,25 +5,29 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.qiuguo.iot.base.constans.RedisConstans;
|
import com.qiuguo.iot.base.constans.RedisConstans;
|
||||||
import com.qiuguo.iot.base.utils.WebClientUtils;
|
import com.qiuguo.iot.base.utils.WebClientUtils;
|
||||||
|
|
||||||
import com.qiuguo.iot.data.request.QGResponse;
|
import com.qiuguo.iot.data.request.qwen.QGResponse;
|
||||||
import com.qiuguo.iot.data.request.TongYiCommunicationRest;
|
import com.qiuguo.iot.data.request.qwen.TongYiCommunicationRest;
|
||||||
|
import com.qiuguo.iot.data.resp.qg.algorithm.QWenReplyRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||||
import org.springframework.data.redis.core.ReactiveValueOperations;
|
import org.springframework.data.redis.core.ReactiveValueOperations;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Service
|
||||||
public class QWenService {
|
public class QWenService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
|
private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
|
||||||
|
|
||||||
|
|
||||||
public Mono<QGResponse.Data> communication(TongYiCommunicationRest rest){
|
public Mono<QWenReplyRequest> communication(TongYiCommunicationRest rest){
|
||||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||||
return operations.get(RedisConstans.TY_QUEUE_LIST+rest.getOnlyId()).defaultIfEmpty("").flatMap(res -> {
|
return operations.get(RedisConstans.TY_QUEUE_LIST+rest.getOnlyId()).defaultIfEmpty("").flatMap(res -> {
|
||||||
JSONArray objects = JSONArray.parseArray(res);
|
JSONArray objects = JSONArray.parseArray(res);
|
||||||
@ -44,31 +48,33 @@ public class QWenService {
|
|||||||
HashMap<String, Object> map = new HashMap<>();
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
map.put("task_type", "qwen");
|
map.put("task_type", "qwen");
|
||||||
map.put("text", rest.getText());
|
map.put("text", rest.getText());
|
||||||
return httpTY(map) ;
|
return httpTY(map);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("调用异常:");
|
log.error("调用异常:");
|
||||||
return Mono.just(new QGResponse().getData());
|
return Mono.just(new QWenReplyRequest());
|
||||||
}finally {
|
}finally {
|
||||||
reactiveStringRedisTemplate.delete(RedisConstans.TY_QUEUE_LIST+rest.getOnlyId()).subscribe();
|
reactiveStringRedisTemplate.delete(RedisConstans.TY_QUEUE_LIST+rest.getOnlyId()).subscribe();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Mono<QGResponse.Data> httpTY(HashMap<String, Object> map){
|
public static Mono<QWenReplyRequest> httpTY(HashMap<String, Object> map){
|
||||||
|
|
||||||
return WebClientUtils.post("http://192.168.8.211:5010/qg_human/qwen", new JSONObject(map)).flatMap(jsonObject -> {
|
return WebClientUtils.post("http://192.168.8.211:5010/qg_human/qwen", new JSONObject(map)).flatMap(jsonObject -> {
|
||||||
long time = new Date().getTime();
|
long time = new Date().getTime();
|
||||||
QGResponse qgResponse = jsonObject.toJavaObject(QGResponse.class);
|
QGResponse qgResponse = jsonObject.toJavaObject(QGResponse.class);
|
||||||
|
QWenReplyRequest data = (QWenReplyRequest) qgResponse.getData();
|
||||||
log.info("发起TY请求,响应时间时间为:{}", new Date().getTime()-time);
|
log.info("发起TY请求,响应时间时间为:{}", new Date().getTime()-time);
|
||||||
if (qgResponse.getStatusCode() == 0) {
|
if (qgResponse.getStatusCode() == 0) {
|
||||||
//成功
|
//成功
|
||||||
log.info("通知成功:"+ qgResponse.getData());
|
log.info("通知成功:"+ qgResponse.getData());
|
||||||
return Mono.just(qgResponse.getData());
|
data.setCode(200);
|
||||||
|
return Mono.just(data);
|
||||||
}
|
}
|
||||||
//失败
|
//失败
|
||||||
log.info("通知失败:"+ map);
|
log.info("通知失败:"+ map);
|
||||||
|
data.setCode(500);
|
||||||
return Mono.just(qgResponse.getData());
|
return Mono.just(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +83,7 @@ public class QWenService {
|
|||||||
map.put("task_type", "qwen");
|
map.put("task_type", "qwen");
|
||||||
map.put("text", "你好");
|
map.put("text", "你好");
|
||||||
try {
|
try {
|
||||||
Mono<QGResponse.Data> qgResponseMono = httpTY(map);
|
Mono<QWenReplyRequest> qgResponseMono = httpTY(map);
|
||||||
System.out.println(qgResponseMono.block());
|
System.out.println(qgResponseMono.block());
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
log.info("异常:{}", e);
|
log.info("异常:{}", e);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.qiuguo.iot.third.service;
|
package com.qiuguo.iot.third.service;
|
||||||
|
|
||||||
|
import com.qiuguo.iot.data.request.qwen.TongYiCommunicationRest;
|
||||||
import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp;
|
import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp;
|
||||||
import com.qiuguo.iot.data.resp.third.ThirdRpcResp;
|
import com.qiuguo.iot.data.resp.third.ThirdRpcResp;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -17,11 +18,23 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
* @description
|
* @description
|
||||||
**/
|
**/
|
||||||
|
|
||||||
@SpringBootTest(classes = IpService.class)
|
@SpringBootTest(classes = {IpService.class,QWenService.class})
|
||||||
class IpServiceTest {
|
class IpServiceTest {
|
||||||
@Resource
|
@Resource
|
||||||
private IpService ipService;
|
private IpService ipService;
|
||||||
|
@Resource
|
||||||
|
private QWenService qWenService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aa() {
|
||||||
|
TongYiCommunicationRest rest = new TongYiCommunicationRest();
|
||||||
|
rest.setIsWhiteList(0);
|
||||||
|
rest.setText("你好");
|
||||||
|
rest.setOnlyId("123");
|
||||||
|
rest.setStatus("0");
|
||||||
|
|
||||||
|
qWenService.communication(rest);
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() throws InterruptedException {
|
void contextLoads() throws InterruptedException {
|
||||||
Mono<ThirdRpcResp<ThirdIpInfoResp>> ipInfo = ipService.getIpInfo("60.186.105.204");
|
Mono<ThirdRpcResp<ThirdIpInfoResp>> ipInfo = ipService.getIpInfo("60.186.105.204");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user