Skip to content

Commit 867154c

Browse files
committed
Changed ChatBot to ChatService with other related name changes
* PromptContext -> ChatServiceContext * Added PromptChange to ChatServiceContext to capture PromptTransformer changes * DefaultChatBot -> PromptTransformingChatService * DefaultStreamingChatBot -> StreamingPromptTransformingChatService * package name changes, chatbot->service and history->memory * Added fluent builders to a few PromptTransformer implementations * Add license headers
1 parent d610dd6 commit 867154c

34 files changed

+836
-642
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.openai.chat.chatbot;
17+
package org.springframework.ai.openai.chat.service;
1818

1919
import java.util.List;
2020

2121
import io.qdrant.client.QdrantClient;
2222
import io.qdrant.client.QdrantGrpcClient;
2323
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
24-
import org.springframework.ai.chat.chatbot.ChatBot;
24+
import org.springframework.ai.chat.service.ChatService;
25+
import org.springframework.ai.chat.service.StreamingChatService;
2526
import org.testcontainers.junit.jupiter.Container;
2627
import org.testcontainers.junit.jupiter.Testcontainers;
2728
import org.testcontainers.qdrant.QdrantContainer;
2829

29-
import org.springframework.ai.chat.chatbot.DefaultChatBot;
30-
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
31-
import org.springframework.ai.chat.chatbot.StreamingChatBot;
32-
import org.springframework.ai.chat.history.VectorStoreChatMemoryChatBotListener;
33-
import org.springframework.ai.chat.history.VectorStoreChatMemoryRetriever;
34-
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
35-
import org.springframework.ai.chat.history.SystemPromptChatMemoryAugmentor;
30+
import org.springframework.ai.chat.service.PromptTransformingChatService;
31+
import org.springframework.ai.chat.service.StreamingPromptTransformingChatService;
32+
import org.springframework.ai.chat.memory.VectorStoreChatMemoryChatServiceListener;
33+
import org.springframework.ai.chat.memory.VectorStoreChatMemoryRetriever;
34+
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer;
35+
import org.springframework.ai.chat.memory.SystemPromptChatMemoryAugmentor;
3636
import org.springframework.ai.embedding.EmbeddingClient;
3737
import org.springframework.ai.evaluation.BaseMemoryTest;
3838
import org.springframework.ai.evaluation.RelevancyEvaluator;
@@ -61,9 +61,9 @@ public class ChatMemoryLongTermSystemPromptIT extends BaseMemoryTest {
6161
static QdrantContainer qdrantContainer = new QdrantContainer("qdrant/qdrant:v1.9.2");
6262

6363
@Autowired
64-
public ChatMemoryLongTermSystemPromptIT(RelevancyEvaluator relevancyEvaluator, ChatBot chatBot,
65-
StreamingChatBot streamingChatBot) {
66-
super(relevancyEvaluator, chatBot, streamingChatBot);
64+
public ChatMemoryLongTermSystemPromptIT(RelevancyEvaluator relevancyEvaluator, ChatService chatService,
65+
StreamingChatService streamingChatService) {
66+
super(relevancyEvaluator, chatService, streamingChatService);
6767
}
6868

6969
@SpringBootConfiguration
@@ -98,26 +98,26 @@ public TokenCountEstimator tokenCountEstimator() {
9898
}
9999

100100
@Bean
101-
public ChatBot memoryChatBot(OpenAiChatClient chatClient, VectorStore vectorStore,
101+
public ChatService memoryChatService(OpenAiChatClient chatClient, VectorStore vectorStore,
102102
TokenCountEstimator tokenCountEstimator) {
103103

104-
return DefaultChatBot.builder(chatClient)
104+
return PromptTransformingChatService.builder(chatClient)
105105
.withRetrievers(List.of(new VectorStoreChatMemoryRetriever(vectorStore, 10)))
106106
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
107107
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
108-
.withChatBotListeners(List.of(new VectorStoreChatMemoryChatBotListener(vectorStore)))
108+
.withChatServiceListeners(List.of(new VectorStoreChatMemoryChatServiceListener(vectorStore)))
109109
.build();
110110
}
111111

112112
@Bean
113-
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, VectorStore vectorStore,
114-
TokenCountEstimator tokenCountEstimator) {
113+
public StreamingChatService memoryStreamingChatService(OpenAiChatClient streamingChatClient,
114+
VectorStore vectorStore, TokenCountEstimator tokenCountEstimator) {
115115

116-
return DefaultStreamingChatBot.builder(streamingChatClient)
116+
return StreamingPromptTransformingChatService.builder(streamingChatClient)
117117
.withRetrievers(List.of(new VectorStoreChatMemoryRetriever(vectorStore, 10)))
118118
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
119119
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
120-
.withChatBotListeners(List.of(new VectorStoreChatMemoryChatBotListener(vectorStore)))
120+
.withChatServiceListeners(List.of(new VectorStoreChatMemoryChatServiceListener(vectorStore)))
121121
.build();
122122
}
123123

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.ai.openai.chat.chatbot;
16+
package org.springframework.ai.openai.chat.service;
1717

1818
import java.util.List;
1919

2020
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2121

22-
import org.springframework.ai.chat.chatbot.ChatBot;
23-
import org.springframework.ai.chat.chatbot.DefaultChatBot;
24-
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
25-
import org.springframework.ai.chat.chatbot.StreamingChatBot;
26-
import org.springframework.ai.chat.history.ChatMemory;
27-
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
28-
import org.springframework.ai.chat.history.ChatMemoryRetriever;
29-
import org.springframework.ai.chat.history.InMemoryChatMemory;
30-
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
31-
import org.springframework.ai.chat.history.MessageChatMemoryAugmentor;
22+
import org.springframework.ai.chat.service.ChatService;
23+
import org.springframework.ai.chat.service.PromptTransformingChatService;
24+
import org.springframework.ai.chat.service.StreamingPromptTransformingChatService;
25+
import org.springframework.ai.chat.service.StreamingChatService;
26+
import org.springframework.ai.chat.memory.ChatMemory;
27+
import org.springframework.ai.chat.memory.ChatMemoryChatServiceListener;
28+
import org.springframework.ai.chat.memory.ChatMemoryRetriever;
29+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
30+
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer;
31+
import org.springframework.ai.chat.memory.MessageChatMemoryAugmentor;
3232
import org.springframework.ai.evaluation.BaseMemoryTest;
3333
import org.springframework.ai.evaluation.RelevancyEvaluator;
3434
import org.springframework.ai.openai.OpenAiChatClient;
@@ -45,9 +45,9 @@
4545
public class ChatMemoryShortTermMessageListIT extends BaseMemoryTest {
4646

4747
@Autowired
48-
public ChatMemoryShortTermMessageListIT(RelevancyEvaluator relevancyEvaluator, ChatBot chatBot,
49-
StreamingChatBot streamingChatBot) {
50-
super(relevancyEvaluator, chatBot, streamingChatBot);
48+
public ChatMemoryShortTermMessageListIT(RelevancyEvaluator relevancyEvaluator, ChatService chatService,
49+
StreamingChatService streamingChatService) {
50+
super(relevancyEvaluator, chatService, streamingChatService);
5151
}
5252

5353
@SpringBootConfiguration
@@ -74,26 +74,26 @@ public TokenCountEstimator tokenCountEstimator() {
7474
}
7575

7676
@Bean
77-
public ChatBot memoryChatBot(OpenAiChatClient chatClient, ChatMemory chatHistory,
77+
public ChatService memoryChatService(OpenAiChatClient chatClient, ChatMemory chatHistory,
7878
TokenCountEstimator tokenCountEstimator) {
7979

80-
return DefaultChatBot.builder(chatClient)
80+
return PromptTransformingChatService.builder(chatClient)
8181
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
8282
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
8383
.withAugmentors(List.of(new MessageChatMemoryAugmentor()))
84-
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
84+
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatHistory)))
8585
.build();
8686
}
8787

8888
@Bean
89-
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
90-
TokenCountEstimator tokenCountEstimator) {
89+
public StreamingChatService memoryStreamingChatService(OpenAiChatClient streamingChatClient,
90+
ChatMemory chatHistory, TokenCountEstimator tokenCountEstimator) {
9191

92-
return DefaultStreamingChatBot.builder(streamingChatClient)
92+
return StreamingPromptTransformingChatService.builder(streamingChatClient)
9393
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
9494
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
9595
.withAugmentors(List.of(new MessageChatMemoryAugmentor()))
96-
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
96+
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatHistory)))
9797
.build();
9898
}
9999

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.openai.chat.chatbot;
17+
package org.springframework.ai.openai.chat.service;
1818

1919
import java.util.List;
2020

2121
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2222

23-
import org.springframework.ai.chat.chatbot.ChatBot;
24-
import org.springframework.ai.chat.chatbot.DefaultChatBot;
25-
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
26-
import org.springframework.ai.chat.chatbot.StreamingChatBot;
27-
import org.springframework.ai.chat.history.ChatMemory;
28-
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
29-
import org.springframework.ai.chat.history.ChatMemoryRetriever;
30-
import org.springframework.ai.chat.history.InMemoryChatMemory;
31-
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
32-
import org.springframework.ai.chat.history.SystemPromptChatMemoryAugmentor;
23+
import org.springframework.ai.chat.service.ChatService;
24+
import org.springframework.ai.chat.service.PromptTransformingChatService;
25+
import org.springframework.ai.chat.service.StreamingPromptTransformingChatService;
26+
import org.springframework.ai.chat.service.StreamingChatService;
27+
import org.springframework.ai.chat.memory.ChatMemory;
28+
import org.springframework.ai.chat.memory.ChatMemoryChatServiceListener;
29+
import org.springframework.ai.chat.memory.ChatMemoryRetriever;
30+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
31+
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer;
32+
import org.springframework.ai.chat.memory.SystemPromptChatMemoryAugmentor;
3333
import org.springframework.ai.evaluation.BaseMemoryTest;
3434
import org.springframework.ai.evaluation.RelevancyEvaluator;
3535
import org.springframework.ai.openai.OpenAiChatClient;
@@ -46,9 +46,9 @@
4646
public class ChatMemoryShortTermSystemPromptIT extends BaseMemoryTest {
4747

4848
@Autowired
49-
public ChatMemoryShortTermSystemPromptIT(RelevancyEvaluator relevancyEvaluator, ChatBot chatBot,
50-
StreamingChatBot streamingChatBot) {
51-
super(relevancyEvaluator, chatBot, streamingChatBot);
49+
public ChatMemoryShortTermSystemPromptIT(RelevancyEvaluator relevancyEvaluator, ChatService chatService,
50+
StreamingChatService streamingChatService) {
51+
super(relevancyEvaluator, chatService, streamingChatService);
5252
}
5353

5454
@SpringBootConfiguration
@@ -75,26 +75,26 @@ public TokenCountEstimator tokenCountEstimator() {
7575
}
7676

7777
@Bean
78-
public ChatBot memoryChatBot(OpenAiChatClient chatClient, ChatMemory chatHistory,
78+
public ChatService memoryChatService(OpenAiChatClient chatClient, ChatMemory chatHistory,
7979
TokenCountEstimator tokenCountEstimator) {
8080

81-
return DefaultChatBot.builder(chatClient)
81+
return PromptTransformingChatService.builder(chatClient)
8282
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
8383
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
8484
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
85-
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
85+
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatHistory)))
8686
.build();
8787
}
8888

8989
@Bean
90-
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
91-
TokenCountEstimator tokenCountEstimator) {
90+
public StreamingChatService memoryStreamingChatService(OpenAiChatClient streamingChatClient,
91+
ChatMemory chatHistory, TokenCountEstimator tokenCountEstimator) {
9292

93-
return DefaultStreamingChatBot.builder(streamingChatClient)
93+
return StreamingPromptTransformingChatService.builder(streamingChatClient)
9494
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
9595
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
9696
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
97-
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
97+
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatHistory)))
9898
.build();
9999
}
100100

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.openai.chat.chatbot;
17+
package org.springframework.ai.openai.chat.service;
1818

1919
import java.util.List;
2020
import java.util.Map;
@@ -26,24 +26,24 @@
2626
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
29-
import org.springframework.ai.chat.chatbot.ChatBot;
29+
import org.springframework.ai.chat.prompt.transformer.ChatServiceContext;
30+
import org.springframework.ai.chat.service.ChatService;
31+
import org.springframework.ai.chat.service.PromptTransformingChatService;
3032
import org.springframework.ai.openai.OpenAiChatOptions;
3133
import org.testcontainers.junit.jupiter.Container;
3234
import org.testcontainers.junit.jupiter.Testcontainers;
3335
import org.testcontainers.qdrant.QdrantContainer;
3436

35-
import org.springframework.ai.chat.chatbot.DefaultChatBot;
36-
import org.springframework.ai.chat.history.ChatMemory;
37-
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
38-
import org.springframework.ai.chat.history.ChatMemoryRetriever;
39-
import org.springframework.ai.chat.history.InMemoryChatMemory;
40-
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
41-
import org.springframework.ai.chat.history.SystemPromptChatMemoryAugmentor;
42-
import org.springframework.ai.chat.history.VectorStoreChatMemoryChatBotListener;
43-
import org.springframework.ai.chat.history.VectorStoreChatMemoryRetriever;
37+
import org.springframework.ai.chat.memory.ChatMemory;
38+
import org.springframework.ai.chat.memory.ChatMemoryChatServiceListener;
39+
import org.springframework.ai.chat.memory.ChatMemoryRetriever;
40+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
41+
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer;
42+
import org.springframework.ai.chat.memory.SystemPromptChatMemoryAugmentor;
43+
import org.springframework.ai.chat.memory.VectorStoreChatMemoryChatServiceListener;
44+
import org.springframework.ai.chat.memory.VectorStoreChatMemoryRetriever;
4445
import org.springframework.ai.chat.messages.UserMessage;
4546
import org.springframework.ai.chat.prompt.Prompt;
46-
import org.springframework.ai.chat.prompt.transformer.PromptContext;
4747
import org.springframework.ai.chat.prompt.transformer.QuestionContextAugmentor;
4848
import org.springframework.ai.chat.prompt.transformer.TransformerContentType;
4949
import org.springframework.ai.chat.prompt.transformer.VectorStoreRetriever;
@@ -89,7 +89,7 @@ public class LongShortTermChatMemoryWithRagIT {
8989
static QdrantContainer qdrantContainer = new QdrantContainer("qdrant/qdrant:v1.9.2");
9090

9191
@Autowired
92-
ChatBot chatBot;
92+
ChatService chatService;
9393

9494
@Autowired
9595
RelevancyEvaluator relevancyEvaluator;
@@ -122,29 +122,29 @@ public List<Document> apply(List<Document> documents) {
122122
}
123123

124124
// @Autowired
125-
// StreamingChatBot streamingChatBot;
125+
// StreamingChatService streamingChatService;
126126

127127
@Test
128-
void memoryChatBot() {
128+
void memoryChatService() {
129129

130130
loadData();
131131

132132
var prompt = new Prompt(new UserMessage("My name is Christian and I like mountain bikes."));
133-
PromptContext promptContext = new PromptContext(prompt);
133+
ChatServiceContext chatServiceContext = new ChatServiceContext(prompt);
134134

135-
var chatBotResponse1 = this.chatBot.call(promptContext);
135+
var chatServiceResponse1 = this.chatService.call(chatServiceContext);
136136

137-
logger.info("Response1: " + chatBotResponse1.getChatResponse().getResult().getOutput().getContent());
137+
logger.info("Response1: " + chatServiceResponse1.getChatResponse().getResult().getOutput().getContent());
138138

139-
var chatBotResponse2 = this.chatBot.call(new PromptContext(
139+
var chatServiceResponse2 = this.chatService.call(new ChatServiceContext(
140140
new Prompt(new String("What is my name and what bike model would you suggest for me?"))));
141-
logger.info("Response2: " + chatBotResponse2.getChatResponse().getResult().getOutput().getContent());
141+
logger.info("Response2: " + chatServiceResponse2.getChatResponse().getResult().getOutput().getContent());
142142

143-
// logger.info(chatBotResponse2.getPromptContext().getContents().toString());
144-
assertThat(chatBotResponse2.getChatResponse().getResult().getOutput().getContent()).contains("Christian");
143+
// logger.info(chatServiceResponse2.getPromptContext().getContents().toString());
144+
assertThat(chatServiceResponse2.getChatResponse().getResult().getOutput().getContent()).contains("Christian");
145145

146146
EvaluationResponse evaluationResponse = this.relevancyEvaluator
147-
.evaluate(new EvaluationRequest(chatBotResponse2));
147+
.evaluate(new EvaluationRequest(chatServiceResponse2));
148148

149149
assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");
150150

@@ -187,12 +187,15 @@ public TokenCountEstimator tokenCountEstimator() {
187187
}
188188

189189
@Bean
190-
public ChatBot memoryChatBot(OpenAiChatClient chatClient, VectorStore vectorStore,
190+
public ChatService memoryChatService(OpenAiChatClient chatClient, VectorStore vectorStore,
191191
TokenCountEstimator tokenCountEstimator, ChatMemory chatHistory) {
192192

193-
return DefaultChatBot.builder(chatClient)
193+
return PromptTransformingChatService.builder(chatClient)
194194
.withRetrievers(List.of(new VectorStoreRetriever(vectorStore, SearchRequest.defaults()),
195-
new ChatMemoryRetriever(chatHistory, Map.of(TransformerContentType.SHORT_TERM_MEMORY, "")),
195+
ChatMemoryRetriever.builder()
196+
.withChatHistory(chatHistory)
197+
.withMetadata(Map.of(TransformerContentType.SHORT_TERM_MEMORY, ""))
198+
.build(),
196199
new VectorStoreChatMemoryRetriever(vectorStore, 10,
197200
Map.of(TransformerContentType.LONG_TERM_MEMORY, ""))))
198201

@@ -214,19 +217,19 @@ public ChatBot memoryChatBot(OpenAiChatClient chatClient, VectorStore vectorStor
214217
Set.of(TransformerContentType.LONG_TERM_MEMORY)),
215218
new SystemPromptChatMemoryAugmentor(Set.of(TransformerContentType.SHORT_TERM_MEMORY))))
216219

217-
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory),
218-
new VectorStoreChatMemoryChatBotListener(vectorStore,
220+
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatHistory),
221+
new VectorStoreChatMemoryChatServiceListener(vectorStore,
219222
Map.of(TransformerContentType.LONG_TERM_MEMORY, ""))))
220223
.build();
221224
}
222225

223226
// @Bean
224-
// public StreamingChatBot memoryStreamingChatAgent(OpenAiChatClient
227+
// public StreamingChatService memoryStreamingChatAgent(OpenAiChatClient
225228
// streamingChatClient,
226229
// VectorStore vectorStore, TokenCountEstimator tokenCountEstimator, ChatHistory
227230
// chatHistory) {
228231

229-
// return DefaultStreamingChatBot.builder(streamingChatClient)
232+
// return StreamingPromptTransformingChatService.builder(streamingChatClient)
230233
// .withRetrievers(List.of(new ChatHistoryRetriever(chatHistory), new
231234
// DocumentChatHistoryRetriever(vectorStore, 10)))
232235
// .withDocumentPostProcessors(List.of(new

0 commit comments

Comments
 (0)