Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-ai
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-ai
Commits
4fdc510f
Commit
4fdc510f
authored
Apr 23, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
18044d65
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
10 deletions
+17
-10
yd-ai-api/src/main/java/com/yd/ai/api/service/impl/ApiAiStreamServiceImpl.java
+17
-10
No files found.
yd-ai-api/src/main/java/com/yd/ai/api/service/impl/ApiAiStreamServiceImpl.java
View file @
4fdc510f
...
...
@@ -92,27 +92,34 @@ public class ApiAiStreamServiceImpl implements ApiAiStreamService {
Publisher
<
GenerationResult
>
publisher
=
null
;
try
{
publisher
=
gen
.
streamCall
(
param
);
}
catch
(
NoApiKeyException
e
)
{
e
.
printStackTrace
(
);
}
catch
(
InputRequiredException
e
)
{
e
.
printStackTrace
()
;
}
catch
(
NoApiKeyException
|
InputRequiredException
e
)
{
log
.
error
(
"流式调用初始化失败"
,
e
);
sink
.
error
(
e
);
return
;
}
Flowable
<
GenerationResult
>
flowable
=
Flowable
.
fromPublisher
(
publisher
)
.
subscribeOn
(
Schedulers
.
io
());
// 使用 RxJava3 的 Schedulers.io()
.
subscribeOn
(
Schedulers
.
io
());
// 用于累计完整文本的 StringBuilder
StringBuilder
fullContent
=
new
StringBuilder
();
flowable
.
subscribe
(
result
->
{
String
delta
=
result
.
getOutput
().
getChoices
().
get
(
0
).
getMessage
().
getContent
();
//
实时着色并打印
printWithStyle
(
delta
);
System
.
out
.
flush
();
sink
.
next
(
delta
);
//
拼接到累计文本中
fullContent
.
append
(
delta
);
// 将当前完整内容发送给前端
sink
.
next
(
fullContent
.
toString
()
);
},
error
->
{
log
.
error
(
"流式调用出错"
,
error
);
sink
.
error
(
error
);
},
sink:
:
complete
()
->
{
log
.
info
(
"流式输出完成,总长度: {}"
,
fullContent
.
length
());
sink
.
complete
();
}
);
});
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment