Skip to content

Commit e57e62c

Browse files
committed
Make ChatMemoryAdvisor's ctors private
1 parent cb8d68b commit e57e62c

File tree

9 files changed

+173
-174
lines changed

9 files changed

+173
-174
lines changed

advisors/spring-ai-advisors-vector-store/src/main/java/org/springframework/ai/chat/client/advisor/vectorstore/VectorStoreChatMemoryAdvisor.java

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.springframework.ai.chat.client.ChatClientRequest;
2525
import org.springframework.ai.chat.client.ChatClientResponse;
2626
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
27+
import org.springframework.ai.chat.client.advisor.api.Advisor;
2728
import org.springframework.ai.chat.client.advisor.api.AdvisorChain;
29+
import org.springframework.ai.chat.memory.ChatMemory;
2830
import org.springframework.ai.chat.messages.AssistantMessage;
2931
import org.springframework.ai.chat.messages.Message;
3032
import org.springframework.ai.chat.messages.MessageType;
@@ -72,7 +74,7 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
7274

7375
protected final int defaultChatMemoryRetrieveSize;
7476

75-
public VectorStoreChatMemoryAdvisor(VectorStore chatMemory, String defaultConversationId,
77+
private VectorStoreChatMemoryAdvisor(VectorStore chatMemory, String defaultConversationId,
7678
int defaultChatMemoryRetrieveSize, boolean protectFromBlocking, PromptTemplate systemPromptTemplate,
7779
int order) {
7880
super(chatMemory, defaultConversationId, protectFromBlocking, order);
@@ -171,23 +173,26 @@ else if (message instanceof AssistantMessage assistantMessage) {
171173
/**
172174
* Builder for VectorStoreChatMemoryAdvisor.
173175
*/
174-
public static class Builder extends AbstractChatMemoryAdvisor.AbstractBuilder<VectorStore, Builder> {
176+
public static class Builder {
175177

176178
private PromptTemplate systemPromptTemplate = DEFAULT_SYSTEM_PROMPT_TEMPLATE;
177179

178180
private Integer chatMemoryRetrieveSize = DEFAULT_CHAT_MEMORY_RESPONSE_SIZE;
179181

182+
private String conversationId = ChatMemory.DEFAULT_CONVERSATION_ID;
183+
184+
private boolean protectFromBlocking = true;
185+
186+
private int order = Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER;
187+
188+
private VectorStore chatMemory;
189+
180190
/**
181191
* Creates a new builder instance.
182192
* @param vectorStore the vector store to use
183193
*/
184194
protected Builder(VectorStore vectorStore) {
185-
super(vectorStore);
186-
}
187-
188-
@Override
189-
protected Builder self() {
190-
return this;
195+
this.chatMemory = vectorStore;
191196
}
192197

193198
/**
@@ -197,17 +202,17 @@ protected Builder self() {
197202
*/
198203
public Builder systemPromptTemplate(PromptTemplate systemPromptTemplate) {
199204
this.systemPromptTemplate = systemPromptTemplate;
200-
return self();
205+
return this;
201206
}
202207

203208
/**
204-
* Set the system prompt template using a text template.
205-
* @param systemTextAdvise the system prompt text template
209+
* Set the system text advice.
210+
* @param systemTextAdvise the system text advice
206211
* @return this builder
207212
*/
208213
public Builder systemTextAdvise(String systemTextAdvise) {
209214
this.systemPromptTemplate = new PromptTemplate(systemTextAdvise);
210-
return self();
215+
return this;
211216
}
212217

213218
/**
@@ -217,10 +222,43 @@ public Builder systemTextAdvise(String systemTextAdvise) {
217222
*/
218223
public Builder chatMemoryRetrieveSize(int chatMemoryRetrieveSize) {
219224
this.chatMemoryRetrieveSize = chatMemoryRetrieveSize;
220-
return self();
225+
return this;
221226
}
222227

223-
@Override
228+
/**
229+
* Set the conversation id.
230+
* @param conversationId the conversation id
231+
* @return the builder
232+
*/
233+
public Builder conversationId(String conversationId) {
234+
this.conversationId = conversationId;
235+
return this;
236+
}
237+
238+
/**
239+
* Set whether to protect from blocking.
240+
* @param protectFromBlocking whether to protect from blocking
241+
* @return the builder
242+
*/
243+
public Builder protectFromBlocking(boolean protectFromBlocking) {
244+
this.protectFromBlocking = protectFromBlocking;
245+
return this;
246+
}
247+
248+
/**
249+
* Set the order.
250+
* @param order the order
251+
* @return the builder
252+
*/
253+
public Builder order(int order) {
254+
this.order = order;
255+
return this;
256+
}
257+
258+
/**
259+
* Build the advisor.
260+
* @return the advisor
261+
*/
224262
public VectorStoreChatMemoryAdvisor build() {
225263
return new VectorStoreChatMemoryAdvisor(this.chatMemory, this.conversationId, this.chatMemoryRetrieveSize,
226264
this.protectFromBlocking, this.systemPromptTemplate, this.order);

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/OpenAiChatClientMemoryAdvisorReproIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void messageChatMemoryAdvisor_withPromptMessages_throwsException() {
3939
ChatMemory chatMemory = MessageWindowChatMemory.builder()
4040
.chatMemoryRepository(new InMemoryChatMemoryRepository())
4141
.build();
42-
MessageChatMemoryAdvisor advisor = new MessageChatMemoryAdvisor(chatMemory);
42+
MessageChatMemoryAdvisor advisor = MessageChatMemoryAdvisor.builder(chatMemory).build();
4343

4444
ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build();
4545

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/advisor/AbstractChatMemoryAdvisorIT.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ public abstract class AbstractChatMemoryAdvisorIT {
5757
*/
5858
protected abstract AbstractChatMemoryAdvisor<?> createAdvisor(ChatMemory chatMemory);
5959

60-
/**
61-
* Create an advisor without a default conversation ID. This is needed for testing
62-
* custom conversation IDs.
63-
* @param chatMemory The chat memory to use
64-
* @return An instance of the advisor without a default conversation ID
65-
*/
66-
protected abstract AbstractChatMemoryAdvisor<?> createAdvisorWithoutDefaultId(ChatMemory chatMemory);
67-
6860
/**
6961
* Assert the follow-up response meets the expectations for this advisor type. Default
7062
* implementation expects the model to remember "John" from the first message.
@@ -199,7 +191,7 @@ protected void testUseCustomConversationId() {
199191
.build();
200192

201193
// Create advisor without a default conversation ID
202-
AbstractChatMemoryAdvisor<?> advisor = createAdvisorWithoutDefaultId(chatMemory);
194+
AbstractChatMemoryAdvisor<?> advisor = createAdvisor(chatMemory);
203195

204196
ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build();
205197

@@ -237,7 +229,7 @@ protected void testMaintainSeparateConversations() {
237229
.build();
238230

239231
// Create advisor without a default conversation ID
240-
AbstractChatMemoryAdvisor<?> advisor = createAdvisorWithoutDefaultId(chatMemory);
232+
AbstractChatMemoryAdvisor<?> advisor = createAdvisor(chatMemory);
241233

242234
ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build();
243235

@@ -322,7 +314,7 @@ protected void testHandleNonExistentConversation() {
322314
.build();
323315

324316
// Create advisor without a default conversation ID
325-
AbstractChatMemoryAdvisor<?> advisor = createAdvisorWithoutDefaultId(chatMemory);
317+
AbstractChatMemoryAdvisor<?> advisor = createAdvisor(chatMemory);
326318

327319
ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build();
328320

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/advisor/MessageChatMemoryAdvisorIT.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ public class MessageChatMemoryAdvisorIT extends AbstractChatMemoryAdvisorIT {
5353

5454
@Override
5555
protected AbstractChatMemoryAdvisor<?> createAdvisor(ChatMemory chatMemory) {
56-
return new MessageChatMemoryAdvisor(chatMemory);
57-
}
58-
59-
@Override
60-
protected AbstractChatMemoryAdvisor<?> createAdvisorWithoutDefaultId(ChatMemory chatMemory) {
61-
return new MessageChatMemoryAdvisor(chatMemory);
56+
return MessageChatMemoryAdvisor.builder(chatMemory).build();
6257
}
6358

6459
@Test
@@ -90,7 +85,9 @@ void shouldHandleMultipleUserMessagesInPrompt() {
9085
.build();
9186

9287
// Create MessageChatMemoryAdvisor with the conversation ID
93-
MessageChatMemoryAdvisor advisor = new MessageChatMemoryAdvisor(chatMemory, conversationId);
88+
MessageChatMemoryAdvisor advisor = MessageChatMemoryAdvisor.builder(chatMemory)
89+
.conversationId(conversationId)
90+
.build();
9491

9592
ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build();
9693

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/advisor/PromptChatMemoryAdvisorIT.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ public class PromptChatMemoryAdvisorIT extends AbstractChatMemoryAdvisorIT {
4444

4545
@Override
4646
protected AbstractChatMemoryAdvisor<?> createAdvisor(ChatMemory chatMemory) {
47-
return new PromptChatMemoryAdvisor(chatMemory);
48-
}
49-
50-
@Override
51-
protected AbstractChatMemoryAdvisor<?> createAdvisorWithoutDefaultId(ChatMemory chatMemory) {
52-
return new PromptChatMemoryAdvisor(chatMemory);
47+
return PromptChatMemoryAdvisor.builder(chatMemory).build();
5348
}
5449

5550
@Override

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

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -134,90 +134,7 @@ protected String doGetConversationId(Map<String, Object> context) {
134134

135135
@Override
136136
public Scheduler getScheduler() {
137-
return this.protectFromBlocking ? BaseAdvisor.DEFAULT_SCHEDULER : Schedulers.immediate();
138-
}
139-
140-
/**
141-
* Abstract builder for {@link AbstractChatMemoryAdvisor}.
142-
*
143-
* @param <T> the type of the chat memory
144-
* @param <B> the type of the builder (self-type)
145-
*/
146-
public static abstract class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
147-
148-
/**
149-
* The conversation id.
150-
*/
151-
protected String conversationId = ChatMemory.DEFAULT_CONVERSATION_ID;
152-
153-
/**
154-
* Whether to protect from blocking.
155-
*/
156-
protected boolean protectFromBlocking = true;
157-
158-
/**
159-
* The order of the advisor.
160-
*/
161-
protected int order = Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER;
162-
163-
/**
164-
* The chat memory.
165-
*/
166-
protected T chatMemory;
167-
168-
/**
169-
* Constructor to create a new {@link AbstractBuilder} instance.
170-
* @param chatMemory the chat memory
171-
*/
172-
protected AbstractBuilder(T chatMemory) {
173-
this.chatMemory = chatMemory;
174-
}
175-
176-
/**
177-
* Returns this builder as the parameterized type.
178-
* @return this builder
179-
*/
180-
@SuppressWarnings("unchecked")
181-
protected B self() {
182-
return (B) this;
183-
}
184-
185-
/**
186-
* Set the conversation id.
187-
* @param conversationId the conversation id
188-
* @return the builder
189-
*/
190-
public B conversationId(String conversationId) {
191-
this.conversationId = conversationId;
192-
return self();
193-
}
194-
195-
/**
196-
* Set whether to protect from blocking.
197-
* @param protectFromBlocking whether to protect from blocking
198-
* @return the builder
199-
*/
200-
public B protectFromBlocking(boolean protectFromBlocking) {
201-
this.protectFromBlocking = protectFromBlocking;
202-
return self();
203-
}
204-
205-
/**
206-
* Set the order.
207-
* @param order the order
208-
* @return the builder
209-
*/
210-
public B order(int order) {
211-
this.order = order;
212-
return self();
213-
}
214-
215-
/**
216-
* Build the advisor.
217-
* @return the advisor
218-
*/
219-
abstract public AbstractChatMemoryAdvisor<T> build();
220-
137+
return this.protectFromBlocking ? Schedulers.boundedElastic() : Schedulers.immediate();
221138
}
222139

223140
}

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,10 @@
3636
*/
3737
public class MessageChatMemoryAdvisor extends AbstractChatMemoryAdvisor<ChatMemory> {
3838

39-
public MessageChatMemoryAdvisor(ChatMemory chatMemory) {
40-
super(chatMemory);
41-
}
42-
43-
public MessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId) {
44-
this(chatMemory, defaultConversationId, Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER);
45-
}
46-
47-
public MessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int order) {
39+
private MessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int order) {
4840
super(chatMemory, defaultConversationId, true, order);
4941
}
5042

51-
public static Builder builder(ChatMemory chatMemory) {
52-
return new Builder(chatMemory);
53-
}
54-
5543
@Override
5644
public ChatClientRequest before(ChatClientRequest request, AdvisorChain advisorChain) {
5745
String conversationId = doGetConversationId(request.context());
@@ -95,17 +83,58 @@ public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorCh
9583
return chatClientResponse;
9684
}
9785

98-
public static class Builder extends AbstractChatMemoryAdvisor.AbstractBuilder<ChatMemory, Builder> {
86+
public static Builder builder(ChatMemory chatMemory) {
87+
return new Builder(chatMemory);
88+
}
89+
90+
public static class Builder {
91+
92+
private String conversationId = ChatMemory.DEFAULT_CONVERSATION_ID;
93+
94+
private boolean protectFromBlocking = true;
95+
96+
private int order = Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER;
97+
98+
private ChatMemory chatMemory;
9999

100100
protected Builder(ChatMemory chatMemory) {
101-
super(chatMemory);
101+
this.chatMemory = chatMemory;
102+
}
103+
104+
/**
105+
* Set the conversation id.
106+
* @param conversationId the conversation id
107+
* @return the builder
108+
*/
109+
public Builder conversationId(String conversationId) {
110+
this.conversationId = conversationId;
111+
return this;
112+
}
113+
114+
/**
115+
* Set whether to protect from blocking.
116+
* @param protectFromBlocking whether to protect from blocking
117+
* @return the builder
118+
*/
119+
public Builder protectFromBlocking(boolean protectFromBlocking) {
120+
this.protectFromBlocking = protectFromBlocking;
121+
return this;
102122
}
103123

104-
@Override
105-
protected Builder self() {
124+
/**
125+
* Set the order.
126+
* @param order the order
127+
* @return the builder
128+
*/
129+
public Builder order(int order) {
130+
this.order = order;
106131
return this;
107132
}
108133

134+
/**
135+
* Build the advisor.
136+
* @return the advisor
137+
*/
109138
public MessageChatMemoryAdvisor build() {
110139
return new MessageChatMemoryAdvisor(this.chatMemory, this.conversationId, this.order);
111140
}

0 commit comments

Comments
 (0)