Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.mockito.Mockito;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
Expand Down Expand Up @@ -225,8 +226,14 @@ void testChatMemory() throws IOException {
val prompt1 = new Prompt("What is the capital of France?");
val prompt2 = new Prompt("And what is the typical food there?");

cl.prompt(prompt1).call().content();
cl.prompt(prompt2).call().content();
cl.prompt(prompt1)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "test-conversation"))
.call()
.content();
cl.prompt(prompt2)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "test-conversation"))
.call()
.content();
// The response is not important
// We just want to verify that the second call remembered the first call
try (var requestInputStream = fileLoader.apply("chatMemory.json")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.mockito.Mockito;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
Expand Down Expand Up @@ -246,8 +247,14 @@ void testChatMemory() throws IOException {
val prompt1 = new Prompt("What is the capital of France?", defaultOptions);
val prompt2 = new Prompt("And what is the typical food there?", defaultOptions);

cl.prompt(prompt1).call().content();
cl.prompt(prompt2).call().content();
cl.prompt(prompt1)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "test-conversation"))
.call()
.content();
cl.prompt(prompt2)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "test-conversation"))
.call()
.content();
// The response is not important
// We just want to verify that the second call remembered the first call
try (var requestInputStream = fileLoader.apply("chatMemory.json")) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<checkstyle.version>13.4.2</checkstyle.version>
<system-stubs.version>2.1.3</system-stubs.version>
<surefire.version>3.5.5</surefire.version>
<spring-ai.version>1.1.5</spring-ai.version>
<spring-ai.version>1.1.6</spring-ai.version>
<reactor-core.version>3.8.5</reactor-core.version>
<dotenv-java.version>3.2.0</dotenv-java.version>
<mockito.version>5.23.0</mockito.version>
Expand Down
6 changes: 6 additions & 0 deletions sample-code/spring-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<artifactId>tomcat-embed-websocket</artifactId>
<version>${apache-tomcat-embed.version}</version>
</dependency>
<!-- Pin mcp-core to align with spring-ai-client-chat transitive dependency -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-core</artifactId>
<version>${mcp-core.version}</version>
</dependency>
<!-- Enforce consistent Junit version -->
<dependency>
<groupId>org.junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.val;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.Message;
Expand Down Expand Up @@ -147,7 +148,11 @@ Generation promptRegistryToSpringAi() {

final List<Message> messages = SpringAiConverter.promptTemplateToMessages(promptResponse);
val prompt = new Prompt(messages);
val response = cl.prompt(prompt).call().chatResponse();
val response =
cl.prompt(prompt)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.chatResponse();
return response != null ? response.getResult() : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.val;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
Expand Down Expand Up @@ -68,7 +69,12 @@ public ChatResponse runAgent(@Nonnull final String userInput) {

// Make a call to the LLM with the new input
response =
Objects.requireNonNull(cl.prompt(prompt).call().chatResponse(), "Chat response is null.");
Objects.requireNonNull(
cl.prompt(prompt)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.chatResponse(),
"Chat response is null.");
responseText = response.getResult().getOutput().getText();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.val;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
Expand Down Expand Up @@ -112,8 +113,15 @@ public ChatResponse chatMemory() {
val prompt1 = new Prompt("What is the capital of France?");
val prompt2 = new Prompt("And what is the typical food there?");

cl.prompt(prompt1).call().content();
cl.prompt(prompt1)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.content();
return Objects.requireNonNull(
cl.prompt(prompt2).call().chatResponse(), "Chat response is null");
cl.prompt(prompt2)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.chatResponse(),
"Chat response is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.val;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.SystemMessage;
Expand Down Expand Up @@ -234,9 +235,16 @@ public ChatResponse chatMemory() {
val prompt1 = new Prompt("What is the capital of France?", defaultOptions);
val prompt2 = new Prompt("And what is the typical food there?", defaultOptions);

cl.prompt(prompt1).call().content();
cl.prompt(prompt1)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.content();
return Objects.requireNonNull(
cl.prompt(prompt2).call().chatResponse(), "Chat response is null");
cl.prompt(prompt2)
.advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "conversation id"))
.call()
.chatResponse(),
"Chat response is null");
}

/**
Expand Down