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 <ThomasVitale@users.noreply.github.com>
This commit is contained in:
Thomas Vitale
2024-11-03 08:44:35 +01:00
committed by Christian Tzolov
parent 048d54b82b
commit cca430428e
2 changed files with 9 additions and 4 deletions

View File

@@ -37,11 +37,13 @@ import org.springframework.ai.document.Document;
import org.springframework.ai.model.Content;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.util.StringUtils;
/**
* Memory is retrieved from a VectorStore added into the prompt's system text.
*
* @author Christian Tzolov
* @author Thomas Vitale
* @since 1.0.0
*/
public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<VectorStore> {
@@ -58,7 +60,6 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
LONG_TERM_MEMORY:
{long_term_memory}
---------------------
""";
private final String systemTextAdvise;
@@ -128,7 +129,13 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
private AdvisedRequest before(AdvisedRequest request) {
String advisedSystemText = request.systemText() + System.lineSeparator() + this.systemTextAdvise;
String advisedSystemText;
if (StringUtils.hasText(request.systemText())) {
advisedSystemText = request.systemText() + System.lineSeparator() + this.systemTextAdvise;
}
else {
advisedSystemText = this.systemTextAdvise;
}
var searchRequest = SearchRequest.query(request.userText())
.withTopK(this.doGetChatMemoryRetrieveSize(request.adviseContext()))

View File

@@ -106,7 +106,6 @@ class PgVectorStoreWithChatMemoryAdvisorIT {
assertThat(promptCaptor.getValue().getInstructions().get(0)).isInstanceOf(SystemMessage.class);
assertThat(promptCaptor.getValue().getInstructions().get(0).getContent()).isEqualTo("""
Use the long term conversation memory from the LONG_TERM_MEMORY section to provide accurate answers.
---------------------
@@ -114,7 +113,6 @@ class PgVectorStoreWithChatMemoryAdvisorIT {
Tell me a good joke
Tell me a bad joke
---------------------
""");
}