Skip to content

Commit cca4304

Browse files
ThomasVitaletzolov
authored andcommitted
Support case with missing system text in VectorStoreChatMemoryAdvisor
The system text advise prompt is only joined with the value of the system text if it's non-null and non-empty. Signed-off-by: Thomas Vitale <[email protected]>
1 parent 048d54b commit cca4304

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

spring-ai-core/src/main/java/org/springframework/ai/chat/client/advisor/VectorStoreChatMemoryAdvisor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
import org.springframework.ai.model.Content;
3838
import org.springframework.ai.vectorstore.SearchRequest;
3939
import org.springframework.ai.vectorstore.VectorStore;
40+
import org.springframework.util.StringUtils;
4041

4142
/**
4243
* Memory is retrieved from a VectorStore added into the prompt's system text.
4344
*
4445
* @author Christian Tzolov
46+
* @author Thomas Vitale
4547
* @since 1.0.0
4648
*/
4749
public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<VectorStore> {
@@ -58,7 +60,6 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
5860
LONG_TERM_MEMORY:
5961
{long_term_memory}
6062
---------------------
61-
6263
""";
6364

6465
private final String systemTextAdvise;
@@ -128,7 +129,13 @@ public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamA
128129

129130
private AdvisedRequest before(AdvisedRequest request) {
130131

131-
String advisedSystemText = request.systemText() + System.lineSeparator() + this.systemTextAdvise;
132+
String advisedSystemText;
133+
if (StringUtils.hasText(request.systemText())) {
134+
advisedSystemText = request.systemText() + System.lineSeparator() + this.systemTextAdvise;
135+
}
136+
else {
137+
advisedSystemText = this.systemTextAdvise;
138+
}
132139

133140
var searchRequest = SearchRequest.query(request.userText())
134141
.withTopK(this.doGetChatMemoryRetrieveSize(request.adviseContext()))

vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/PgVectorStoreWithChatMemoryAdvisorIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,13 @@ private static void verifyRequestHasBeenAdvisedWithMessagesFromVectorStore(ChatM
106106
assertThat(promptCaptor.getValue().getInstructions().get(0)).isInstanceOf(SystemMessage.class);
107107
assertThat(promptCaptor.getValue().getInstructions().get(0).getContent()).isEqualTo("""
108108
109-
110109
Use the long term conversation memory from the LONG_TERM_MEMORY section to provide accurate answers.
111110
112111
---------------------
113112
LONG_TERM_MEMORY:
114113
Tell me a good joke
115114
Tell me a bad joke
116115
---------------------
117-
118116
""");
119117
}
120118

0 commit comments

Comments
 (0)