Skip to content

Commit 9ec7fb1

Browse files
committed
Use streaming mode by default
1 parent ef0e7bc commit 9ec7fb1

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<dependencies>
2222
<dependency>
2323
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter-web</artifactId>
24+
<artifactId>spring-boot-starter-webflux</artifactId>
2525
</dependency>
2626
<dependency>
2727
<groupId>org.springframework.ai</groupId>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>com.javaaidev</groupId>
3333
<artifactId>chat-agent-ui</artifactId>
34-
<version>0.6.0</version>
34+
<version>0.7.0</version>
3535
</dependency>
3636

3737
<dependency>

src/main/java/com/javaaidev/agent/ChatAgentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import reactor.core.publisher.Flux;
1212

1313
@RestController
14-
@RequestMapping("/chat")
14+
@RequestMapping("/chat_non_streaming")
1515
public class ChatAgentController extends AbstractChatAgentController {
1616

1717
private final ChatClient chatClient;

src/main/java/com/javaaidev/agent/ChatAgentStreamingController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.javaaidev.chatagent.model.ChatRequest;
44
import org.springframework.ai.chat.client.ChatClient;
55
import org.springframework.ai.chat.messages.Message;
6+
import org.springframework.http.MediaType;
67
import org.springframework.http.codec.ServerSentEvent;
78
import org.springframework.web.bind.annotation.PostMapping;
89
import org.springframework.web.bind.annotation.RequestBody;
@@ -11,7 +12,7 @@
1112
import reactor.core.publisher.Flux;
1213

1314
@RestController
14-
@RequestMapping("/chat_streaming")
15+
@RequestMapping("/chat")
1516
public class ChatAgentStreamingController extends AbstractChatAgentController {
1617

1718
private final ChatClient chatClient;
@@ -20,7 +21,7 @@ public ChatAgentStreamingController(ChatClient.Builder builder) {
2021
chatClient = builder.build();
2122
}
2223

23-
@PostMapping
24+
@PostMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
2425
public Flux<ServerSentEvent<String>> chatStreaming(@RequestBody ChatRequest request) {
2526
var messages = chatRequestToMessages(request);
2627
return chatClient.prompt().system(SYSTEM_TEXT).messages(messages.toArray(new Message[0]))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.javaaidev.agent;
2+
3+
import org.springframework.ai.chat.client.ChatClient;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
7+
public class SimpleChatController {
8+
9+
public static final String SYSTEM_TEXT = """
10+
You are a chef who is proficient in various cuisines. Please answer users' questions about cooking.
11+
For other unrelated inputs, simply tell the user that you don't know.
12+
""";
13+
14+
private final ChatClient chatClient;
15+
16+
public SimpleChatController(ChatClient.Builder builder) {
17+
chatClient = builder.build();
18+
}
19+
20+
@PostMapping("/simple_chat")
21+
public ChatOutput chat(@RequestBody ChatInput chatInput) {
22+
return new ChatOutput(
23+
chatClient.prompt()
24+
.system(SYSTEM_TEXT)
25+
.user(chatInput.input())
26+
.call().content());
27+
}
28+
29+
30+
public record ChatInput(String input) {
31+
32+
}
33+
34+
public record ChatOutput(String output) {
35+
36+
}
37+
}

0 commit comments

Comments
 (0)