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
048c02c8
Commit
048c02c8
authored
Apr 01, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
3c484189
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
12 deletions
+50
-12
yd-ai-api/src/main/java/com/yd/ai/api/controller/ApiAiStreamController.java
+50
-12
No files found.
yd-ai-api/src/main/java/com/yd/ai/api/controller/ApiAiStreamController.java
View file @
048c02c8
...
...
@@ -8,6 +8,7 @@ import com.alibaba.dashscope.common.Role;
import
com.alibaba.dashscope.exception.InputRequiredException
;
import
com.alibaba.dashscope.exception.NoApiKeyException
;
import
io.reactivex.Flowable
;
import
lombok.var
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.codec.ServerSentEvent
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -21,9 +22,19 @@ public class ApiAiStreamController {
@CrossOrigin
(
origins
=
"*"
)
// 开发时允许跨域
@GetMapping
(
value
=
"/stream"
,
produces
=
MediaType
.
TEXT_EVENT_STREAM_VALUE
)
public
Flux
<
ServerSentEvent
<
String
>>
streamChat
(
@RequestParam
String
question
)
{
Generation
gen
=
new
Generation
();
Message
systemMsg
=
Message
.
builder
().
role
(
Role
.
SYSTEM
.
getValue
()).
content
(
"You are a helpful assistant."
).
build
();
Message
userMsg
=
Message
.
builder
().
role
(
Role
.
USER
.
getValue
()).
content
(
question
).
build
();
if
(
question
==
null
||
question
.
trim
().
isEmpty
())
{
return
Flux
.
error
(
new
IllegalArgumentException
(
"question cannot be empty"
));
}
Message
systemMsg
=
Message
.
builder
()
.
role
(
Role
.
SYSTEM
.
getValue
())
.
content
(
"You are a helpful assistant."
)
.
build
();
Message
userMsg
=
Message
.
builder
()
.
role
(
Role
.
USER
.
getValue
())
.
content
(
question
)
.
build
();
GenerationParam
param
=
GenerationParam
.
builder
()
.
apiKey
(
"sk-d6551c67cfbe4a759a78dc3625729291"
)
.
model
(
"qwen-plus"
)
...
...
@@ -32,19 +43,45 @@ public class ApiAiStreamController {
.
incrementalOutput
(
true
)
.
build
();
Flowable
<
GenerationResult
>
flowable
=
null
;
Generation
gen
=
new
Generation
();
// 使用 defer 延迟执行,避免 flowable 为 null
return
Flux
.
from
(
Flowable
.
defer
(()
->
{
try
{
flowable
=
gen
.
streamCall
(
param
);
return
gen
.
streamCall
(
param
);
}
catch
(
NoApiKeyException
e
)
{
e
.
printStackTrace
(
);
return
Flowable
.
error
(
new
RuntimeException
(
"API Key missing"
,
e
)
);
}
catch
(
InputRequiredException
e
)
{
e
.
printStackTrace
();
return
Flowable
.
error
(
new
RuntimeException
(
"Invalid input"
,
e
));
}
catch
(
Exception
e
)
{
return
Flowable
.
error
(
new
RuntimeException
(
"DashScope call failed"
,
e
));
}
// 将 RxJava Flowable 转为 Reactor Flux
return
Flux
.
from
(
flowable
)
}))
.
map
(
result
->
{
String
delta
=
result
.
getOutput
().
getChoices
().
get
(
0
).
getMessage
().
getContent
();
return
ServerSentEvent
.
builder
(
delta
).
build
();
});
String
delta
=
""
;
if
(
result
.
getOutput
()
!=
null
&&
!
result
.
getOutput
().
getChoices
().
isEmpty
())
{
var
choice
=
result
.
getOutput
().
getChoices
().
get
(
0
);
if
(
choice
.
getMessage
()
!=
null
)
{
delta
=
choice
.
getMessage
().
getContent
();
}
}
// 发送标准 SSE 事件,包含 event 字段便于前端区分
return
ServerSentEvent
.<
String
>
builder
()
.
event
(
"message"
)
.
data
(
delta
)
.
build
();
})
.
concatWith
(
Flux
.
just
(
ServerSentEvent
.<
String
>
builder
()
.
event
(
"complete"
)
.
data
(
"[DONE]"
)
.
build
()
))
.
onErrorResume
(
error
->
Flux
.
just
(
ServerSentEvent
.<
String
>
builder
()
.
event
(
"error"
)
.
data
(
"Error: "
+
error
.
getMessage
())
.
build
())
);
}
}
\ No newline at end of file
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