1818
1919import java .util .ArrayList ;
2020import java .util .List ;
21+ import java .util .function .Supplier ;
2122
2223import reactor .core .publisher .Flux ;
2324import reactor .core .publisher .Mono ;
4243 * @author Christian Tzolov
4344 * @author Mark Pollack
4445 * @author Thomas Vitale
46+ * @author Tomasz Forys
4547 * @since 1.0.0
4648 */
4749public final class MessageChatMemoryAdvisor implements BaseChatMemoryAdvisor {
4850
4951 private final ChatMemory chatMemory ;
5052
51- private final String defaultConversationId ;
53+ private final Supplier < String > defaultConversationId ;
5254
5355 private final int order ;
5456
5557 private final Scheduler scheduler ;
5658
57- private MessageChatMemoryAdvisor (ChatMemory chatMemory , String defaultConversationId , int order ,
58- Scheduler scheduler ) {
59+ private MessageChatMemoryAdvisor (ChatMemory chatMemory , Supplier < String > defaultConversationId , int order ,
60+ Scheduler scheduler ) {
5961 Assert .notNull (chatMemory , "chatMemory cannot be null" );
60- Assert .hasText (defaultConversationId , "defaultConversationId cannot be null or empty" );
62+ Assert .hasText (defaultConversationId . get () , "defaultConversationId cannot be null or empty" );
6163 Assert .notNull (scheduler , "scheduler cannot be null" );
6264 this .chatMemory = chatMemory ;
6365 this .defaultConversationId = defaultConversationId ;
@@ -77,7 +79,7 @@ public Scheduler getScheduler() {
7779
7880 @ Override
7981 public ChatClientRequest before (ChatClientRequest chatClientRequest , AdvisorChain advisorChain ) {
80- String conversationId = getConversationId (chatClientRequest .context (), this .defaultConversationId );
82+ String conversationId = getConversationId (chatClientRequest .context (), this .defaultConversationId . get () );
8183
8284 // 1. Retrieve the chat memory for the current conversation.
8385 List <Message > memoryMessages = this .chatMemory .get (conversationId );
@@ -108,7 +110,7 @@ public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorCh
108110 .map (g -> (Message ) g .getOutput ())
109111 .toList ();
110112 }
111- this .chatMemory .add (this .getConversationId (chatClientResponse .context (), this .defaultConversationId ),
113+ this .chatMemory .add (this .getConversationId (chatClientResponse .context (), this .defaultConversationId . get () ),
112114 assistantMessages );
113115 return chatClientResponse ;
114116 }
@@ -134,7 +136,7 @@ public static Builder builder(ChatMemory chatMemory) {
134136
135137 public static final class Builder {
136138
137- private String conversationId = ChatMemory .DEFAULT_CONVERSATION_ID ;
139+ private Supplier < String > conversationIdSupplier = () -> ChatMemory .DEFAULT_CONVERSATION_ID ;
138140
139141 private int order = Advisor .DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER ;
140142
@@ -152,7 +154,17 @@ private Builder(ChatMemory chatMemory) {
152154 * @return the builder
153155 */
154156 public Builder conversationId (String conversationId ) {
155- this .conversationId = conversationId ;
157+ this .conversationIdSupplier = () -> conversationId ;
158+ return this ;
159+ }
160+
161+ /**
162+ * Set the conversation id supplier.
163+ * @param conversationIdSupplier the conversation id supplier
164+ * @return the builder
165+ */
166+ public Builder conversationIdSupplier (Supplier <String > conversationIdSupplier ) {
167+ this .conversationIdSupplier = conversationIdSupplier ;
156168 return this ;
157169 }
158170
@@ -176,7 +188,7 @@ public Builder scheduler(Scheduler scheduler) {
176188 * @return the advisor
177189 */
178190 public MessageChatMemoryAdvisor build () {
179- return new MessageChatMemoryAdvisor (this .chatMemory , this .conversationId , this .order , this .scheduler );
191+ return new MessageChatMemoryAdvisor (this .chatMemory , this .conversationIdSupplier , this .order , this .scheduler );
180192 }
181193
182194 }
0 commit comments