此版本仍在开发中,尚不被认为是稳定的。对于最新的快照版本,请使用 Spring AI 1.0.1! |
Mistral AI 聊天
Spring AI 支持 Mistral AI 的各种 AI 语言模型。您可以与 Mistral AI 语言模型交互,并创建基于 Mistral 模型的多语言对话助手。
Mistral AI 还提供与 OpenAI API 兼容的端点。 查看 OpenAI API 兼容性部分,了解如何使用 Spring AI OpenAI 集成与 Mistral 端点通信。 |
前提条件
您需要使用 Mistral AI 创建一个 API 才能访问 Mistral AI 语言模型。
在 Mistral AI 注册页面创建一个帐户,并在 API 密钥页面上生成Tokens。
Spring AI 项目定义了一个名为spring.ai.mistralai.api-key
您应该将API Key
从 console.mistral.ai 获得。
您可以在application.properties
文件:
spring.ai.mistralai.api-key=<your-mistralai-api-key>
为了在处理 API 密钥等敏感信息时增强安全性,您可以使用 Spring Expression Language (SpEL) 来引用自定义环境变量:
# In application.yml
spring:
ai:
mistralai:
api-key: ${MISTRALAI_API_KEY}
# In your environment or .env file
export MISTRALAI_API_KEY=<your-mistralai-api-key>
您还可以在应用程序代码中以编程方式设置此配置:
// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("MISTRALAI_API_KEY");
添加存储库和 BOM
Spring AI 工件发布在 Maven Central 和 Spring Snapshot 存储库中。 请参阅 Artifact Repositories 部分,将这些存储库添加到您的构建系统中。
为了帮助进行依赖管理,Spring AI 提供了 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
Spring AI 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明。 |
Spring AI 为 Mistral AI 聊天客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-mistral-ai</artifactId>
</dependency>
或 Gradlebuild.gradle
构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-mistral-ai'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry
用作属性前缀,用于配置 Mistral AI 聊天模型的重试机制。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.retry.max次尝试 |
重试尝试的最大次数。 |
10 |
spring.ai.retry.backoff.初始间隔 |
指数退避策略的初始睡眠持续时间。 |
2 秒。 |
spring.ai.retry.backoff.乘数 |
回退间隔乘数。 |
5 |
spring.ai.retry.backoff.max间隔 |
最大回退持续时间。 |
3分钟 |
spring.ai.retry.on-client-错误 |
如果为 false,则引发 NonTransientAiException,并且不要尝试重试 |
false |
spring.ai.retry.exclude-on-http-codes |
不应触发重试的 HTTP 状态代码列表(例如,引发 NonTransientAiException)。 |
empty |
spring.ai.retry.on-http-代码 |
应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。 |
empty |
连接属性
前缀spring.ai.mistralai
用作允许您连接到 OpenAI 的属性前缀。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.mistralai.base-url |
要连接到的 URL |
|
spring.ai.mistralai.api-key |
API 密钥 |
- |
配置属性
现在,通过前缀为 要启用,spring.ai.model.chat=mistral(默认启用) 要禁用,spring.ai.model.chat=none(或任何与 mistral 不匹配的值) 进行此更改是为了允许配置多个模型。 |
前缀spring.ai.mistralai.chat
是属性前缀,用于配置 Mistral AI 的聊天模型实现。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.mistralai.chat.enabled(已删除且不再有效) |
启用 Mistral AI 聊天模型。 |
true |
spring.ai.model.聊天 |
启用 Mistral AI 聊天模型。 |
米斯特拉尔 |
spring.ai.mistralai.chat.base-url |
可选的覆盖 |
- |
spring.ai.mistralai.chat.api-key |
可选的覆盖 |
- |
spring.ai.mistralai.chat.options.model |
这是要使用的 Mistral AI 聊天模型 |
|
spring.ai.mistralai.chat.options.temperature |
要使用的采样温度,用于控制生成的完成的表观创造力。较高的值将使输出更加随机,而较低的值将使结果更加集中和确定。不建议修改 |
0.8 |
spring.ai.mistralai.chat.options.maxTokens |
聊天补全时要生成的最大Tokens数。输入标记和生成标记的总长度受模型上下文长度的限制。 |
- |
spring.ai.mistralai.chat.options.safePrompt |
指示是否在所有对话之前注入安全提示。 |
false |
spring.ai.mistralai.chat.options.randomSeed |
此功能处于测试阶段。如果指定,我们的系统将尽最大努力确定性地采样,以便具有相同种子和参数的重复请求应返回相同的结果。 |
- |
spring.ai.mistralai.chat.options.stop |
如果检测到此Tokens,则停止生成。或者,如果在提供数组时检测到其中一个标记。 |
- |
spring.ai.mistralai.chat.options.topP |
温度采样的替代方法称为核采样,其中模型考虑具有top_p概率质量的标记的结果。因此,0.1 意味着仅考虑包含前 10% 概率质量的Tokens。我们通常建议更改此或 |
- |
spring.ai.mistralai.chat.options.response格式 |
指定模型必须输出的格式的对象。设置为 |
- |
spring.ai.mistralai.chat.options.tools |
模型可能调用的工具列表。目前,仅支持功能作为工具。使用它来提供模型可能为其生成 JSON 输入的函数列表。 |
- |
spring.ai.mistralai.chat.options.tool选择 |
控制模型调用的函数(如果有)。 |
- |
spring.ai.mistralai.chat.options.functions |
函数列表,由其名称标识,以便在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。 |
- |
spring.ai.mistralai.chat.options.function回调 |
Mistral AI 工具函数回调,用于向 ChatModel 注册。 |
- |
spring.ai.mistralai.chat.options.proxy-tool-calls |
如果为 true,则 Spring AI 不会在内部处理函数调用,而是将它们代理给客户端。然后是客户端负责处理函数调用,将它们分派到适当的函数,并返回结果。如果为 false(默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
false |
您可以覆盖通用的spring.ai.mistralai.base-url 和spring.ai.mistralai.api-key 对于ChatModel 和EmbeddingModel 实现。
这spring.ai.mistralai.chat.base-url 和spring.ai.mistralai.chat.api-key 属性(如果已设置)优先于公共属性。
如果您想对不同的模型和不同的模型端点使用不同的 Mistral AI 帐户,这将非常有用。 |
所有以spring.ai.mistralai.chat.options 可以通过将特定于请求的运行时选项添加到Prompt 叫。 |
运行时选项
MistralAiChatOptions.java提供模型配置,例如要使用的模型、温度、频率惩罚等。
启动时,可以使用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#builder() 创建的可移植 ChatOptions 实例。 |
函数调用
您可以使用MistralAiChatModel
并让 Mistral AI 模型智能地选择输出一个 JSON 对象,其中包含调用一个或多个已注册函数的参数。
这是一种将 LLM 功能与外部工具和 API 连接起来的强大技术。
阅读有关工具调用的更多信息。
模 态
多模态是指模型同时理解和处理来自各种来源的信息的能力,包括文本、图像、音频和其他数据格式。Mistral AI 支持文本和视觉模式。
视觉
提供视觉多模态支持的 Mistral AI 模型包括pixtral-large-latest
. 有关更多信息,请参阅视觉指南。
Mistral AI 用户消息 API 可以将 base64 编码的图像或图像 URL 列表与消息合并。Spring AI 的消息界面通过引入媒体类型促进了多模态 AI 模型。此类型包含有关消息中媒体附件的数据和详细信息,利用 Spring 的org.springframework.util.MimeType
和org.springframework.core.io.Resource
用于原始媒体数据。
下面是一个代码示例,摘自MistralAiChatModelIT.java
,说明用户文本与图像的融合。
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 that create an open structure, allowing the fruit to be visible from all angles. Inside the bowl, there are two yellow bananas resting on top of what appears to be a red apple. The bananas are slightly overripe, as indicated by the brown spots on their peels. The bowl has a metal ring at the top, likely to serve as a handle for carrying. The bowl is placed on a flat surface with a neutral-colored background that provides a clear view of the fruit inside.
OpenAI API 兼容性
Mistral 与 OpenAI API 兼容,您可以使用 Spring AI OpenAI 客户端与 Mistrial 对话。为此,您需要配置 Mistral AI 平台的 OpenAI 基本 URL: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=<YOUR MISTRAL API KEY
.
查看 MistralWithOpenAiChatModelIT.java 测试,了解在 Spring AI OpenAI 上使用 Mistral 的示例。
Samples控制器(自动配置)
创建一个新的 Spring Boot 项目,并将spring-ai-starter-model-mistral-ai
到你的 pom(或 gradle)依赖项。
添加一个application.properties
文件src/main/resources
目录以启用和配置 Mistral AI 聊天模型:
spring.ai.mistralai.api-key=YOUR_API_KEY
spring.ai.mistralai.chat.options.model=mistral-small
spring.ai.mistralai.chat.options.temperature=0.7
将api-key 使用您的 Mistral AI 凭据。 |
这将创建一个MistralAiChatModel
实现,您可以将其注入到类中。这是一个简单的示例@RestController
使用聊天模型生成文本的类。
@RestController
public class ChatController {
private final MistralAiChatModel chatModel;
@Autowired
public 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);
}
}
手动配置
MistralAiChatModel 实现了ChatModel
和StreamingChatModel
并使用低级 MistralAiApi 客户端连接到 Mistral AI 服务。
添加spring-ai-mistral-ai
对项目 Maven 的依赖pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mistral-ai</artifactId>
</dependency>
或 Gradlebuild.gradle
构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-mistral-ai'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
接下来,创建一个MistralAiChatModel
并将其用于文本生成:
var mistralAiApi = new MistralAiApi(System.getenv("MISTRAL_AI_API_KEY"));
var chatModel = new MistralAiChatModel(this.mistralAiApi, MistralAiChatOptions.builder()
.model(MistralAiApi.ChatModel.LARGE.getValue())
.temperature(0.4)
.maxTokens(200)
.build());
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
// Or with streaming responses
Flux<ChatResponse> response = this.chatModel.stream(
new Prompt("Generate the names of 5 famous pirates."));
这MistralAiChatOptions
提供聊天请求的配置信息。
这MistralAiChatOptions.Builder
是一个流利的期权生成器。
低级 MistralAiApi 客户端
MistralAiApi 为 Mistral AI API 提供的轻量级 Java 客户端。
下面是一个简单的代码片段,展示了如何以编程方式使用 API:
MistralAiApi mistralAiApi = new MistralAiApi(System.getenv("MISTRAL_AI_API_KEY"));
ChatCompletionMessage chatCompletionMessage =
new ChatCompletionMessage("Hello world", Role.USER);
// Sync request
ResponseEntity<ChatCompletion> response = this.mistralAiApi.chatCompletionEntity(
new ChatCompletionRequest(List.of(this.chatCompletionMessage), MistralAiApi.ChatModel.LARGE.getValue(), 0.8, false));
// Streaming request
Flux<ChatCompletionChunk> streamResponse = this.mistralAiApi.chatCompletionStream(
new ChatCompletionRequest(List.of(this.chatCompletionMessage), MistralAiApi.ChatModel.LARGE.getValue(), 0.8, true));
请关注MistralAiApi.java的 JavaDoc 以获取更多信息。
MistralAiApi 示例
-
MistralAiApiIT.java测试提供了一些如何使用轻量级库的一般示例。
-
PaymentStatusFunctionCallingIT.java测试演示如何使用低级 API 调用工具函数。 基于 Mistral AI 函数调用教程。