Mistral AI Chat - Mistral AI聊天

Spring AI支持来自Mistral AI的各种AI语言模型。您可以与Mistral AI语言模型进行交互,并基于Mistral模型创建多语言对话助手。

先决条件

您需要创建一个API以访问Mistral AI语言模型。请在MistralAI注册页面创建一个帐户,并在API Keys页面生成令牌。Spring AI项目定义了一个名为spring.ai.mistralai.api-key的配置属性,您应将其设置为从console.mistral.ai获取的API密钥的值。通过导出环境变量是设置该配置属性的一种方式:

export SPRING_AI_MISTRALAI_API_KEY=<在此处插入密钥>

添加存储库和BOM

Spring AI工件发布在Spring里程碑和快照存储库中。请参考存储库部分,以将这些存储库添加到您的构建系统中。

为了帮助管理依赖关系,Spring AI提供了一个BOM(材料清单),以确保整个项目中使用一致的Spring AI版本。请参考依赖管理部分,以将Spring AI BOM添加到您的构建系统中。

自动配置

Spring AI为MistralAI聊天客户端提供了Spring Boot自动配置。要启用它,请将以下依赖项添加到您项目的Maven pom.xml文件中:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId>
</dependency>

或者添加到您的Gradle build.gradle构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-mistral-ai-spring-boot-starter'
}
请参考依赖管理章节,将Spring AI BOM添加到您的构建文件中。

聊天属性

重试属性

前缀spring.ai.retry用作属性前缀,可让您为 Mistral AI Chat 客户端配置重试机制。

属性 描述 默认

spring.ai.retry.max-attempts

最大重试次数。

10

spring.ai.retry.backoff.initial-interval

指数回退策略的初始睡眠持续时间。

2 秒。

spring.ai.retry.backoff.multiplier

回退间隔倍增器。

5

spring.ai.retry.backoff.max-interval

最大回退持续时间。

3 分钟。

spring.ai.retry.on-client-errors

如果为 false,则抛出 NonTransientAiException,并且不会尝试重试4xx客户端错误代码

false

spring.ai.retry.exclude-on-http-codes

不应触发重试的 HTTP 状态码列表(例如抛出 NonTransientAiException)。

连接属性

前缀spring.ai.mistralai用作属性前缀,可让您连接到 OpenAI。

属性 描述 默认

spring.ai.mistralai.base-url

连接到的 URL

api.mistral.ai

spring.ai.mistralai.api-key

API 密钥

-

配置属性

前缀spring.ai.mistralai.chat是配置 MistralAI 的聊天客户端实现的属性前缀。

属性 描述 默认值

spring.ai.mistralai.chat.enabled

启用 MistralAI 聊天客户端。

true

spring.ai.mistralai.chat.base-url

可选,用于覆盖 spring.ai.mistralai.base-url,提供特定于聊天的 URL。

-

spring.ai.mistralai.chat.api-key

可选,用于覆盖 spring.ai.mistralai.api-key,提供特定于聊天的 API 密钥。

-

spring.ai.mistralai.chat.options.model

要使用的 MistralAI 聊天模型。

open-mistral-7b, open-mixtral-8x7b, mistral-small-latest, mistral-medium-latest, mistral-large-latest

spring.ai.mistralai.chat.options.temperature

用于控制生成完成的表面创造性的采样温度。更高的值会使输出更随机,而较低的值会使结果更集中和确定性。不建议同时修改温度和 top_p,因为这两个设置的交互难以预测。

0.8

spring.ai.mistralai.chat.options.maxTokens

生成的聊天完成中生成的最大令牌数。输入令牌和生成的令牌的总长度由模型的上下文长度限制。

-

spring.ai.mistralai.chat.options.safePrompt

指示是否在所有对话之前注入安全提示。

false

spring.ai.mistralai.chat.options.randomSeed

此功能处于 Beta 版。如果指定,我们的系统将尽最大努力以确定性地进行采样,以便具有相同种子和参数的重复请求应返回相同的结果。

-

spring.ai.mistralai.chat.options.stop

最多 4 个序列,其中 API 将停止生成更多的令牌。

-

spring.ai.mistralai.chat.options.topP

一种与温度一起进行采样的替代方法,称为核采样,其中模型考虑具有 top_p 概率质量的令牌的结果。因此,0.1 表示仅考虑组成前 10% 概率质量的令牌。我们通常建议修改此设置或温度,但不要同时修改两者。

-

spring.ai.mistralai.chat.options.responseFormat

指定模型必须输出的格式的对象。将其设置为 { "type": "json_object" } 可以启用 JSON 模式,从而保证模型生成的消息是有效的 JSON。

-

spring.ai.mistralai.chat.options.tools

模型可能调用的工具列表。目前,仅支持将函数作为工具。使用此选项来提供模型可能为其生成 JSON 输入的函数列表。

-

spring.ai.mistralai.chat.options.toolChoice

控制模型调用哪个(如果有)函数。none 表示模型不会调用函数,而是生成消息。auto 表示模型可以在生成消息或调用函数之间选择。通过 {"type: "function", "function": {"name": "my_function"}} 指定特定函数会强制模型调用该函数。如果不存在函数,则 none 是默认值。如果存在函数,则 auto 是默认值。

-

spring.ai.mistralai.chat.options.functions

要在单个提示请求中启用函数调用的函数列表,由其名称标识。这些名称的函数必须存在于 functionCallbacks 注册表中。

-

spring.ai.mistralai.chat.options.functionCallbacks

MistralAI 工具函数回调,用于与 ChatClient 注册。

-

您可以覆盖常见的spring.ai.mistralai.base-urlspring.ai.mistralai.api-key以用于ChatClientEmbeddingClient的实现。如果设置了spring.ai.mistralai.chat.base-urlspring.ai.mistralai.chat.api-key属性,则这些属性优先于通用属性。如果您希望为不同的模型和不同的模型端点使用不同的MistralAI帐户,则这很有用。
所有以spring.ai.mistralai.chat.options为前缀的属性都可以通过向Prompt调用中添加一个特定于请求的Chat Options来在运行时进行覆盖。

聊天选项

MistralAiChatOptions.java 提供了模型配置,如要使用的模型、温度、频率惩罚等。

在启动时,可以使用 MistralAiChatClient(api,options) 构造函数或 spring.ai.mistralai.chat.options.* 属性来配置默认选项。

在运行时,您可以通过向 Prompt 调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认模型和温度:

ChatResponse response = chatClient.call(
    new Prompt(
        "生成5个著名海盗的名字。",
        MistralAiChatOptions.builder()
            .withModel(MistralAiApi.ChatModel.LARGE.getValue())
            .withTemperature(0.5f)
        .build()
    ));
除了特定于模型的 MistralAiChatOptions,您还可以使用一个可移植的 ChatOptions 实例,使用 ChatOptionsBuilder#builder() 创建。

示例控制器(自动配置)

创建 一个新的 Spring Boot 项目,并将 spring-ai-mistralai-spring-boot-starter 添加到您的 pom(或 gradle)依赖项中。

src/main/resources 目录下添加一个 application.properties 文件,以启用并配置 OpenAi 聊天客户端:

spring.ai.mistralai.api-key=YOUR_API_KEY
spring.ai.mistralai.chat.options.model=mistral-medium
spring.ai.mistralai.chat.options.temperature=0.7
用您的 OpenAI 凭据替换 api-key
MistralAiChatClient 实现

手动配置

MistralAiChatClient 实现了 ChatClientStreamingChatClient 接口,并使用低级别的 MistralAiApi 客户端Low-level MistralAiApi Client连接 MistralAI 服务。

spring-ai-mistralai 依赖项添加到项目的 Maven pom.xml 文件中:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mistralai</artifactId>
</dependency>

或者将其添加到 Gradle 的 build.gradle 文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-mistralai'
}
请参阅 依赖管理 部分,在构建文件中添加 Spring AI BOM。

接下来,创建一个MistralAiChatClient并用它进行文本生成:

var mistralAiApi = new MistralAiApi(System.getenv("MISTRAL_AI_API_KEY"));

var chatClient = new MistralAiChatClient(mistralAiApi, MistralAiChatOptions.builder()
                .withModel(MistralAiApi.ChatModel.LARGE.getValue())
                .withTemperature(0.4f)
                .withMaxToken(200)
                .build());

ChatResponse response = chatClient.call(
    new Prompt("生成五个著名海盗的名字。"));

// 或使用流式响应
Flux<ChatResponse> response = chatClient.stream(
    new Prompt("生成五个著名海盗的名字。"));

MistralAiChatOptions 提供了聊天请求的配置信息。 MistralAiChatOptions.Builder 是流畅的选项构建器。

低级别的 MistralAiApi 客户端

MistralAiApi 提供了一个轻量级的 Java 客户端,用于连接Mistral AI API

下面是一个简单的代码片段,演示如何以编程方式使用该 API:

MistralAiApi mistralAiApi =
    new MistralAiApi(System.getenv("MISTRAL_AI_API_KEY"));

ChatCompletionMessage chatCompletionMessage =
    new ChatCompletionMessage("Hello world", Role.USER);

// 同步请求
ResponseEntity<ChatCompletion> response = mistralAiApi.chatCompletionEntity(
    new ChatCompletionRequest(List.of(chatCompletionMessage), MistralAiApi.ChatModel.LARGE.getValue(), 0.8f, false));

// 流式请求
Flux<ChatCompletionChunk> streamResponse = mistralAiApi.chatCompletionStream(
        new ChatCompletionRequest(List.of(chatCompletionMessage), MistralAiApi.ChatModel.LARGE.getValue(), 0.8f, true));

请参阅 MistralAiApi.java 的 JavaDoc,了解更多信息。

MistralAiApi 示例