Add overload methods for defaultSytem()/defaultUer() and system() methods.

Most ChatModel enums extend the new  ModelDescription interface.
   Add ChatClientTest mockito testing
This commit is contained in:
Christian Tzolov
2024-05-21 20:34:15 +02:00
parent d5c7e19e95
commit bc5c47b201
20 changed files with 288 additions and 93 deletions

View File

@@ -26,6 +26,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.HttpHeaders;
@@ -116,7 +117,7 @@ public class AnthropicApi {
* "https://docs.anthropic.com/claude/docs/models-overview#model-comparison">model
* comparison</a> for additional details and options.
*/
public enum ChatModel {
public enum ChatModel implements ModelDescription {
// @formatter:off
CLAUDE_3_OPUS("claude-3-opus-20240229"),
@@ -140,6 +141,11 @@ public class AnthropicApi {
return this.value;
}
@Override
public String getModelName() {
return this.value;
}
}
/**

View File

@@ -29,6 +29,7 @@ import software.amazon.awssdk.regions.Region;
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi.AnthropicChatRequest;
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi.AnthropicChatResponse;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.model.ModelDescription;
import org.springframework.util.Assert;
/**
@@ -225,7 +226,7 @@ public class AnthropicChatBedrockApi extends
/**
* Anthropic models version.
*/
public enum AnthropicChatModel {
public enum AnthropicChatModel implements ModelDescription {
/**
* anthropic.claude-instant-v1
*/
@@ -251,6 +252,11 @@ public class AnthropicChatBedrockApi extends
AnthropicChatModel(String value) {
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -23,6 +23,7 @@ import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.An
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatResponse;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.model.ModelDescription;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
@@ -436,7 +437,7 @@ public class Anthropic3ChatBedrockApi extends
/**
* Anthropic models version.
*/
public enum AnthropicChatModel {
public enum AnthropicChatModel implements ModelDescription {
/**
* anthropic.claude-instant-v1
@@ -476,6 +477,11 @@ public class Anthropic3ChatBedrockApi extends
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -30,6 +30,7 @@ import software.amazon.awssdk.regions.Region;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.cohere.api.CohereChatBedrockApi.CohereChatRequest;
import org.springframework.ai.bedrock.cohere.api.CohereChatBedrockApi.CohereChatResponse;
import org.springframework.ai.model.ModelDescription;
import org.springframework.util.Assert;
/**
@@ -366,7 +367,7 @@ public class CohereChatBedrockApi extends
/**
* Cohere models version.
*/
public enum CohereChatModel {
public enum CohereChatModel implements ModelDescription {
/**
* cohere.command-light-text-v14
@@ -390,6 +391,11 @@ public class CohereChatBedrockApi extends
CohereChatModel(String value) {
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -27,6 +27,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.jurassic2.api.Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatRequest;
import org.springframework.ai.bedrock.jurassic2.api.Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatResponse;
import org.springframework.ai.model.ModelDescription;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;
@@ -371,7 +372,7 @@ public class Ai21Jurassic2ChatBedrockApi extends
/**
* Ai21 Jurassic2 models version.
*/
public enum Ai21Jurassic2ChatModel {
public enum Ai21Jurassic2ChatModel implements ModelDescription {
/**
* ai21.j2-mid-v1
@@ -395,6 +396,11 @@ public class Ai21Jurassic2ChatBedrockApi extends
Ai21Jurassic2ChatModel(String value) {
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -26,6 +26,7 @@ import software.amazon.awssdk.regions.Region;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.llama.api.LlamaChatBedrockApi.LlamaChatRequest;
import org.springframework.ai.bedrock.llama.api.LlamaChatBedrockApi.LlamaChatResponse;
import org.springframework.ai.model.ModelDescription;
import java.time.Duration;
@@ -204,7 +205,7 @@ public class LlamaChatBedrockApi extends
/**
* Llama models version.
*/
public enum LlamaChatModel {
public enum LlamaChatModel implements ModelDescription {
/**
* meta.llama2-13b-chat-v1
@@ -238,6 +239,11 @@ public class LlamaChatBedrockApi extends
LlamaChatModel(String value) {
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -31,6 +31,7 @@ import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatReq
import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatResponse;
import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatResponse.CompletionReason;
import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatResponseChunk;
import org.springframework.ai.model.ModelDescription;
/**
* Java client for the Bedrock Titan chat model.
@@ -265,7 +266,7 @@ public class TitanChatBedrockApi extends
/**
* Titan models version.
*/
public enum TitanChatModel {
public enum TitanChatModel implements ModelDescription {
/**
* amazon.titan-text-lite-v1
@@ -294,6 +295,11 @@ public class TitanChatBedrockApi extends
TitanChatModel(String value) {
this.id = value;
}
@Override
public String getModelName() {
return this.id;
}
}
@Override

View File

@@ -19,6 +19,8 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@@ -111,7 +113,7 @@ public class MiniMaxApi {
* MiniMax Chat Completion Models:
* <a href="https://www.minimaxi.com/document/algorithm-concept">MiniMax Model</a>.
*/
public enum ChatModel {
public enum ChatModel implements ModelDescription {
ABAB_6_Chat("abab6-chat"),
ABAB_5_5_Chat("abab5.5-chat"),
ABAB_5_5_S_Chat("abab5.5s-chat");
@@ -125,6 +127,11 @@ public class MiniMaxApi {
public String getValue() {
return value;
}
@Override
public String getModelName() {
return this.value;
}
}
/**

View File

@@ -27,6 +27,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@@ -706,7 +707,7 @@ public class MistralAiApi {
* <li><b>LARGE</b> - mistral-large-latest (aka mistral-large-2402)</li>
* </ul>
*/
public enum ChatModel {
public enum ChatModel implements ModelDescription {
// @formatter:off
TINY("open-mistral-7b"),
@@ -726,6 +727,11 @@ public class MistralAiApi {
return this.value;
}
@Override
public String getModelName() {
return this.value;
}
}
/**

View File

@@ -15,13 +15,15 @@
*/
package org.springframework.ai.ollama.api;
import org.springframework.ai.model.ModelDescription;
/**
* Helper class for common Ollama models.
*
* @author Siarhei Blashuk
* @since 0.8.1
*/
public enum OllamaModel {
public enum OllamaModel implements ModelDescription {
/**
* Llama 2 is a collection of language models ranging from 7B to 70B parameters.
@@ -99,4 +101,9 @@ public enum OllamaModel {
return this.id;
}
@Override
public String getModelName() {
return this.id;
}
}

View File

@@ -26,6 +26,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@@ -113,7 +114,7 @@ public class OpenAiApi {
* - <a href="https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo">GPT-4 and GPT-4 Turbo</a>
* - <a href="https://platform.openai.com/docs/models/gpt-3-5-turbo">GPT-3.5 Turbo</a>.
*/
public enum ChatModel {
public enum ChatModel implements ModelDescription {
/**
* Multimodal flagship model thats cheaper and faster than GPT-4 Turbo.
* Currently points to gpt-4o-2024-05-13.
@@ -199,6 +200,11 @@ public class OpenAiApi {
public String getValue() {
return value;
}
@Override
public String getModelName() {
return this.value;
}
}
/**

View File

@@ -33,9 +33,8 @@ import reactor.core.publisher.Flux;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiTestConfiguration;
import org.springframework.ai.openai.api.OpenAiApi;
@@ -62,14 +61,17 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void roleTest() {
// @formatter:off
ChatResponse response = ChatClient.builder(modelCaller).build().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();
.call()
.chatResponse();
// @formatter:on
System.out.println(response);
logger.info("" + response);
// UserMessage userMessage = new UserMessage(
// "Tell me about 3 famous pirates from the Golden Age of Piracy and what they
// did.");
@@ -88,10 +90,13 @@ class OpenAiChatClientIT extends AbstractIT {
void listOutputConverter() {
// TODO: there is a problem here.
// @formatter:off
Collection<String> list = ChatClient.builder(modelCaller).build().prompt()
.user(u -> u.text("List five {subject}")
.param("subject", "ice cream flavors"))
.call().list(String.class);
.call()
.list(String.class);
// @formatter:on
// DefaultConversionService conversionService = new DefaultConversionService();
// ListOutputConverter outputConverter = new
@@ -115,12 +120,13 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void mapOutputConverter() {
// @formatter:off
Map<String, Object> result = ChatClient.builder(modelCaller).build().prompt()
.user(u -> u.text("Provide me a List of {subject}")
.param("subject", "an array of numbers from 1 to 9 under they key name 'numbers'"))
.call().single(new ParameterizedTypeReference<Map<String, Object>>() {
});
// @formatter:on
// MapOutputConverter outputConverter = new MapOutputConverter();
@@ -143,10 +149,12 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void beanOutputConverter() {
// @formatter:off
ActorsFilms actorsFilms = ChatClient.builder(modelCaller).build().prompt()
.user("Generate the filmography for a random actor.")
.call()
.single(ActorsFilms.class);
// @formatter:on
// BeanOutputConverter<ActorsFilms> outputConverter = new
// BeanOutputConverter<>(ActorsFilms.class);
@@ -173,10 +181,12 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void beanOutputConverterRecords() {
// @formatter:off
ActorsFilmsRecord actorsFilms = ChatClient.builder(modelCaller).build().prompt()
.user("Generate the filmography of 5 movies for Tom Hanks.")
.call()
.single(ActorsFilmsRecord.class);
// @formatter:on
// BeanOutputConverter<ActorsFilmsRecord> outputConverter = new
// BeanOutputConverter<>(ActorsFilmsRecord.class);
@@ -203,6 +213,7 @@ class OpenAiChatClientIT extends AbstractIT {
BeanOutputConverter<ActorsFilmsRecord> outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class);
// @formatter:off
Flux<String> chatResponse = ChatClient.builder(modelCaller)
.build()
.prompt()
@@ -217,6 +228,7 @@ class OpenAiChatClientIT extends AbstractIT {
.block()
.stream()
.collect(Collectors.joining());
// @formatter:on
// String generationTextFromStream = chatResponse.collectList()
// .block()
@@ -254,11 +266,13 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void functionCallTest() {
// @formatter:off
String response = ChatClient.builder(modelCaller).build().prompt()
.user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris?"))
.function("getCurrentWeather", "Get the weather in location", new MockWeatherService())
.call()
.content();
// @formatter:on
// UserMessage userMessage = new UserMessage("What's the weather like in San
// Francisco, Tokyo, and Paris?");
@@ -287,11 +301,13 @@ class OpenAiChatClientIT extends AbstractIT {
@Test
void streamFunctionCallTest() {
// @formatter:off
Flux<String> response = ChatClient.builder(modelCaller).build().prompt()
.user("What's the weather like in San Francisco, Tokyo, and Paris?")
.function("getCurrentWeather", "Get the weather in location", new MockWeatherService())
.stream()
.content();
// @formatter:on
// UserMessage userMessage = new UserMessage("What's the weather like in San
// Francisco, Tokyo, and Paris?");
@@ -311,10 +327,7 @@ class OpenAiChatClientIT extends AbstractIT {
// Flux<ChatResponse> response = streamingChatClient.stream(new Prompt(messages,
// promptOptions));
String content = response.collectList()
.block()
.stream()
.collect(Collectors.joining());
String content = response.collectList().block().stream().collect(Collectors.joining());
logger.info("Response: {}", content);
assertThat(content).containsAnyOf("30.0", "30");
@@ -326,6 +339,7 @@ class OpenAiChatClientIT extends AbstractIT {
@ValueSource(strings = { "gpt-4-vision-preview", "gpt-4o" })
void multiModalityEmbeddedImage(String modelName) throws IOException {
// @formatter:off
String response = ChatClient.builder(modelCaller).build().prompt()
// TODO consider adding model(...) method to ChatClient as a shortcut to
// OpenAiChatOptions.builder().withModel(modelName).build()
@@ -334,6 +348,7 @@ class OpenAiChatClientIT extends AbstractIT {
.media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource("/test.png")))
.call()
.content();
// @formatter:on
// var imageData = new ClassPathResource("/test.png");
@@ -356,6 +371,7 @@ class OpenAiChatClientIT extends AbstractIT {
// TODO: add url method that wrapps the checked exception.
URL url = new URL("https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/_images/multimodal.test.png");
// @formatter:off
String response = ChatClient.builder(modelCaller)
.build()
.prompt()
@@ -365,6 +381,7 @@ class OpenAiChatClientIT extends AbstractIT {
.user(u -> u.text("Explain what do you see on this picture?").media(MimeTypeUtils.IMAGE_PNG, url))
.call()
.content();
// @formatter:on
// var userMessage = new UserMessage("Explain what do you see on this picture?",
// List
@@ -387,6 +404,7 @@ class OpenAiChatClientIT extends AbstractIT {
// TODO: add url method that wrapps the checked exception.
URL url = new URL("https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/_images/multimodal.test.png");
// @formatter:off
Flux<String> response = ChatClient.builder(modelCaller).build().prompt()
.options(OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
.build())
@@ -394,6 +412,7 @@ class OpenAiChatClientIT extends AbstractIT {
.media(MimeTypeUtils.IMAGE_PNG, url))
.stream()
.content();
// @formatter:on
// var userMessage = new UserMessage("Explain what do you see on this picture?",
// List.of(new Media(MimeTypeUtils.IMAGE_PNG,

View File

@@ -42,6 +42,7 @@ import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.model.function.AbstractFunctionCallSupport;
import org.springframework.ai.model.function.FunctionCallbackContext;
@@ -95,7 +96,7 @@ public class VertexAiGeminiModelCaller
}
public enum ChatModel {
public enum ChatModel implements ModelDescription {
GEMINI_PRO_VISION("gemini-pro-vision"),
@@ -115,6 +116,11 @@ public class VertexAiGeminiModelCaller
return this.value;
}
@Override
public String getModelName() {
return this.value;
}
}
public VertexAiGeminiModelCaller(VertexAI vertexAI) {

View File

@@ -18,6 +18,8 @@ package org.springframework.ai.zhipuai.api;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.model.ModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@@ -110,7 +112,7 @@ public class ZhiPuAiApi {
* ZhiPuAI Chat Completion Models:
* <a href="https://open.bigmodel.cn/dev/howuse/model">ZhiPuAI Model</a>.
*/
public enum ChatModel {
public enum ChatModel implements ModelDescription {
GLM_4("GLM-4"),
GLM_3_Turbo("GLM-3-Turbo");
@@ -123,6 +125,11 @@ public class ZhiPuAiApi {
public String getValue() {
return value;
}
@Override
public String getModelName() {
return this.value;
}
}
/**