@@ -35,131 +35,147 @@ class ServiceWithFlowTest {
35
35
private lateinit var model: StreamingChatLanguageModel
36
36
37
37
@Test
38
- fun `Should use TokenStreamToStringFlowAdapter` () = runTest {
39
- val partialToken1 = " Hello"
40
- val partialToken2 = " world"
41
- val completeResponse = ChatResponse .builder().aiMessage(AiMessage (" Hello" )).build()
42
-
43
- doAnswer {
44
- val handler = it.arguments[1 ] as StreamingChatResponseHandler
45
- handler.onPartialResponse(partialToken1)
46
- handler.onPartialResponse(partialToken2)
47
- handler.onCompleteResponse(completeResponse)
48
- }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
49
-
50
- val assistant =
51
- AiServices
52
- .builder(Assistant ::class .java)
53
- .streamingChatLanguageModel(model)
54
- .build()
55
-
56
- val result = assistant.askQuestion(userName = " My friend" , question = " How are you?" )
57
- .toList()
58
-
59
- assertThat(result).containsExactly(partialToken1, partialToken2)
60
- }
38
+ fun `Should use TokenStreamToStringFlowAdapter` () =
39
+ runTest {
40
+ val partialToken1 = " Hello"
41
+ val partialToken2 = " world"
42
+ val completeResponse = ChatResponse .builder().aiMessage(AiMessage (" Hello" )).build()
43
+
44
+ doAnswer {
45
+ val handler = it.arguments[1 ] as StreamingChatResponseHandler
46
+ handler.onPartialResponse(partialToken1)
47
+ handler.onPartialResponse(partialToken2)
48
+ handler.onCompleteResponse(completeResponse)
49
+ }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
50
+
51
+ val assistant =
52
+ AiServices
53
+ .builder(Assistant ::class .java)
54
+ .streamingChatLanguageModel(model)
55
+ .build()
56
+
57
+ val result =
58
+ assistant
59
+ .askQuestion(userName = " My friend" , question = " How are you?" )
60
+ .toList()
61
+
62
+ assertThat(result).containsExactly(partialToken1, partialToken2)
63
+ }
61
64
62
65
@Test
63
- fun `Should use TokenStreamToStringFlowAdapter error` () = runTest {
64
- val partialToken1 = " Hello"
65
- val partialToken2 = " world"
66
- val error = RuntimeException (" Test error" )
67
-
68
- doAnswer {
69
- val handler = it.arguments[1 ] as StreamingChatResponseHandler
70
- handler.onPartialResponse(partialToken1)
71
- handler.onPartialResponse(partialToken2)
72
- handler.onError(error)
73
- }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
74
-
75
- val assistant =
76
- AiServices
77
- .builder(Assistant ::class .java)
78
- .streamingChatLanguageModel(model)
79
- .build()
80
-
81
-
82
- val response = assistant.askQuestion(userName = " My friend" , question = " How are you?" )
83
- .catch {
84
- val message =
85
- requireNotNull(it.message) { " Only $error is allowed to occur here but found $it " }
86
- emit(message)
87
- }.toList()
88
-
89
- assertThat(response).containsExactly(partialToken1, partialToken2, error.message)
90
- }
66
+ fun `Should use TokenStreamToStringFlowAdapter error` () =
67
+ runTest {
68
+ val partialToken1 = " Hello"
69
+ val partialToken2 = " world"
70
+ val error = RuntimeException (" Test error" )
71
+
72
+ doAnswer {
73
+ val handler = it.arguments[1 ] as StreamingChatResponseHandler
74
+ handler.onPartialResponse(partialToken1)
75
+ handler.onPartialResponse(partialToken2)
76
+ handler.onError(error)
77
+ }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
78
+
79
+ val assistant =
80
+ AiServices
81
+ .builder(Assistant ::class .java)
82
+ .streamingChatLanguageModel(model)
83
+ .build()
84
+
85
+ val response =
86
+ assistant
87
+ .askQuestion(userName = " My friend" , question = " How are you?" )
88
+ .catch {
89
+ val message =
90
+ requireNotNull(
91
+ it.message,
92
+ ) { " Only $error is allowed to occur here but found $it " }
93
+ emit(message)
94
+ }.toList()
95
+
96
+ assertThat(response).containsExactly(partialToken1, partialToken2, error.message)
97
+ }
91
98
92
99
@Test
93
- fun `Should use TokenStreamToReplyFlowAdapter` () = runTest {
94
- val partialToken1 = " Hello"
95
- val partialToken2 = " world"
96
- val completeResponse = ChatResponse .builder().aiMessage(AiMessage (" Hello" )).build()
97
-
98
- doAnswer {
99
- val handler = it.arguments[1 ] as StreamingChatResponseHandler
100
- handler.onPartialResponse(partialToken1)
101
- handler.onPartialResponse(partialToken2)
102
- handler.onCompleteResponse(completeResponse)
103
- }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
104
-
105
- val assistant =
106
- AiServices
107
- .builder(Assistant ::class .java)
108
- .streamingChatLanguageModel(model)
109
- .build()
110
-
111
- val result = assistant.askQuestion2(userName = " My friend" , question = " How are you?" )
112
- .toList()
113
-
114
- assertThat(result).startsWith(PartialResponse (partialToken1), PartialResponse (partialToken2))
115
- assertThat(result).index(2 ).isInstanceOf(CompleteResponse ::class )
116
- }
100
+ fun `Should use TokenStreamToReplyFlowAdapter` () =
101
+ runTest {
102
+ val partialToken1 = " Hello"
103
+ val partialToken2 = " world"
104
+ val completeResponse = ChatResponse .builder().aiMessage(AiMessage (" Hello" )).build()
105
+
106
+ doAnswer {
107
+ val handler = it.arguments[1 ] as StreamingChatResponseHandler
108
+ handler.onPartialResponse(partialToken1)
109
+ handler.onPartialResponse(partialToken2)
110
+ handler.onCompleteResponse(completeResponse)
111
+ }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
112
+
113
+ val assistant =
114
+ AiServices
115
+ .builder(Assistant ::class .java)
116
+ .streamingChatLanguageModel(model)
117
+ .build()
118
+
119
+ val result =
120
+ assistant
121
+ .askQuestion2(userName = " My friend" , question = " How are you?" )
122
+ .toList()
123
+
124
+ assertThat(
125
+ result,
126
+ ).startsWith(PartialResponse (partialToken1), PartialResponse (partialToken2))
127
+ assertThat(result).index(2 ).isInstanceOf(CompleteResponse ::class )
128
+ }
117
129
118
130
@Test
119
- fun `Should use TokenStreamToReplyFlowAdapter error` () = runTest {
120
- val partialToken1 = " Hello"
121
- val partialToken2 = " world"
122
- val error = RuntimeException (" Test error" )
123
-
124
- doAnswer {
125
- val handler = it.arguments[1 ] as StreamingChatResponseHandler
126
- handler.onPartialResponse(partialToken1)
127
- handler.onPartialResponse(partialToken2)
128
- handler.onError(error)
129
- }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
130
-
131
- val assistant =
132
- AiServices
133
- .builder(Assistant ::class .java)
134
- .streamingChatLanguageModel(model)
135
- .build()
136
-
137
- val response = assistant.askQuestion2(userName = " My friend" , question = " How are you?" )
138
- .catch { emit(StreamingChatLanguageModelReply .Error (it)) }
139
- .toList()
140
-
141
- assertThat(response).hasSize(3 )
142
- assertThat(response).startsWith(PartialResponse (partialToken1), PartialResponse (partialToken2))
143
- assertThat(response).index(2 ).isInstanceOf(StreamingChatLanguageModelReply .Error ::class )
144
- }
131
+ fun `Should use TokenStreamToReplyFlowAdapter error` () =
132
+ runTest {
133
+ val partialToken1 = " Hello"
134
+ val partialToken2 = " world"
135
+ val error = RuntimeException (" Test error" )
136
+
137
+ doAnswer {
138
+ val handler = it.arguments[1 ] as StreamingChatResponseHandler
139
+ handler.onPartialResponse(partialToken1)
140
+ handler.onPartialResponse(partialToken2)
141
+ handler.onError(error)
142
+ }.whenever(model).chat(any<ChatRequest >(), any<StreamingChatResponseHandler >())
143
+
144
+ val assistant =
145
+ AiServices
146
+ .builder(Assistant ::class .java)
147
+ .streamingChatLanguageModel(model)
148
+ .build()
149
+
150
+ val response =
151
+ assistant
152
+ .askQuestion2(userName = " My friend" , question = " How are you?" )
153
+ .catch { emit(StreamingChatLanguageModelReply .Error (it)) }
154
+ .toList()
155
+
156
+ assertThat(response).hasSize(3 )
157
+ assertThat(
158
+ response,
159
+ ).startsWith(PartialResponse (partialToken1), PartialResponse (partialToken2))
160
+ assertThat(response).index(2 ).isInstanceOf(StreamingChatLanguageModelReply .Error ::class )
161
+ }
145
162
146
163
@Suppress(" unused" )
147
164
private interface Assistant {
148
165
@dev.langchain4j.service.UserMessage (
149
- " Hello, I am {{ userName }}. {{ message }}."
166
+ " Hello, I am {{ userName }}. {{ message }}." ,
150
167
)
151
168
fun askQuestion (
152
169
@UserName userName : String ,
153
170
@V(" message" ) question : String ,
154
171
): Flow <String >
155
172
156
173
@dev.langchain4j.service.UserMessage (
157
- " Hello, I am {{ userName }}. {{ message }}."
174
+ " Hello, I am {{ userName }}. {{ message }}." ,
158
175
)
159
176
fun askQuestion2 (
160
177
@UserName userName : String ,
161
178
@V(" message" ) question : String ,
162
179
): Flow <StreamingChatLanguageModelReply >
163
180
}
164
181
}
165
-
0 commit comments