|
最新快照版本请使用Spring AI 1.1.0! |
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 密钥摘自 console.mistral.ai。
你可以在你的application.properties文件:
spring.ai.mistralai.api-key=<your-mistralai-api-key>
为了在处理敏感信息如 API 密钥时增强安全性,您可以使用 Spring 表达式语言(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");
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 Mistral AI 聊天客户端提供 Spring Boot 自动配置。
要启用它,请在项目的Maven中添加以下依赖pom.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的物料清单添加到你的构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry作为属性前缀,允许你配置Mistral AI聊天模型的重试机制。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.retry.max尝试 |
重试次数上限。 |
10 |
spring.ai.retry.backoff.initial-interval |
指数式退避政策的初始睡眠时长。 |
两秒钟。 |
spring.ai.retry.backoff.multiplier |
后退间隔倍数。 |
5 |
spring.ai.retry.backoff.max区间 |
最大退回时间。 |
3分钟。 |
spring.ai.retry.on-client-errors |
如果为假,抛出非瞬态AiException,且不要尝试重试 |
false |
spring.ai.retry.exclude-on-http-codes |
不应该触发重试的HTTP状态码列表(例如抛出非瞬态Ai异常)。 |
empty |
spring.ai.retry.on-http-codes |
应触发重试的HTTP状态码列表(例如抛出TransientAiException)。 |
empty |
连接性质
前缀春.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 不匹配的值) 此改动旨在允许配置多个模型。 |
前缀春.ai.mistralai.chat是允许你配置Mistral AI聊天模型实现的属性前缀。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.mistralai.chat.enabled(已移除且不再有效) |
启用Mistral AI聊天模型。 |
true |
spring.ai.model.chat |
启用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.safe提示 |
提示是否在所有对话前注入安全提示。 |
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.toolChoice |
控制模型调用的函数(如果有的话)。 |
- |
spring.ai.mistralai.chat.options.functions |
函数列表,以名称标识,以支持函数调用一次提示请求。带有这些名称的函数必须存在于 functionCallbacks 注册表中。 |
- |
spring.ai.mistralai.chat.options.functionCallbacks |
Mistral AI 工具功能回调以向 ChatModel 注册。 |
- |
spring.ai.mistralai.chat.options.proxy-tool-calls |
如果属实,Spring AI不会在内部处理函数调用,而是会代理给客户端。那么,客户端负责处理函数调用,将其派遣到相应的函数,并返回结果。如果 false(默认),Spring AI 会在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
false |
你可以覆盖公共信息Spring.ai.mistralai.base-url和spring.ai.mistralai.api-key对于聊天模型和嵌入模型实现。
这spring.ai.mistralai.chat.base-url和spring.ai.mistralai.chat.api-key如果属性被设置,则优先于共同属性。
如果你想为不同模型和不同模型端点使用不同的Mistral AI账户,这非常有用。 |
所有以spring.ai.mistralai.chat.optionss可以通过在运行时为 Run 添加请求特定的运行时选项来覆盖提示叫。 |
运行时选项
MistralAiChatOptions.java提供模型配置,如所用模型、温度、频率惩罚等。
启动时,默认选项可以配置为MistralAiChatModel(api, options)构造者或spring.ai.mistralai.chat.options.*性能。
运行时,您可以通过添加新的、针对特定请求的选项来覆盖默认选项提示叫。
例如,要覆盖特定请求的默认型号和温度:
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 编码的图片或图片链接列表与消息一同整合。
Spring AI 的 Message 界面通过引入媒体类型,促进了多模态 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 沟通。
为此,你需要配置 OpenAI 的 Mistral AI 平台的基础 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测试,了解如何使用Mistral替代Spring AI OpenAI的示例。
采样控制器(自动配置)
创建一个新的 Spring Boot 项目并添加Spring-ai-starter-model-mistral-ai对你的POM(或Gradle)依赖。
添加一个application.properties归档于以下src/主/资源用于启用和配置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密钥用你的密斯特拉尔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 实现了聊天模型和StreamingChatModel并使用低级 MistralAiApi 客户端连接 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的物料清单添加到你的构建文件中。 |
接下来,创建一个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函数调用教程。