Commit d48fa846 authored by 赵啸非's avatar 赵啸非

修改配置文件

parent 1366a0ee
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
<![CDATA[jdbc:mysql://127.0.0.1:3306/robot-trans-platform?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong]]></profiles.datasource.uri> <![CDATA[jdbc:mysql://127.0.0.1:3306/robot-trans-platform?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong]]></profiles.datasource.uri>
<profiles.datasource.username>root</profiles.datasource.username> <profiles.datasource.username>root</profiles.datasource.username>
<profiles.datasource.password>xhx@2022</profiles.datasource.password> <profiles.datasource.password>xhx@2022</profiles.datasource.password>
<profiles.streamChatUrl>http://chat.scsmile.cn</profiles.streamChatUrl> <profiles.streamChatUrl>https://agent.wx3.com.cn/v1/chat-messages</profiles.streamChatUrl>
</properties> </properties>
</profile> </profile>
</profiles> </profiles>
......
...@@ -45,12 +45,8 @@ public class MessageServiceImpl implements MessageService { ...@@ -45,12 +45,8 @@ public class MessageServiceImpl implements MessageService {
@Override @Override
public Flux<String> sendMessageToFastApi(SseEmitter sseEmitter, RobotTransReq robotTransReq) { public Flux<String> sendMessageToFastApi(SseEmitter sseEmitter, RobotTransReq robotTransReq) {
// 创建 WebClient 客户端 // 创建 WebClient 客户端
WebClient webClient = WebClient.builder().baseUrl(streamChatUrl).build(); WebClient webClient = WebClient.builder().baseUrl(streamChatUrl).build();
// 封装参数 // 封装参数
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
params.put("chatId", "my_chatId"); params.put("chatId", "my_chatId");
......
...@@ -82,36 +82,6 @@ public class FlowChatSubscriber implements Subscriber<String>, Disposable { ...@@ -82,36 +82,6 @@ public class FlowChatSubscriber implements Subscriber<String>, Disposable {
subscription.request(1); subscription.request(1);
} }
} }
/* new Thread(() -> { // 需要异步执行,否则会阻塞
String rsp = "";
try {
rsp = callBack.onNext(data, context);
if (ObjectUtils.isEmpty(rsp)) {
this.stopFlag = true;
}
log.info(" onNext rsp:{}", rsp);
} catch (Exception e) {
log.error("流式问答异常:{}", e.getMessage());
} finally {
// todo 临时打印日志
// log.info("=============== data: {}", data);
if (stopFlag) {
log.info("stopFlag is true");
subscription.cancel();
onComplete();
} else {
// emitter.(msg);
// 将数据发送给前端 如果是起始包 需要要处理一下
emitter.next(rsp);
// 继续请求接收下一个数据项
subscription.request(1);
}
}
}).start();*/
} }
@Override @Override
......
...@@ -17,6 +17,10 @@ public class RobotTransReq { ...@@ -17,6 +17,10 @@ public class RobotTransReq {
private String enterpriseId; private String enterpriseId;
@JSONField(name = "device_id")
@JsonProperty("device_id")
private String deviceId;
@JSONField(name = "max_tokens") @JSONField(name = "max_tokens")
@JsonProperty("max_tokens") @JsonProperty("max_tokens")
private Integer maxTokens; //最大2048 private Integer maxTokens; //最大2048
......
...@@ -15,6 +15,8 @@ import com.mortals.xhx.webflux.template.AbstractFlowChatTemplate; ...@@ -15,6 +15,8 @@ import com.mortals.xhx.webflux.template.AbstractFlowChatTemplate;
import com.mortals.xhx.webflux.factory.FLowChatRequest; import com.mortals.xhx.webflux.factory.FLowChatRequest;
import com.mortals.xhx.webflux.factory.FlowChatContext; import com.mortals.xhx.webflux.factory.FlowChatContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -52,6 +54,12 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -52,6 +54,12 @@ public class BaseChatService extends AbstractFlowChatTemplate {
@Override @Override
public String onNext(String data, FlowChatContext context) { public String onNext(String data, FlowChatContext context) {
//return parseFastApiResp(data, context);
return parseDifyApiResp(data, context);
}
@Nullable
private String parseFastApiResp(String data, FlowChatContext context) {
if (StrUtil.equals("[DONE]", data)) {//[DONE]是消息结束标识 if (StrUtil.equals("[DONE]", data)) {//[DONE]是消息结束标识
return null; return null;
} }
...@@ -60,63 +68,6 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -60,63 +68,6 @@ public class BaseChatService extends AbstractFlowChatTemplate {
if (bodyJson == null) { if (bodyJson == null) {
return data; return data;
} }
/*if(ObjectUtils.isEmpty(context.getAnswer())){
//创建一个初始化的answer
JSONObject rspJson = new JSONObject();
rspJson.put("id", context.getMsgId());
rspJson.put("created", new Date().getTime());
rspJson.put("model", "");
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
JSONObject delta = new JSONObject();
delta.put("role", "assistant");
delta.put("content", "");
obj.put("delta", delta);
obj.put("index", 0);
obj.put("finish_reason", "null");
jsonArray.add(obj);
rspJson.put("choices", jsonArray);
rspJson.put("object", "");
context.getAnswer().append(".");
context.getEmitter().next(rspJson.toJSONString());
}
*/
// context.getAnswer().append(answer);
// String id = bodyJson.getString("id");
/* if (num == 0) {
JSONObject rspJson = new JSONObject();
rspJson.put("id", context.getMsgId());
rspJson.put("created", new Date().getTime());
rspJson.put("model", "");
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
JSONObject delta = new JSONObject();
delta.put("role", "assistant");
delta.put("content", "");
obj.put("delta", delta);
obj.put("index", 0);
obj.put("finish_reason", "null");
jsonArray.add(obj);
rspJson.put("choices", jsonArray);
rspJson.put("object", "");
num++;
context.getEmitter().next(rspJson.toJSONString());
}*/
JSONArray choices = bodyJson.getJSONArray("choices"); JSONArray choices = bodyJson.getJSONArray("choices");
bodyJson.put("id", context.getMsgId()); bodyJson.put("id", context.getMsgId());
bodyJson.put("created", new Date().getTime()); bodyJson.put("created", new Date().getTime());
...@@ -135,13 +86,92 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -135,13 +86,92 @@ public class BaseChatService extends AbstractFlowChatTemplate {
if ("stop".equals(finish_reason)) { if ("stop".equals(finish_reason)) {
delta.put("content", ""); delta.put("content", "");
this.num = 0; this.num = 0;
}else{ } else {
jsonObject.put("finish_reason", null); jsonObject.put("finish_reason", null);
} }
} }
return bodyJson.toJSONString(bodyJson,SerializerFeature.WriteMapNullValue); return bodyJson.toJSONString(bodyJson, SerializerFeature.WriteMapNullValue);
}
@Nullable
private String parseDifyApiResp(String data, FlowChatContext context) {
if (StrUtil.equals("[DONE]", data)) {//[DONE]是消息结束标识
return null;
}
// 解析响应数据项
JSONObject bodyJson = JSONObject.parseObject(data);
if (bodyJson == null) {
return data;
}
String event = bodyJson.getString("event");
JSONObject rspObj = new JSONObject();
if ("message".equalsIgnoreCase(event)) {
rspObj.put("model", "dify");
rspObj.put("object", "");
rspObj.put("id", context.getMsgId());
rspObj.put("created", new Date().getTime());
JSONArray choices = new JSONArray();
JSONObject object = new JSONObject();
object.put("finish_reason", null);
object.put("index", 0);
JSONObject delta = new JSONObject();
delta.put("role", "");
delta.put("content", bodyJson.getString("answer"));
rspObj.put("delta", delta);
choices.add(object);
rspObj.put("choices", choices);
return rspObj.toJSONString(rspObj, SerializerFeature.WriteMapNullValue);
}
if ("message_end".equalsIgnoreCase(event)) {
rspObj.put("model", "dify");
rspObj.put("object", "");
rspObj.put("id", context.getMsgId());
rspObj.put("created", new Date().getTime());
JSONArray choices = new JSONArray();
JSONObject object = new JSONObject();
object.put("finish_reason", "stop");
object.put("index", 0);
JSONObject delta = new JSONObject();
delta.put("role", "");
delta.put("content", bodyJson.getString("answer"));
rspObj.put("delta", delta);
choices.add(object);
rspObj.put("choices", choices);
this.num = 0;
return rspObj.toJSONString(rspObj, SerializerFeature.WriteMapNullValue);
}
//{
// "created": 1742891327432,
// "model": "",
// "id": "683a65fd-8feb-4446-ad32-714c4785f667",
// "choices": [
// {
// "finish_reason": "stop",
// "delta": {
// "role": "",
// "content": ""
// },
// "index": 0
// }
// ],
// "object": ""
//}
return bodyJson.toJSONString(bodyJson, SerializerFeature.WriteMapNullValue);
} }
@Override @Override
...@@ -160,6 +190,37 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -160,6 +190,37 @@ public class BaseChatService extends AbstractFlowChatTemplate {
protected FLowChatRequest buildRequest(FlowChatContext context) { protected FLowChatRequest buildRequest(FlowChatContext context) {
//fastgptapi //fastgptapi
// 封装参数 // 封装参数
// JSONObject params = buildFastApiJsonObject(context);
//dify
JSONObject params = buildDifyApiJsonObject(context);
log.info("请求参数:{}", params.toJSONString());
return FLowChatRequest.builder().url(streamChatUrl).jsonBody(params.toJSONString()).build();
}
private JSONObject buildDifyApiJsonObject(FlowChatContext context) {
JSONObject params = new JSONObject();
// 构建请求参数
RobotTransReq robotTransReq = context.getRequestParam();
//messageId
String id = robotTransReq.getId();
context.setMsgId(id);
Boolean stream = robotTransReq.getStream();
params.put("inputs", new JSONObject());
params.put("response_mode", "streaming");
params.put("conversation_id", robotTransReq.getId());
params.put("user", robotTransReq.getDeviceId());
List<MessagesItem> messages = robotTransReq.getMessages();
for (MessagesItem message : messages) {
params.put("query", message.getContent());
}
return params;
}
@NotNull
private JSONObject buildFastApiJsonObject(FlowChatContext context) {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
...@@ -188,8 +249,7 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -188,8 +249,7 @@ public class BaseChatService extends AbstractFlowChatTemplate {
variables.put("uid", "asdfadsfasfd2323"); variables.put("uid", "asdfadsfasfd2323");
variables.put("name", "robot"); variables.put("name", "robot");
params.put("variables", variables); params.put("variables", variables);
return params;
return FLowChatRequest.builder().url(streamChatUrl).jsonBody(params.toJSONString()).build();
} }
@Override @Override
......
...@@ -46,10 +46,8 @@ public abstract class AbstractFlowChatTemplate implements IFlowChat, FlowChatCal ...@@ -46,10 +46,8 @@ public abstract class AbstractFlowChatTemplate implements IFlowChat, FlowChatCal
*/ */
@Override @Override
public Flux<String> request(FlowChatContext context) { public Flux<String> request(FlowChatContext context) {
// 请求大模型问答之前的逻辑处理 // 请求大模型问答之前的逻辑处理
doPreRequest(context); doPreRequest(context);
// 请求大模型、处理回调逻辑 // 请求大模型、处理回调逻辑
return Flux.create(emitter -> { return Flux.create(emitter -> {
Flux<String> response = this.doRequest(context, buildRequest(context), buildRequestHeaders(context)); Flux<String> response = this.doRequest(context, buildRequest(context), buildRequestHeaders(context));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment