Skip to content

Commit

Permalink
Update to Spring AI 1.0 M1
Browse files Browse the repository at this point in the history
  • Loading branch information
samie committed Jul 2, 2024
1 parent 82dc9ff commit 1114c3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<properties>
<java.version>17</java.version>
<vaadin.version>24.3.12</vaadin.version>
<spring-ai.version>1.0.0-M1</spring-ai.version>
</properties>
<repositories>
<repository>
Expand Down Expand Up @@ -55,7 +56,6 @@
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>0.8.1</version>
</dependency>
<dependency>
<groupId>in.virit</groupId>
Expand All @@ -70,6 +70,13 @@
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/example/demo/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@
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;

import java.util.ArrayList;

@Route("") // map view to the root
class MainView extends VerticalLayout {
public class MainView extends VerticalLayout {

private final ArrayList<Message> chatHistory = new ArrayList<>();

VerticalLayout messageList = new VerticalLayout();
Scroller messageScroller = new Scroller(messageList);
MessageInput messageInput = new MessageInput();

MainView(StreamingChatClient chatClient) {
public MainView(ChatClient.Builder chatClientBuilder) {
add(messageScroller, messageInput);
setSizeFull();
setMargin(false);
messageScroller.setSizeFull();
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()));
Expand All @@ -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();
});
}
Expand Down

0 comments on commit 1114c3f

Please sign in to comment.