|
1 | 1 | /* |
2 | | - * Copyright 2023-2024 the original author or authors. |
| 2 | + * Copyright 2023-2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
21 | 21 | import java.util.Map; |
22 | 22 | import java.util.stream.Collectors; |
23 | 23 |
|
| 24 | +import org.springframework.util.StringUtils; |
24 | 25 | import reactor.core.publisher.Flux; |
25 | 26 |
|
26 | 27 | import org.springframework.ai.chat.client.advisor.api.AdvisedRequest; |
|
33 | 34 | import org.springframework.ai.chat.messages.MessageType; |
34 | 35 | import org.springframework.ai.chat.messages.UserMessage; |
35 | 36 | import org.springframework.ai.chat.model.MessageAggregator; |
36 | | -import org.springframework.ai.content.Content; |
37 | 37 |
|
38 | 38 | /** |
39 | 39 | * Memory is retrieved added into the prompt's system text. |
40 | 40 | * |
41 | 41 | * @author Christian Tzolov |
| 42 | + * @author Miloš Havránek |
42 | 43 | * @since 1.0.0 |
43 | 44 | */ |
44 | 45 | public class PromptChatMemoryAdvisor extends AbstractChatMemoryAdvisor<ChatMemory> { |
@@ -111,14 +112,16 @@ private AdvisedRequest before(AdvisedRequest request) { |
111 | 112 |
|
112 | 113 | String memory = (memoryMessages != null) ? memoryMessages.stream() |
113 | 114 | .filter(m -> m.getMessageType() == MessageType.USER || m.getMessageType() == MessageType.ASSISTANT) |
114 | | - .map(m -> m.getMessageType() + ":" + ((Content) m).getText()) |
| 115 | + .map(m -> m.getMessageType() + ":" + m.getText()) |
115 | 116 | .collect(Collectors.joining(System.lineSeparator())) : ""; |
116 | 117 |
|
117 | 118 | Map<String, Object> advisedSystemParams = new HashMap<>(request.systemParams()); |
118 | 119 | advisedSystemParams.put("memory", memory); |
119 | 120 |
|
120 | 121 | // 2. Advise the system text. |
121 | | - String advisedSystemText = request.systemText() + System.lineSeparator() + this.systemTextAdvise; |
| 122 | + String systemText = request.systemText(); |
| 123 | + String advisedSystemText = (StringUtils.hasText(systemText) ? systemText + System.lineSeparator() : "") |
| 124 | + + this.systemTextAdvise; |
122 | 125 |
|
123 | 126 | // 3. Create a new request with the advised system text and parameters. |
124 | 127 | AdvisedRequest advisedRequest = AdvisedRequest.from(request) |
|
0 commit comments