From 1114c3f86842e50ab13cf248ca8625a9047b7900 Mon Sep 17 00:00:00 2001 From: Sami Ekblad Date: Tue, 2 Jul 2024 16:53:50 +0300 Subject: [PATCH] Update to Spring AI 1.0 M1 --- pom.xml | 9 ++++++++- src/main/java/com/example/demo/MainView.java | 17 +++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 9b895e0..02bae75 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 17 24.3.12 + 1.0.0-M1 @@ -55,7 +56,6 @@ org.springframework.ai spring-ai-ollama-spring-boot-starter - 0.8.1 in.virit @@ -70,6 +70,13 @@ + + org.springframework.ai + spring-ai-bom + ${spring-ai.version} + pom + import + com.vaadin vaadin-bom diff --git a/src/main/java/com/example/demo/MainView.java b/src/main/java/com/example/demo/MainView.java index c25a1fc..cfdf673 100644 --- a/src/main/java/com/example/demo/MainView.java +++ b/src/main/java/com/example/demo/MainView.java @@ -4,7 +4,7 @@ import com.vaadin.flow.component.orderedlayout.Scroller; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.Route; -import org.springframework.ai.chat.StreamingChatClient; +import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.*; import org.springframework.ai.chat.prompt.Prompt; import org.vaadin.firitin.components.messagelist.MarkdownMessage; @@ -12,7 +12,7 @@ import java.util.ArrayList; @Route("") // map view to the root -class MainView extends VerticalLayout { +public class MainView extends VerticalLayout { private final ArrayList chatHistory = new ArrayList<>(); @@ -20,7 +20,7 @@ class MainView extends VerticalLayout { Scroller messageScroller = new Scroller(messageList); MessageInput messageInput = new MessageInput(); - MainView(StreamingChatClient chatClient) { + public MainView(ChatClient.Builder chatClientBuilder) { add(messageScroller, messageInput); setSizeFull(); setMargin(false); @@ -28,8 +28,12 @@ class MainView extends VerticalLayout { messageInput.setWidthFull(); // Add system message to help the AI to behave - chatHistory.add(new SystemMessage("Only if the user asks you about Vaadin, reply in bro style. Always show a piece a code.")); + chatHistory.add(new SystemMessage("Answer politely to user. When user asks you about Vaadin, reply in bro style. Always show a piece a code.")); + // Init the client + ChatClient chatClient = chatClientBuilder.build(); + + // Pass user input to chatClient messageInput.addSubmitListener(ev -> { // Add use input as markdown message chatHistory.add(new UserMessage(ev.getValue())); @@ -41,9 +45,10 @@ class MainView extends VerticalLayout { // Ask AI and stream back the reply to UI Prompt prompt = new Prompt(chatHistory); - chatClient.stream(prompt) + chatClient.prompt(prompt) + .stream().content() .doOnComplete(() -> chatHistory.add(new AssistantMessage(reply.getMarkdown()))) - .subscribe(cr -> reply.appendMarkdownAsync(cr.getResult().getOutput().getContent())); + .subscribe(reply::appendMarkdownAsync); reply.scrollIntoView(); }); }