Add message history ITs for few chat models

This commit is contained in:
Christian Tzolov
2024-09-10 12:08:57 +02:00
parent b5c7975fac
commit b22a577ebc
6 changed files with 120 additions and 2 deletions

View File

@@ -99,6 +99,25 @@ class AnthropicChatModelIT {
logger.info(response.toString());
}
@Test
void testMessageHistory() {
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage),
AnthropicChatOptions.builder().withModel("claude-3-sonnet-20240229").build());
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");
var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), response.getResult().getOutput(),
new UserMessage("Repeat the last assistant message.")));
response = chatModel.call(promptWithMessageHistory);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");
}
@Test
void streamingWithTokenUsage() {
var promptOptions = AnthropicChatOptions.builder().withTemperature(0f).build();

View File

@@ -82,6 +82,32 @@ class AzureOpenAiChatModelIT {
assertThat(response.getResult().getOutput().getContent()).contains("Blackbeard");
}
@Test
void testMessageHistory() {
Message systemMessage = new SystemPromptTemplate("""
You are a helpful AI assistant. Your name is {name}.
You are an AI assistant that helps people find information.
Your name is {name}
You should reply to the user's request with your name and also in the style of a {voice}.
""").createMessage(Map.of("name", "Bob", "voice", "pirate"));
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard");
var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), response.getResult().getOutput(),
new UserMessage("Repeat the last assistant message.")));
response = chatModel.call(promptWithMessageHistory);
System.out.println(response.getResult().getOutput().getContent());
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard");
}
@Test
void listOutputConverter() {
DefaultConversionService conversionService = new DefaultConversionService();

View File

@@ -27,7 +27,11 @@ import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.mistralai.api.MistralAiApi;
@@ -73,6 +77,32 @@ class MistralAiChatClientIT {
assertThat(response.getResults().get(0).getOutput().getContent()).contains("Blackbeard");
}
@Test
void testMessageHistory() {
// @formatter:off
ChatResponse response = ChatClient.create(chatModel).prompt()
.system(s -> s.text(systemTextResource)
.param("name", "Bob")
.param("voice", "pirate"))
.user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did")
.call()
.chatResponse();
// @formatter:on
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard");
// @formatter:off
response = ChatClient.create(chatModel).prompt()
.messages(List.of(new UserMessage("Dummy"), response.getResult().getOutput()))
.user("Repeat the last assistant message.")
.call()
.chatResponse();
// @formatter:on
logger.info("" + response);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard");
}
@Test
void listOutputConverterString() {
// @formatter:off

View File

@@ -101,6 +101,31 @@ class OllamaChatModelIT extends BaseOllamaIT {
}
@Test
void testMessageHistory() {
Message systemMessage = new SystemPromptTemplate("""
You are a helpful AI assistant. Your name is {name}.
You are an AI assistant that helps people find information.
Your name is {name}
You should reply to the user's request with your name and also in the style of a {voice}.
""").createMessage(Map.of("name", "Bob", "voice", "pirate"));
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Bonny");
var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), response.getResult().getOutput(),
new UserMessage("Repeat the last assistant message.")));
response = chatModel.call(promptWithMessageHistory);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Bonny");
}
@Test
void usageTest() {
Prompt prompt = new Prompt("Tell me a joke");

View File

@@ -84,6 +84,24 @@ class OpenAiChatModelIT extends AbstractIT {
// needs fine tuning... evaluateQuestionAndAnswer(request, response, false);
}
@Test
void testMessageHistory() {
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");
var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), response.getResult().getOutput(),
new UserMessage("Repeat the last assistant message.")));
response = chatModel.call(promptWithMessageHistory);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");
}
@Test
void streamCompletenessTest() throws InterruptedException {
UserMessage userMessage = new UserMessage(

View File

@@ -75,8 +75,8 @@ class VertexAiGeminiChatModelIT {
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");
var promptWithMessageHistory = new Prompt(List.of(prompt.getInstructions().get(0),
response.getResult().getOutput(), new UserMessage("What was the answer from the previous question?")));
var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), prompt.getInstructions().get(1),
response.getResult().getOutput(), new UserMessage("Repeat the last assistant message.")));
response = chatModel.call(promptWithMessageHistory);
assertThat(response.getResult().getOutput().getContent()).containsAnyOf("Blackbeard", "Bartholomew");