MistralAiChatOptions提供了模型配置,如要使用的模型、温度、频率惩罚等。
启动时,可以使用MistralAiChatModel(api, options)构造函数或spring.ai.mistralai.chat.options.*属性来配置默认选项。
在运行时,您可以通过向Prompt调用添加新的、特定于请求的选项来覆盖默认选项。例如,要为特定请求覆盖默认模型和温度:
ChatResponse response = chatModel.call(new Prompt("Generate the names of 5 famous pirates.",MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.LARGE.getValue()).temperature(0.5).build()));
除了特定于模型的MistralAiChatOptions之外,您还可以使用一个便携式的ChatOptions实例,该实例是通过ChatOptions#builder()创建的。
函数调用
您可以使用MistralAiChatModel注册自定义Java函数,并让Mistral AI模型智能地选择输出一个包含参数的JSON对象,以调用一个或多个已注册的函数。这是一种将大型语言模型(LLM)功能与外部工具和应用程序编程接口(API)连接起来的强大技术。
多模态
多模态指的是模型能够同时理解和处理来自不同来源的信息,包括文本、图像、音频和其他数据格式。Mistral AI支持文本和视觉模态。
视觉
提供视觉多模态支持的Mistral AI模型包括pixtral-large-latest。
Mistral AI用户消息API可以将base64编码的图像列表或图像URL与消息合并。Spring AI的消息接口通过引入Media类型来促进多模态AI模型的发展。此类型包含有关消息中媒体附件的数据和详细信息,利用Spring的org.springframework.util.MimeType和org.springframework.core.io.Resource来处理原始媒体数据。
以下是从MistralAiChatModelIT中摘录的一段代码示例,展示了如何将用户文本与图像融合。
var imageResource = new ClassPathResource("/multimodal.test.png");var userMessage = new UserMessage("Explain what do you see on this picture?",new Media(MimeTypeUtils.IMAGE_PNG, this.imageResource));ChatResponse response = chatModel.call(new Prompt(this.userMessage,ChatOptions.builder().model(MistralAiApi.ChatModel.PIXTRAL_LARGE.getValue()).build()));
或对应的图片URL:
var userMessage = new UserMessage("Explain what do you see on this picture?",new Media(MimeTypeUtils.IMAGE_PNG,URI.create("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png")));ChatResponse response = chatModel.call(new Prompt(this.userMessage,ChatOptions.builder().model(MistralAiApi.ChatModel.PIXTRAL_LARGE.getValue()).build()));
该示例展示了一个模型,该模型将multimodal.test.png图像作为输入:

随同信息“请描述你在这张图片上看到了什么?”,并生成如下回复:
This is an image of a fruit bowl with a simple design. The bowl is made of metal with curved wire edges thatcreate an open structure, allowing the fruit to be visible from all angles. Inside the bowl, there are twoyellow bananas resting on top of what appears to be a red apple. The bananas are slightly overripe, asindicated by the brown spots on their peels. The bowl has a metal ring at the top, likely to serve as a handlefor carrying. The bowl is placed on a flat surface with a neutral-colored background that provides a clearview of the fruit inside.
OpenAI API兼容性
Mistral与OpenAI API兼容,您可以使用Spring AI OpenAI客户端与Mistral进行交互。为此,您需要将OpenAI的基础URL配置到Mistral AI平台:spring.ai.openai.chat.base-url=https://api.mistral.ai,并选择一个Mistral模型:spring.ai.openai.chat.options.model=mistral-small-latest,并设置Mistral AI API密钥:spring.ai.openai.chat.api-key=<您的MISTRAL API密钥。
样本控制器(自动配置)
创建一个新的Spring Boot项目,并将spring-ai-starter-model-mistral-ai添加到您的pom(或gradle)依赖项中。
在src/main/resources目录下添加一个application.properties文件,以启用和配置Mistral AI聊天模型:
spring.ai.mistralai.api-key=YOUR_API_KEYspring.ai.mistralai.chat.options.model=mistral-smallspring.ai.mistralai.chat.options.temperature=0.7
将api-key替换为您的Mistral AI凭据。
这将创建一个MistralAiChatModel实现,你可以将其注入到你的类中。以下是一个使用聊天模型进行文本生成的简单@RestController类的示例。
@RestControllerpublic class ChatController {private final MistralAiChatModel chatModel;@Autowiredpublic ChatController(MistralAiChatModel chatModel) {this.chatModel = chatModel;}@GetMapping("/ai/generate")public Map<String,String> generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return Map.of("generation", this.chatModel.call(message));}@GetMapping("/ai/generateStream")public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {var prompt = new Prompt(new UserMessage(message));return this.chatModel.stream(prompt);}}