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 密钥页面生成令牌。
Spring AI 项目定义了一个名为spring.ai.mistralai.api-key
,您应该设置为API Key
从 console.mistral.ai 获得。
导出环境变量是设置该配置属性的一种方法:
export SPRING_AI_MISTRALAI_API_KEY=<INSERT KEY HERE>
添加存储库和 BOM
Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。 请参阅 Repositories 部分,将这些存储库添加到您的构建系统中。
为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
Spring AI 为 Mistral AI Chat 客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-mistral-ai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry
用作属性前缀,允许您为 Mistral AI 聊天模型配置重试机制。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.retry.max 次尝试 |
最大重试尝试次数。 |
10 |
spring.ai.retry.backoff.initial-interval |
指数回退策略的初始休眠持续时间。 |
2 秒 |
spring.ai.retry.backoff.multiplier |
Backoff interval 乘数。 |
5 |
spring.ai.retry.backoff.max 间隔 |
最大回退持续时间。 |
3 分钟 |
spring.ai.retry.on-client-errors |
如果为 false,则抛出 NonTransientAiException,并且不要尝试重试 |
假 |
spring.ai.retry.exclude-on-http-codes |
不应触发重试的 HTTP 状态代码列表(例如,引发 NonTransientAiException)。 |
空 |
spring.ai.retry.on-http-codes |
应触发重试的 HTTP 状态代码列表(例如,引发 TransientAiException)。 |
空 |
连接属性
前缀spring.ai.mistralai
用作允许您连接到 OpenAI 的属性前缀。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.mistralai.base-url |
要连接到的 URL |
|
spring.ai.mistralai.api-key |
API 密钥 |
- |
配置属性
前缀spring.ai.mistralai.chat
是允许您为 Mistral AI 配置聊天模型实现的属性前缀。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.mistralai.chat.enabled |
启用 Mistral AI 聊天模型。 |
真 |
spring.ai.mistralai.chat.base-url |
的可选覆盖 |
- |
spring.ai.mistralai.chat.api-key |
的可选覆盖 |
- |
spring.ai.mistralai.chat.options.model |
这是要使用的 Mistral AI Chat 模型 |
|
spring.ai.mistralai.chat.options.temperature |
要使用的采样温度,用于控制生成的完成项的明显创造性。较高的值将使输出更具随机性,而较低的值将使结果更加集中和确定。不建议修改 |
0.8 |
spring.ai.mistralai.chat.options.max代币 |
在聊天完成中生成的最大令牌数。输入标记和生成的标记的总长度受模型的上下文长度限制。 |
- |
spring.ai.mistralai.chat.options.safePrompt |
指示是否在所有对话之前注入安全提示。 |
假 |
spring.ai.mistralai.chat.options.randomSeed |
此功能处于 Beta 阶段。如果指定,我们的系统将尽最大努力确定性地采样,以便具有相同种子和参数的重复请求应返回相同的结果。 |
- |
spring.ai.mistralai.chat.options.stop |
如果检测到此令牌,则停止生成。或者在提供数组时检测到这些标记之一。 |
- |
spring.ai.mistralai.chat.options.topP |
使用温度进行采样的替代方法,称为核抽样,其中模型考虑具有top_p概率质量的标记的结果。所以 0.1 意味着只考虑包含前 10% 概率质量的 token。我们通常建议更改此 或 |
- |
spring.ai.mistralai.chat.options.responseFormat |
一个对象,用于指定模型必须输出的格式。设置为 |
- |
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.function回调 |
用于注册 ChatModel 的 Mistral AI 工具函数回调。 |
- |
spring.ai.mistralai.chat.options.proxy-tool-calls |
如果为true,则 Spring AI 将不会在内部处理函数调用,而是将它们代理给客户端。然后,客户端负责处理函数调用,将它们分派给适当的函数,并返回结果。如果为 false (默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
假 |
您可以覆盖常见的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)
constructor 或spring.ai.mistralai.chat.options.*
性能。
在运行时,您可以通过向Prompt
叫。
例如,要覆盖特定请求的默认型号和温度:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
MistralAiChatOptions.builder()
.withModel(MistralAiApi.ChatModel.LARGE.getValue())
.withTemperature(0.5)
.build()
));
除了特定于模型的 MistralAiChatOptions 之外,您还可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。 |
函数调用
您可以使用MistralAiChatModel
并让 Mistral AI 模型智能地选择输出包含参数的 JSON 对象,以调用一个或多个已注册的函数。
这是一种将 LLM 功能与外部工具和 API 连接起来的强大技术。
阅读有关 Mistral AI 函数调用的更多信息。
OpenAI API 兼容性
Mistral 与 OpenAI API 兼容,您可以使用 Spring AI OpenAI 客户端与 Mistrial 交谈。
为此,您需要将 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=<YOUR MISTRAL API KEY
.
查看 MistralWithOpenAiChatModelIT.java 测试,了解在 Spring AI OpenAI 上使用 Mistral 的示例。
Samples控制器(自动配置)
创建一个新的 Spring Boot 项目并添加spring-ai-mistral-ai-spring-boot-starter
添加到您的 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
使用 Chat 模型生成文本的类。
@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 Client 连接到 Mistral AI 服务。
添加spring-ai-mistral-ai
依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mistral-ai</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-mistral-ai'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
接下来,创建一个MistralAiChatModel
并将其用于文本生成:
var mistralAiApi = new MistralAiApi(System.getenv("MISTRAL_AI_API_KEY"));
var chatModel = new MistralAiChatModel(this.mistralAiApi, MistralAiChatOptions.builder()
.withModel(MistralAiApi.ChatModel.LARGE.getValue())
.withTemperature(0.4)
.withMaxTokens(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
是一个 Fluent 选项构建者。
低级 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 函数调用教程。