星尘对接完成

This commit is contained in:
wulin 2023-11-16 21:00:43 +08:00
parent a1636ea9dd
commit b370b13fe0
9 changed files with 197 additions and 12 deletions

View File

@ -6,7 +6,7 @@ package com.qiuguo.iot.base.enums;
* */
// 0响铃一次time指定的时间 1每天 2指定星期
public enum DeviceCodeEnum {
BOX(0, "qgbox"),
BOX(0, "28ff210c6e664903a5076b027f23b30b"),
;
DeviceCodeEnum(Integer c, String n){
code = c;

View File

@ -88,7 +88,7 @@ public class DeviceUserBindEntity extends GenericEntity<Long> {
@Comment("乡镇以下地址")
@Column(name = "address", length = 200)
private String address;
@Comment("分类字段名称")
@Comment("分类字段名称。当为果Box代表通义星尘角色id")
@Column(name = "category_code", length = 20, nullable = false)
private String categoryCode;

View File

@ -7,6 +7,14 @@ public class TongYiCommunicationRest {
private Integer isWhiteList;//是否为白名单
private String text;//消息
private String onlyId;//唯一编码
/**
* 星尘角色id
*/
private String roleId;
/**
* 用户昵称
*/
private String nickName;
private String status; //状态,0:已完成,1.未发起,2.发起中
/**

View File

@ -87,6 +87,14 @@
<version>2.5.1</version>
</dependency>
<!-- 阿里星辰大模型-->
<dependency>
<groupId>com.alibaba.xingchen</groupId>
<artifactId>xingchen-java-client</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,112 @@
package com.qiuguo.iot.third.nlp;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Role;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.api.ChatApiSub;
import com.alibaba.xingchen.auth.HttpBearerAuth;
import com.alibaba.xingchen.model.*;
import com.qiuguo.iot.data.resp.qg.algorithm.QWenReplyResponse;
import com.qiuguo.iot.third.service.IQianWen;
import io.reactivex.Flowable;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
@Data
@Slf4j
public class AliYunXingChen {
@Data
class RecordMessage{
String msg = "";
}
/**
*
*/
ChatApiSub api;
List<Message> messages = new ArrayList<>(50);
ChatReqParams chatReqParams;
String userName;
public AliYunXingChen(String basePath, String key, String roleId, String userId, String userName) {
api = new ChatApiSub();
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(basePath);
// 开启SSE输出
apiClient.addDefaultHeader("X-DashScope-SSE", "enable");
// Configure HTTP bearer authorization: Authorization
HttpBearerAuth authorization = (HttpBearerAuth) apiClient.getAuthentication("Authorization");
authorization.setBearerToken(key);
api.setApiClient(apiClient);
this.userName = userName;
chatReqParams = ChatReqParams.builder()
.botProfile(
CharacterKey.builder()
// 星尘预制角色
.characterId(roleId)
.version(1)
.build()
)
.modelParameters(
ModelParameters.builder()
.seed(1683806810L)
.topP(0.8)
.temperature(0.8)
.build()
)
.userProfile(
UserProfile.builder()
.userId(userId)
.build()
).build();
}
public Mono<Boolean> sendMessage(String msg,
IQianWen<? super String> onNext){
Message message = Message.builder().name(userName).role("user").content(msg).build();
addMessage(message);
chatReqParams.setMessages(messages);
return Mono.defer(() -> {
try {
Flowable<ChatResult> response = api.streamOut(chatReqParams);
RecordMessage recordMessage = new RecordMessage();
response.forEach(m -> {
String v = m.getChoices().get(0).getMessages().get(0).getContent().replaceAll(recordMessage.getMsg(), "");
onNext.sendMessage(v);
if("stop".equals(m.getChoices().get(0).getStopReason())){
//
Message message1 = Message.builder()
.role(m.getChoices().get(0).getMessages().get(0).getRole())
.content(m.getChoices().get(0).getMessages().get(0).getContent())
.build();
addMessage(message1);
onNext.finish();
}
});
}catch (Exception e){
log.info("调用星尘异常{}", e);
}
return Mono.just(true);
});
}
private void addMessage(Message msg){
if(messages.size() >= 50){
messages.remove(0);
}
messages.add(msg);
}
}

View File

@ -0,0 +1,57 @@
package com.qiuguo.iot.third.service;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.base.constans.RedisConstans;
import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.base.utils.WebClientUtils;
import com.qiuguo.iot.data.dto.queue.QueuePackagingDTO;
import com.qiuguo.iot.data.request.qwen.TongYiCommunicationRest;
import com.qiuguo.iot.data.resp.qg.algorithm.QGResponse;
import com.qiuguo.iot.data.resp.qg.algorithm.QWenReplyResponse;
import com.qiuguo.iot.third.nlp.AliYunQianWen;
import com.qiuguo.iot.third.nlp.AliYunXingChen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.ReactiveValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Slf4j
@Service
public class TongYiXinChenService {
protected static ConcurrentHashMap<String, AliYunXingChen> qianwenGroup = new ConcurrentHashMap<>();
public Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext){
AliYunXingChen aliXingChen = null;
if (!qianwenGroup.containsKey(rest.getOnlyId())) {
aliXingChen = new AliYunXingChen(SpringUtil.getProperty("Ali.xingchen.basepath"),
SpringUtil.getProperty("Ali.xingchen.key"),
rest.getRoleId(), rest.getOnlyId(), rest.getNickName());
qianwenGroup.put(rest.getOnlyId(), aliXingChen);
} else {
aliXingChen = qianwenGroup.get(rest.getOnlyId());
}
QWenReplyResponse qWenReplyResponse = new QWenReplyResponse();
return aliXingChen.sendMessage(rest.getText(), onNext).flatMap(b -> {
return Mono.just(qWenReplyResponse);
});
}
}

View File

@ -106,13 +106,7 @@
<artifactId>tea-openapi</artifactId>
<version>0.2.5</version>
</dependency>
<!-- 阿里星辰大模型-->
<dependency>
<groupId>com.alibaba.xingchen</groupId>
<artifactId>xingchen-java-client</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -124,11 +124,14 @@ public abstract class ActionCommand {
tongYiCommunicationRest.setText(action.getAsk());
tongYiCommunicationRest.setStatus("2");
tongYiCommunicationRest.setRequestId(baseSession.getRequestId());
if(baseSession instanceof BoxSession){
//调用星尘
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
//下面是调用千问
/*if(baseSession instanceof BoxSession){
tongYiCommunicationRest.setOnlyId(baseSession.getSn());
}else{
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
}
}*/
QueueMessage queueMessage = new QueueMessage();

View File

@ -58,4 +58,7 @@ lac:
Ali:
qianwen: 'sk-8d64677afaf6404cb83ce1910b5b2558'
accesskeId: 'LTAI5t7d1iZb18SvGQhtDnyN'
accesskeySecret: 'j2Cp3uCDGuiA7xZIJmYOCmDhJl9HuJ'
accesskeySecret: 'j2Cp3uCDGuiA7xZIJmYOCmDhJl9HuJ'
xingchen:
key: 'lm-dIINta/ISx0/8aZK5xKt/A=='
basepath: https://nlp.aliyuncs.com