Rename Agent classes to use ChatBot

This commit is contained in:
Mark Pollack
2024-05-08 15:43:00 -04:00
parent dfb8bf6a44
commit ad527303ed
11 changed files with 72 additions and 72 deletions

View File

@@ -29,7 +29,7 @@ import org.testcontainers.qdrant.QdrantContainer;
import org.springframework.ai.chat.chatbot.DefaultChatBot;
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
import org.springframework.ai.chat.chatbot.StreamingChatBot;
import org.springframework.ai.chat.history.VectorStoreChatMemoryAgentListener;
import org.springframework.ai.chat.history.VectorStoreChatMemoryChatBotListener;
import org.springframework.ai.chat.history.VectorStoreChatMemoryRetriever;
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
import org.springframework.ai.chat.history.SystemPromptChatMemoryAugmentor;
@@ -98,26 +98,26 @@ public class ChatMemoryLongTermSystemPromptIT extends BaseMemoryTest {
}
@Bean
public ChatBot memoryChatAgent(OpenAiChatClient chatClient, VectorStore vectorStore,
public ChatBot memoryChatBot(OpenAiChatClient chatClient, VectorStore vectorStore,
TokenCountEstimator tokenCountEstimator) {
return DefaultChatBot.builder(chatClient)
.withRetrievers(List.of(new VectorStoreChatMemoryRetriever(vectorStore, 10)))
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new VectorStoreChatMemoryAgentListener(vectorStore)))
.withChatBotListeners(List.of(new VectorStoreChatMemoryChatBotListener(vectorStore)))
.build();
}
@Bean
public StreamingChatBot memoryStreamingChatAgent(OpenAiChatClient streamingChatClient, VectorStore vectorStore,
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, VectorStore vectorStore,
TokenCountEstimator tokenCountEstimator) {
return DefaultStreamingChatBot.builder(streamingChatClient)
.withRetrievers(List.of(new VectorStoreChatMemoryRetriever(vectorStore, 10)))
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new VectorStoreChatMemoryAgentListener(vectorStore)))
.withChatBotListeners(List.of(new VectorStoreChatMemoryChatBotListener(vectorStore)))
.build();
}

View File

@@ -24,7 +24,7 @@ import org.springframework.ai.chat.chatbot.DefaultChatBot;
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
import org.springframework.ai.chat.chatbot.StreamingChatBot;
import org.springframework.ai.chat.history.ChatMemory;
import org.springframework.ai.chat.history.ChatMemoryAgentListener;
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
import org.springframework.ai.chat.history.ChatMemoryRetriever;
import org.springframework.ai.chat.history.InMemoryChatMemory;
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
@@ -74,26 +74,26 @@ public class ChatMemoryShortTermMessageListIT extends BaseMemoryTest {
}
@Bean
public ChatBot memoryChatAgent(OpenAiChatClient chatClient, ChatMemory chatHistory,
public ChatBot memoryChatBot(OpenAiChatClient chatClient, ChatMemory chatHistory,
TokenCountEstimator tokenCountEstimator) {
return DefaultChatBot.builder(chatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new MessageChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
}
@Bean
public StreamingChatBot memoryStreamingChatAgent(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
TokenCountEstimator tokenCountEstimator) {
return DefaultStreamingChatBot.builder(streamingChatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new MessageChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
}

View File

@@ -25,7 +25,7 @@ import org.springframework.ai.chat.chatbot.DefaultChatBot;
import org.springframework.ai.chat.chatbot.DefaultStreamingChatBot;
import org.springframework.ai.chat.chatbot.StreamingChatBot;
import org.springframework.ai.chat.history.ChatMemory;
import org.springframework.ai.chat.history.ChatMemoryAgentListener;
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
import org.springframework.ai.chat.history.ChatMemoryRetriever;
import org.springframework.ai.chat.history.InMemoryChatMemory;
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
@@ -75,26 +75,26 @@ public class ChatMemoryShortTermSystemPromptIT extends BaseMemoryTest {
}
@Bean
public ChatBot memoryChatAgent(OpenAiChatClient chatClient, ChatMemory chatHistory,
public ChatBot memoryChatBot(OpenAiChatClient chatClient, ChatMemory chatHistory,
TokenCountEstimator tokenCountEstimator) {
return DefaultChatBot.builder(chatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
}
@Bean
public StreamingChatBot memoryStreamingChatAgent(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
public StreamingChatBot memoryStreamingChatBot(OpenAiChatClient streamingChatClient, ChatMemory chatHistory,
TokenCountEstimator tokenCountEstimator) {
return DefaultStreamingChatBot.builder(streamingChatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withDocumentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000)))
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
}

View File

@@ -34,12 +34,12 @@ import org.testcontainers.qdrant.QdrantContainer;
import org.springframework.ai.chat.chatbot.DefaultChatBot;
import org.springframework.ai.chat.history.ChatMemory;
import org.springframework.ai.chat.history.ChatMemoryAgentListener;
import org.springframework.ai.chat.history.ChatMemoryChatBotListener;
import org.springframework.ai.chat.history.ChatMemoryRetriever;
import org.springframework.ai.chat.history.InMemoryChatMemory;
import org.springframework.ai.chat.history.LastMaxTokenSizeContentTransformer;
import org.springframework.ai.chat.history.SystemPromptChatMemoryAugmentor;
import org.springframework.ai.chat.history.VectorStoreChatMemoryAgentListener;
import org.springframework.ai.chat.history.VectorStoreChatMemoryChatBotListener;
import org.springframework.ai.chat.history.VectorStoreChatMemoryRetriever;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
@@ -187,7 +187,7 @@ public class LongShortTermChatMemoryWithRagIT {
}
@Bean
public ChatBot memoryChatAgent(OpenAiChatClient chatClient, VectorStore vectorStore,
public ChatBot memoryChatBot(OpenAiChatClient chatClient, VectorStore vectorStore,
TokenCountEstimator tokenCountEstimator, ChatMemory chatHistory) {
return DefaultChatBot.builder(chatClient)
@@ -214,8 +214,8 @@ public class LongShortTermChatMemoryWithRagIT {
Set.of(TransformerContentType.LONG_TERM_MEMORY)),
new SystemPromptChatMemoryAugmentor(Set.of(TransformerContentType.SHORT_TERM_MEMORY))))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory),
new VectorStoreChatMemoryAgentListener(vectorStore,
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory),
new VectorStoreChatMemoryChatBotListener(vectorStore,
Map.of(TransformerContentType.LONG_TERM_MEMORY, ""))))
.build();
}

View File

@@ -19,13 +19,13 @@ package org.springframework.ai.chat.chatbot;
import org.springframework.ai.chat.prompt.transformer.PromptContext;
/**
* The ChatAgentListener is a callback interface that can be implemented by classes that
* The ChatBotListener is a callback interface that can be implemented by classes that
* want to be notified of the completion of a ChatBot execution.
*
* @author Mark Pollack
* @author Christian Tzolov
*/
public interface ChatAgentListener {
public interface ChatBotListener {
default void onStart(PromptContext promptContext) {

View File

@@ -38,21 +38,21 @@ public class DefaultChatBot implements ChatBot {
private List<PromptTransformer> augmentors;
private List<ChatAgentListener> chatAgentListeners;
private List<ChatBotListener> chatBotListeners;
public DefaultChatBot(ChatClient chatClient, List<PromptTransformer> retrievers,
List<PromptTransformer> documentPostProcessors, List<PromptTransformer> augmentors,
List<ChatAgentListener> chatAgentListeners) {
List<ChatBotListener> chatBotListeners) {
Objects.requireNonNull(chatClient, "chatClient must not be null");
this.chatClient = chatClient;
this.retrievers = retrievers;
this.documentPostProcessors = documentPostProcessors;
this.augmentors = augmentors;
this.chatAgentListeners = chatAgentListeners;
this.chatBotListeners = chatBotListeners;
}
public static DefaultChatAgentBuilder builder(ChatClient chatClient) {
return new DefaultChatAgentBuilder().withChatClient(chatClient);
public static DefaultChatBotBuilder builder(ChatClient chatClient) {
return new DefaultChatBotBuilder().withChatClient(chatClient);
}
@Override
@@ -76,7 +76,7 @@ public class DefaultChatBot implements ChatBot {
}
// Invoke Listeners onStart
for (ChatAgentListener listener : this.chatAgentListeners) {
for (ChatBotListener listener : this.chatBotListeners) {
listener.onStart(promptContextOnStart);
}
@@ -85,13 +85,13 @@ public class DefaultChatBot implements ChatBot {
// Invoke Listeners onComplete
ChatBotResponse chatBotResponse = new ChatBotResponse(promptContext, chatResponse);
for (ChatAgentListener listener : this.chatAgentListeners) {
for (ChatBotListener listener : this.chatBotListeners) {
listener.onComplete(chatBotResponse);
}
return chatBotResponse;
}
public static class DefaultChatAgentBuilder {
public static class DefaultChatBotBuilder {
private ChatClient chatClient;
@@ -101,35 +101,35 @@ public class DefaultChatBot implements ChatBot {
private List<PromptTransformer> augmentors = new ArrayList<>();
private List<ChatAgentListener> chatAgentListeners = new ArrayList<>();
private List<ChatBotListener> chatBotListeners = new ArrayList<>();
public DefaultChatAgentBuilder withChatClient(ChatClient chatClient) {
public DefaultChatBotBuilder withChatClient(ChatClient chatClient) {
this.chatClient = chatClient;
return this;
}
public DefaultChatAgentBuilder withRetrievers(List<PromptTransformer> retrievers) {
public DefaultChatBotBuilder withRetrievers(List<PromptTransformer> retrievers) {
this.retrievers = retrievers;
return this;
}
public DefaultChatAgentBuilder withContentPostProcessors(List<PromptTransformer> documentPostProcessors) {
public DefaultChatBotBuilder withContentPostProcessors(List<PromptTransformer> documentPostProcessors) {
this.documentPostProcessors = documentPostProcessors;
return this;
}
public DefaultChatAgentBuilder withAugmentors(List<PromptTransformer> augmentors) {
public DefaultChatBotBuilder withAugmentors(List<PromptTransformer> augmentors) {
this.augmentors = augmentors;
return this;
}
public DefaultChatAgentBuilder withChatAgentListeners(List<ChatAgentListener> chatAgentListeners) {
this.chatAgentListeners = chatAgentListeners;
public DefaultChatBotBuilder withChatBotListeners(List<ChatBotListener> chatBotListeners) {
this.chatBotListeners = chatBotListeners;
return this;
}
public DefaultChatBot build() {
return new DefaultChatBot(chatClient, retrievers, documentPostProcessors, augmentors, chatAgentListeners);
return new DefaultChatBot(chatClient, retrievers, documentPostProcessors, augmentors, chatBotListeners);
}
}

View File

@@ -41,21 +41,21 @@ public class DefaultStreamingChatBot implements StreamingChatBot {
private List<PromptTransformer> augmentors;
private List<ChatAgentListener> chatAgentListeners;
private List<ChatBotListener> chatBotListeners;
public DefaultStreamingChatBot(StreamingChatClient chatClient, List<PromptTransformer> retrievers,
List<PromptTransformer> documentPostProcessors, List<PromptTransformer> augmentors,
List<ChatAgentListener> chatAgentListeners) {
List<ChatBotListener> chatBotListeners) {
Objects.requireNonNull(chatClient, "chatClient must not be null");
this.streamingChatClient = chatClient;
this.retrievers = retrievers;
this.documentPostProcessors = documentPostProcessors;
this.augmentors = augmentors;
this.chatAgentListeners = chatAgentListeners;
this.chatBotListeners = chatBotListeners;
}
public static DefaultChatAgentBuilder builder(StreamingChatClient chatClient) {
return new DefaultChatAgentBuilder().withChatClient(chatClient);
public static DefaultChatBotBuilder builder(StreamingChatClient chatClient) {
return new DefaultChatBotBuilder().withChatClient(chatClient);
}
@Override
@@ -79,7 +79,7 @@ public class DefaultStreamingChatBot implements StreamingChatBot {
}
// Invoke Listeners onStart
for (ChatAgentListener listener : this.chatAgentListeners) {
for (ChatBotListener listener : this.chatBotListeners) {
listener.onStart(promptContextOnStart);
}
@@ -88,7 +88,7 @@ public class DefaultStreamingChatBot implements StreamingChatBot {
Flux<ChatResponse> fluxChatResponse = new MessageAggregator()
.aggregate(this.streamingChatClient.stream(promptContext.getPrompt()), chatResponse -> {
for (ChatAgentListener listener : this.chatAgentListeners) {
for (ChatBotListener listener : this.chatBotListeners) {
listener.onComplete(new ChatBotResponse(promptContext2, chatResponse));
}
});
@@ -97,7 +97,7 @@ public class DefaultStreamingChatBot implements StreamingChatBot {
return new StreamingChatBotResponse(promptContext, fluxChatResponse);
}
public static class DefaultChatAgentBuilder {
public static class DefaultChatBotBuilder {
private StreamingChatClient chatClient;
@@ -107,36 +107,36 @@ public class DefaultStreamingChatBot implements StreamingChatBot {
private List<PromptTransformer> augmentors = new ArrayList<>();
private List<ChatAgentListener> chatAgentListeners = new ArrayList<>();
private List<ChatBotListener> chatBotListeners = new ArrayList<>();
public DefaultChatAgentBuilder withChatClient(StreamingChatClient chatClient) {
public DefaultChatBotBuilder withChatClient(StreamingChatClient chatClient) {
this.chatClient = chatClient;
return this;
}
public DefaultChatAgentBuilder withRetrievers(List<PromptTransformer> retrievers) {
public DefaultChatBotBuilder withRetrievers(List<PromptTransformer> retrievers) {
this.retrievers = retrievers;
return this;
}
public DefaultChatAgentBuilder withDocumentPostProcessors(List<PromptTransformer> documentPostProcessors) {
public DefaultChatBotBuilder withDocumentPostProcessors(List<PromptTransformer> documentPostProcessors) {
this.documentPostProcessors = documentPostProcessors;
return this;
}
public DefaultChatAgentBuilder withAugmentors(List<PromptTransformer> augmentors) {
public DefaultChatBotBuilder withAugmentors(List<PromptTransformer> augmentors) {
this.augmentors = augmentors;
return this;
}
public DefaultChatAgentBuilder withChatAgentListeners(List<ChatAgentListener> chatAgentListeners) {
this.chatAgentListeners = chatAgentListeners;
public DefaultChatBotBuilder withChatBotListeners(List<ChatBotListener> chatBotListeners) {
this.chatBotListeners = chatBotListeners;
return this;
}
public DefaultStreamingChatBot build() {
return new DefaultStreamingChatBot(chatClient, retrievers, documentPostProcessors, augmentors,
chatAgentListeners);
chatBotListeners);
}
}

View File

@@ -19,7 +19,7 @@ package org.springframework.ai.chat.history;
import java.util.List;
import org.springframework.ai.chat.chatbot.ChatBotResponse;
import org.springframework.ai.chat.chatbot.ChatAgentListener;
import org.springframework.ai.chat.chatbot.ChatBotListener;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.prompt.transformer.PromptContext;
@@ -28,11 +28,11 @@ import org.springframework.ai.chat.prompt.transformer.TransformerContentType;
/**
* @author Christian Tzolov
*/
public class ChatMemoryAgentListener implements ChatAgentListener {
public class ChatMemoryChatBotListener implements ChatBotListener {
private final ChatMemory chatHistory;
public ChatMemoryAgentListener(ChatMemory chatHistory) {
public ChatMemoryChatBotListener(ChatMemory chatHistory) {
this.chatHistory = chatHistory;
}

View File

@@ -20,8 +20,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.ai.chat.chatbot.ChatBotListener;
import org.springframework.ai.chat.chatbot.ChatBotResponse;
import org.springframework.ai.chat.chatbot.ChatAgentListener;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.prompt.transformer.TransformerContentType;
@@ -33,17 +33,17 @@ import org.springframework.util.CollectionUtils;
/**
* @author Christian Tzolov
*/
public class VectorStoreChatMemoryAgentListener implements ChatAgentListener {
public class VectorStoreChatMemoryChatBotListener implements ChatBotListener {
private final VectorStore vectorStore;
private final Map<String, Object> additionalMetadata;
public VectorStoreChatMemoryAgentListener(VectorStore vectorStore) {
public VectorStoreChatMemoryChatBotListener(VectorStore vectorStore) {
this(vectorStore, new HashMap<>());
}
public VectorStoreChatMemoryAgentListener(VectorStore vectorStore, Map<String, Object> additionalMetadata) {
public VectorStoreChatMemoryChatBotListener(VectorStore vectorStore, Map<String, Object> additionalMetadata) {
this.vectorStore = vectorStore;
this.additionalMetadata = additionalMetadata;
}

View File

@@ -61,15 +61,15 @@ public class ChatMemoryTests {
ChatMemory chatHistory = new InMemoryChatMemory();
DefaultChatBot chatAgent = DefaultChatBot.builder(chatClient)
DefaultChatBot chatBot = DefaultChatBot.builder(chatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withContentPostProcessors(
List.of(new LastMaxTokenSizeContentTransformer(new JTokkitTokenCountEstimator(), 10)))
.withAugmentors(List.of(new MessageChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
chatClientUserMessages(chatAgent, chatHistory);
chatClientUserMessages(chatBot, chatHistory);
}
@Test
@@ -77,18 +77,18 @@ public class ChatMemoryTests {
ChatMemory chatHistory = new InMemoryChatMemory();
DefaultChatBot chatAgent = DefaultChatBot.builder(chatClient)
DefaultChatBot chatBot = DefaultChatBot.builder(chatClient)
.withRetrievers(List.of(new ChatMemoryRetriever(chatHistory)))
.withContentPostProcessors(
List.of(new LastMaxTokenSizeContentTransformer(new JTokkitTokenCountEstimator(), 10)))
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor()))
.withChatAgentListeners(List.of(new ChatMemoryAgentListener(chatHistory)))
.withChatBotListeners(List.of(new ChatMemoryChatBotListener(chatHistory)))
.build();
chatClientUserMessages(chatAgent, chatHistory);
chatClientUserMessages(chatBot, chatHistory);
}
public void chatClientUserMessages(DefaultChatBot chatAgent, ChatMemory chatHistory) {
public void chatClientUserMessages(DefaultChatBot chatBot, ChatMemory chatHistory) {
when(chatClient.call(promptCaptor.capture()))
.thenReturn(new ChatResponse(List.of(new Generation("assistant:1"))))
@@ -102,7 +102,7 @@ public class ChatMemoryTests {
new UserMessage("user:4"), new UserMessage("user:5"))))
.build();
ChatBotResponse response1 = chatAgent.call(promptContext);
ChatBotResponse response1 = chatBot.call(promptContext);
assertThat(response1.getChatResponse().getResult().getOutput().getContent()).isEqualTo("assistant:1");
@@ -112,7 +112,7 @@ public class ChatMemoryTests {
List<Message> history = chatHistory.get("test-session-id", 1000);
assertThat(history).hasSize(6);
ChatBotResponse response2 = chatAgent.call(PromptContext.builder()
ChatBotResponse response2 = chatBot.call(PromptContext.builder()
.withConversationId("test-session-id")
.withPrompt(new Prompt(
List.of(new UserMessage("user:6"), new UserMessage("user:7"), new UserMessage("user:8"))))
@@ -129,7 +129,7 @@ public class ChatMemoryTests {
assertThat(contents.get(1).getContent()).isEqualTo("user:5");
assertThat(contents.get(2).getContent()).isEqualTo("assistant:1");
ChatBotResponse response3 = chatAgent.call(PromptContext.builder()
ChatBotResponse response3 = chatBot.call(PromptContext.builder()
.withConversationId("test-session-id")
.withPrompt(new Prompt(List.of(new UserMessage("user:9")))).build());
assertThat(response3.getChatResponse().getResult().getOutput().getContent()).isEqualTo("assistant:3");

View File

@@ -51,7 +51,7 @@ public class BaseMemoryTest {
}
@Test
void memoryChatAgent() {
void memoryChatBot() {
var prompt = new Prompt(new UserMessage("my name John Vincent Atanasoff"));
PromptContext promptContext = new PromptContext(prompt);