Commit 3d254032 authored by 赵啸非's avatar 赵啸非

添加对接dify

parent c34a0995
......@@ -48,7 +48,7 @@
<profiles.datasource.password>12345678</profiles.datasource.password>
<profiles.publish.path>E:\pic\publish\</profiles.publish.path>
<profiles.filepath>E:/mortals/app/data</profiles.filepath>
<profiles.chat.streamChatUrl>https://agent.wx3.com.cn:443/v1/chat-messages</profiles.chat.streamChatUrl>
<profiles.chat.streamChatUrl>https://agent.wx3.com.cn/v1/chat-messages</profiles.chat.streamChatUrl>
<profiles.chat.auth-head>Authorization</profiles.chat.auth-head>
<profiles.chat.auth-value>Bearer app-ritPdgkz8bsrfzpcJdSTkUF5</profiles.chat.auth-value>
</properties>
......
......@@ -207,8 +207,9 @@ public class BaseChatService extends AbstractFlowChatTemplate {
Boolean stream = robotTransReq.getStream();
params.put("inputs", new JSONObject());
params.put("response_mode", "streaming");
params.put("conversation_id", robotTransReq.getId());
params.put("conversation_id", "");
params.put("user", robotTransReq.getDeviceId());
params.put("files", new JSONArray());
List<MessagesItem> messages = robotTransReq.getMessages();
for (MessagesItem message : messages) {
......
......@@ -41,8 +41,6 @@ public class ChatMsgServiceImpl implements IChatMsgService {
.request(new FlowChatContext(param));
}
@Override
public void stopChat(SessionBaseParam param) {
chatFactory.getInstance(FlowChatSceneEnum.BASE_CHAT)
......
......@@ -37,7 +37,6 @@ public abstract class AbstractFlowChatTemplate implements IFlowChat, FlowChatCal
private void init() {
webClient = WebClient
.builder()
.baseUrl("https://agent.wx3.com.cn")
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json").build();
}
......@@ -111,12 +110,14 @@ public abstract class AbstractFlowChatTemplate implements IFlowChat, FlowChatCal
*/
private Flux<String> doRequest(FlowChatContext context, FLowChatRequest request, HttpHeaders headers) {
log.info("请求大模型开始,URL:{}, 参数:{}", request.getUrl(), request.getJsonBody());
Flux<String> flux = webClient
.post()
.uri("/v1/chat-messages")
.uri(request.getUrl())
.accept(MediaType.TEXT_EVENT_STREAM)
.headers(httpHeaders -> httpHeaders.addAll(headers))
.bodyValue(request.getJsonBody())
//.bodyValue(bodyStr)
.retrieve()
.bodyToFlux(String.class)
.onErrorResume(WebClientResponseException.class, ex -> {
......
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okio.BufferedSource;
......@@ -12,13 +14,15 @@ import java.util.concurrent.TimeUnit;
public class TestRot {
public static void testRobotRwq() {
public static void testRobotRwq() throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.followRedirects(true) // 允许自动重定向
.followSslRedirects(true)
.build();
......@@ -26,36 +30,39 @@ public class TestRot {
params.put("question", question);*/
String requestStr = "{\n" +
" \"id\": \"683a65fd-8feb-4446-ad32-714c4785f667\",\n" +
" \"enterprise_id\": \"orion.ovs.entprise.6687895631\",\n" +
" \"device_id\": \"M03SCN00030230362FFA\",\n" +
" \"messages\": [\n" +
" {\n" +
" \"role\": \"user\",\n" +
" \"content\": \"你有什么能力?\"\n" +
" }\n" +
" ],\n" +
" \"max_tokens\": 2048,\n" +
" \"stream\": true\n" +
" \"inputs\": {},\n" +
" \"query\": \"周杰伦是谁?\",\n" +
" \"response_mode\": \"streaming\",\n" +
" \"conversation_id\": \"\",\n" +
" \"user\": \"abc-123\",\n" +
" \"files\": []\n" +
"}";
RequestBody requestBody = RequestBody.create(requestStr, MediaType.parse("application/json; charset=utf-8"));
// 封装请求头
Headers headers = new Headers.Builder()
.set("Content-Type", "application/json")
.set("Accept", "text/event-stream")
.set("Accept", "*/*")
.set("Authorization", "Bearer app-ritPdgkz8bsrfzpcJdSTkUF5")
.build();
Request request = new Request.Builder()
.url("http://localhost:18006/chat/base")
//.url("http://robot.scsmile.cn/chat/base")
//.url("http://localhost:18006/chat/base")
// .url("http://robot.scsmile.cn/v1/chat-messages")
.url("https://agent.wx3.com.cn/v1/chat-messages")
//.url(".url("https://agent.wx3.com.cn/v1/chat-messages")")
.headers(headers)
.post(requestBody)
.build();
/* Response response = client.newCall(request).execute();
log.info(response.code() + "");*/
Call call = client.newCall(request);
// 4. 监听回调
call.enqueue(new Callback() {
......@@ -67,6 +74,7 @@ public class TestRot {
@Override
public void onResponse(Call call, Response response) throws IOException {
log.info(JSON.toJSONString(response));
if (response.isSuccessful()) {
String chunkMessage = "";
BufferedSource source = response.body().source();
......@@ -75,12 +83,14 @@ public class TestRot {
if (StringUtils.isBlank(chunkMessage)) {
continue;
}
/* JSONObject jsonObject = JSONObject.parseObject(chunkMessage);
/* JSONObject jsonObject = JSONObject.parseObject(chunkMessage);
if (null != jsonObject && null != jsonObject.getJSONObject("data")) {
String answer = jsonObject.getJSONObject("data").getString("answer");
// sseEmitter.send(answer);
}*/
log.info("chunkMessage:{}", chunkMessage);
}
......@@ -92,11 +102,10 @@ public class TestRot {
}
});
}
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
testRobotRwq();
......
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okio.BufferedSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class TestRotWebClient {
public static String decodeUnicode(String unicodeStr) {
Pattern pattern = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
Matcher matcher = pattern.matcher(unicodeStr);
StringBuffer decoded = new StringBuffer();
while (matcher.find()) {
int charCode = Integer.parseInt(matcher.group(1), 16);
matcher.appendReplacement(decoded, Character.toString((char) charCode));
}
matcher.appendTail(decoded);
return decoded.toString();
}
public static void testRobotRwq() throws IOException {
WebClient client = WebClient.create("https://agent.wx3.com.cn");
String bodyStr="{\n" +
" \"inputs\": {},\n" +
" \"query\": \"周杰伦是谁?\",\n" +
" \"response_mode\": \"streaming\",\n" +
" \"conversation_id\": \"\",\n" +
" \"user\": \"abc-123\",\n" +
" \"files\": []\n" +
"}";
Flux<String> eventStream = client.post()
.uri("/v1/chat-messages") // 远程 SSE API
.header("Content-Type", "application/json")
.header("Accept", "*/*")
.header("Accept-Charset", "UTF-8")
.header("Authorization", "Bearer app-ritPdgkz8bsrfzpcJdSTkUF5")
.bodyValue(bodyStr) // 发送 JSON 订阅请求
.retrieve()
.bodyToFlux(String.class); // 处理流数据
// 订阅并打印收到的事件
eventStream.subscribe(event -> System.out.println("Received event: " + decodeUnicode(event)),
error -> System.err.println("Error: " + error),
() -> System.out.println("Stream completed"));
// 保持主线程运行,否则程序可能会立即退出
while (true) {}
}
public static void main(String[] args) throws IOException {
testRobotRwq();
}
}
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