File tree Expand file tree Collapse file tree 5 files changed +39
-10
lines changed
src/main/java/com/javaaidev/chatagent/springai Expand file tree Collapse file tree 5 files changed +39
-10
lines changed Original file line number Diff line number Diff line change @@ -38,19 +38,21 @@ const AgentModelAdapter: ChatModelAdapter = {
38
38
let text = "" ;
39
39
40
40
for ( ; ; ) {
41
- const { done, value} = await eventStream . read ( )
41
+ const { done, value} = await eventStream . read ( ) ;
42
42
if ( ! done ) {
43
- let data = value . data || "" ;
44
- if ( data . length > 2 ) {
45
- data = data . substring ( 1 , data . length - 1 ) ;
43
+ if ( ! value . data ) {
44
+ continue ;
46
45
}
47
- text += data ;
46
+ const { content } = JSON . parse ( value . data ) ;
47
+ if ( ! content || content . length == 0 || content [ 0 ] . type !== "text" || ! content [ 0 ] . text ) {
48
+ continue ;
49
+ }
50
+ text += content [ 0 ] . text ;
48
51
yield {
49
52
content : [ { type : "text" , text } ] ,
50
53
} ;
51
- continue
52
54
} else {
53
- break
55
+ break ;
54
56
}
55
57
}
56
58
} ,
Original file line number Diff line number Diff line change 6
6
<parent >
7
7
<groupId >com.javaaidev.chatagentui</groupId >
8
8
<artifactId >chat-agent-ui-parent</artifactId >
9
- <version >0.9 .0</version >
9
+ <version >0.10 .0</version >
10
10
</parent >
11
11
12
12
<artifactId >chat-agent-ui</artifactId >
Original file line number Diff line number Diff line change 5
5
<modelVersion >4.0.0</modelVersion >
6
6
<groupId >com.javaaidev.chatagentui</groupId >
7
7
<artifactId >chat-agent-ui-parent</artifactId >
8
- <version >0.9 .0</version >
8
+ <version >0.10 .0</version >
9
9
<packaging >pom</packaging >
10
10
<name >Chat Agent UI :: Parent</name >
11
11
<description >Chat Agent UI</description >
Original file line number Diff line number Diff line change 6
6
<parent >
7
7
<groupId >com.javaaidev.chatagentui</groupId >
8
8
<artifactId >chat-agent-ui-parent</artifactId >
9
- <version >0.9 .0</version >
9
+ <version >0.10 .0</version >
10
10
</parent >
11
11
12
12
<artifactId >spring-ai-adapter</artifactId >
19
19
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
20
20
<java .version>17</java .version>
21
21
<spring-ai .version>1.0.0-M5</spring-ai .version>
22
+ <spring .version>6.2.3</spring .version>
22
23
</properties >
23
24
24
25
<dependencyManagement >
38
39
<groupId >org.springframework.ai</groupId >
39
40
<artifactId >spring-ai-core</artifactId >
40
41
</dependency >
42
+ <dependency >
43
+ <groupId >org.springframework</groupId >
44
+ <artifactId >spring-web</artifactId >
45
+ <version >${spring.version} </version >
46
+ </dependency >
41
47
<dependency >
42
48
<groupId >com.javaaidev.chatagentui</groupId >
43
49
<artifactId >chat-agent-ui</artifactId >
Original file line number Diff line number Diff line change 15
15
import org .springframework .ai .chat .messages .UserMessage ;
16
16
import org .springframework .ai .chat .model .ChatResponse ;
17
17
import org .springframework .ai .chat .model .Generation ;
18
+ import org .springframework .http .codec .ServerSentEvent ;
19
+ import reactor .core .publisher .Flux ;
18
20
19
21
/**
20
22
* Convert models between chat agent and Spring AI
@@ -64,4 +66,23 @@ public static ChatAgentResponse toResponse(ChatResponse chatResponse) {
64
66
}
65
67
return new ChatAgentResponse (content );
66
68
}
69
+
70
+ /**
71
+ * Convert a stream of Spring AI {@linkplain ChatResponse} to a stream of
72
+ * {@linkplain ChatAgentResponse} using Server-sent Events
73
+ *
74
+ * @param chatResponse Stream of {@linkplain ChatResponse}
75
+ * @return Stream of {@linkplain ChatAgentResponse}
76
+ */
77
+ public static Flux <ServerSentEvent <ChatAgentResponse >> toStreamingResponse (
78
+ Flux <ChatResponse > chatResponse ) {
79
+ return chatResponse .concatMap (
80
+ response -> Flux .fromIterable (response .getResults ()))
81
+ .filter (generation -> Objects .nonNull (generation .getOutput ().getText ()))
82
+ .map (generation -> generation .getOutput ().getText ())
83
+ .map (text -> ServerSentEvent .<ChatAgentResponse >builder ()
84
+ .data (new ChatAgentResponse (
85
+ List .of (new TextContentPart (text ))))
86
+ .build ());
87
+ }
67
88
}
You can’t perform that action at this time.
0 commit comments