forked from XiaoMi/mone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mesh streaming response support (XiaoMi#894)
- Loading branch information
Showing
13 changed files
with
388 additions
and
49 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
jcommon/api/src/main/java/com/xiaomi/data/push/uds/processor/StreamCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.xiaomi.data.push.uds.processor; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/11/7 11:56 | ||
*/ | ||
public interface StreamCallback { | ||
|
||
void onContent(String content); | ||
|
||
void onComplete(); | ||
|
||
void onError(Throwable error); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
import com.xiaomi.data.push.uds.context.TraceContext; | ||
import com.xiaomi.data.push.uds.context.TraceEvent; | ||
import com.xiaomi.data.push.uds.context.UdsClientContext; | ||
import com.xiaomi.data.push.uds.handler.MessageTypes; | ||
import com.xiaomi.data.push.uds.handler.ClientStreamCallback; | ||
import com.xiaomi.data.push.uds.handler.UdsClientConnetManageHandler; | ||
import com.xiaomi.data.push.uds.handler.UdsClientHandler; | ||
import com.xiaomi.data.push.uds.po.UdsCommand; | ||
|
@@ -41,7 +43,6 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -134,6 +135,20 @@ public void call(Object msg) { | |
Send.send(this.channel, command); | ||
} | ||
|
||
|
||
/** | ||
* 发送OpenAI流式请求 | ||
*/ | ||
public void stream(UdsCommand command, ClientStreamCallback callback) { | ||
Map<String, String> attachments = command.getAttachments(); | ||
// 注册回调 | ||
((UdsClientHandler) channel.pipeline().last()).getStreamCallbacks() | ||
.put(attachments.get(MessageTypes.STREAM_ID_KEY), callback); | ||
// 发送请求 | ||
Send.send(this.channel, command); | ||
} | ||
|
||
|
||
@Override | ||
public UdsCommand call(UdsCommand req) { | ||
Stopwatch sw = Stopwatch.createStarted(); | ||
|
@@ -142,11 +157,11 @@ public UdsCommand call(UdsCommand req) { | |
long id = req.getId(); | ||
try { | ||
CompletableFuture<Object> future = new CompletableFuture<>(); | ||
HashMap<String,Object> hashMap = new HashMap<>(); | ||
HashMap<String, Object> hashMap = new HashMap<>(); | ||
hashMap.put("future", future); | ||
hashMap.put("async", req.isAsync()); | ||
hashMap.put("returnType", req.getReturnClass()); | ||
reqMap.put(req.getId(),hashMap); | ||
reqMap.put(req.getId(), hashMap); | ||
Channel channel = this.channel; | ||
if (null == channel || !channel.isOpen()) { | ||
log.warn("client channel is close"); | ||
|
@@ -160,7 +175,7 @@ public UdsCommand call(UdsCommand req) { | |
wheelTimer.newTimeout(() -> { | ||
log.warn("check async udsClient time out auto close:{},{}", req.getId(), req.getTimeout()); | ||
reqMap.remove(req.getId()); | ||
}, req.getTimeout()+350); | ||
}, req.getTimeout() + 350); | ||
return req; | ||
} | ||
return (UdsCommand) future.get(req.getTimeout(), TimeUnit.MILLISECONDS); | ||
|
15 changes: 15 additions & 0 deletions
15
jcommon/rcurve/src/main/java/com/xiaomi/data/push/uds/handler/ClientStreamCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.xiaomi.data.push.uds.handler; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/11/7 10:35 | ||
*/ | ||
public interface ClientStreamCallback { | ||
|
||
void onContent(String content); | ||
|
||
void onComplete(); | ||
|
||
void onError(Throwable error); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
jcommon/rcurve/src/main/java/com/xiaomi/data/push/uds/handler/MessageTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.xiaomi.data.push.uds.handler; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/11/6 17:41 | ||
*/ | ||
public class MessageTypes { | ||
|
||
public static final String TYPE_KEY = "messageType"; | ||
public static final String TYPE_NORMAL = "normal"; | ||
public static final String TYPE_OPENAI = "openai"; | ||
public static final String STREAM_ID_KEY = "streamId"; | ||
public static final String PROMPT_KEY = "prompt"; | ||
public static final String CONTENT_KEY = "content"; | ||
public static final String STATUS_KEY = "status"; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.