获取最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

智拍AI对话

智源AI为各种AI语言模型提供支持。您可以通过智源AI语言模型实现交互,并基于ZhiPuAI模型创建一个多语言对话助手。spring-doc.cadn.net.cn

前提条件

你需要通过ZhiPuAI创建一个API,以访问ZhiPu AI语言模型。spring-doc.cadn.net.cn

ZhiPu AI 注册页面创建账户,并在API密钥页面生成Tokens。spring-doc.cadn.net.cn

Spring AI项目定义了一个名为spring.ai.zhipuai.api-key的配置属性,您应将其设置为从API密钥页面获取的API Key值。spring-doc.cadn.net.cn

您可以在application.properties文件中设置此配置属性:spring-doc.cadn.net.cn

spring.ai.zhipuai.api-key=<your-zhipuai-api-key>

为了在处理API密钥等敏感信息时增强安全性,您可以使用Spring表达式语言(SpEL)引用自定义环境变量:spring-doc.cadn.net.cn

# In application.yml
spring:
  ai:
    zhipuai:
      api-key: ${ZHIPUAI_API_KEY}
# In your environment or .env file
export ZHIPUAI_API_KEY=<your-zhipuai-api-key>

您也可以在应用程序代码中以编程方式设置此配置:spring-doc.cadn.net.cn

// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("ZHIPUAI_API_KEY");

添加仓库和BOM

Spring AI 工件发布在 Maven Central 和 Spring Snapshot 仓库中。 请参阅 工件仓库 部分,以将这些仓库添加到您的构建系统。spring-doc.cadn.net.cn

为了帮助管理依赖,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建系统。spring-doc.cadn.net.cn

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。spring-doc.cadn.net.cn

Spring AI提供针对ZhiPuAI聊天客户端的自动生成配置。 要使它工作,将以下依赖项添加到项目Maven pom.xml文件中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-zhipuai'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

聊天属性

重试属性

The prefix spring.ai.retry 是用于配置 Zhi算AI 智能模型重试机制的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认

spring.ai.retry.max-attemptsspring-doc.cadn.net.cn

最大重试次数。spring-doc.cadn.net.cn

10spring-doc.cadn.net.cn

spring.ai.retry.backoff.initial-intervalspring-doc.cadn.net.cn

指数退避策略的初始睡眠时长。spring-doc.cadn.net.cn

2 秒spring-doc.cadn.net.cn

spring.ai.retry.backoff.multiplierspring-doc.cadn.net.cn

退避间隔乘数。spring-doc.cadn.net.cn

5spring-doc.cadn.net.cn

spring.ai.retry.backoff.max-intervalspring-doc.cadn.net.cn

最大退避时长。spring-doc.cadn.net.cn

3 分钟。spring-doc.cadn.net.cn

spring.ai.retry.on-client-errorsspring-doc.cadn.net.cn

如果为false,则抛出NonTransientAiException,且不重试客户端错误代码4xxspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.retry.exclude-on-http-codesspring-doc.cadn.net.cn

不应触发重试的HTTP状态代码列表(例如,用于抛出非暂时性AI异常)。spring-doc.cadn.net.cn

emptyspring-doc.cadn.net.cn

spring.ai.retry.on-http-codesspring-doc.cadn.net.cn

应触发重试的HTTP状态码列表(例如,用于抛出TransientAiException)。spring-doc.cadn.net.cn

emptyspring-doc.cadn.net.cn

连接属性

The prefix spring.ai.zhiPu is used as the property prefix that lets you connect to ZhiPuAI.spring-doc.cadn.net.cn

属性 描述 默认

spring.ai.zhipuai.base-urlspring-doc.cadn.net.cn

要连接的URLspring-doc.cadn.net.cn

open.bigmodel.cn/api/paasspring-doc.cadn.net.cn

spring.ai.zhipuai.api-keyspring-doc.cadn.net.cn

API 密钥spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

配置属性

启用和禁用聊天自动配置现在通过顶级属性使用前缀 spring.ai.model.chat 配置完成。spring-doc.cadn.net.cn

要启用 spring.ai.model.chat=zhipaiu(这是默认设置)spring-doc.cadn.net.cn

要禁用,设置 spring.ai.model.chat 为 none(或者任何不匹配 zhipai 的值)spring-doc.cadn.net.cn

这种修改是为了允许配置多个模型。spring-doc.cadn.net.cn

茨 之 爱 spring.ai.zhipuai.chat 是茨 之 爱 配置 节 点 位 置 的 性 质 前 缀,让 您 可 以 配置 节 点 位 置 的 部 分。spring-doc.cadn.net.cn

属性 描述 默认

spring.ai.zhipai.chat.Enabled(已移除,不再有效)spring-doc.cadn.net.cn

启用智派AI聊天模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.chatspring-doc.cadn.net.cn

启用智派AI聊天模型。spring-doc.cadn.net.cn

智图AIspring-doc.cadn.net.cn

spring.ai.zhipuai.chat.base-urlspring-doc.cadn.net.cn

可选地,覆盖 `spring.ai.zhipuai.base-url` 以提供聊天特定的URL。spring-doc.cadn.net.cn

open.bigmodel.cn/api/paasspring-doc.cadn.net.cn

spring.ai.zhipuai.chat.api-keyspring-doc.cadn.net.cn

可选地,覆盖 `spring.ai.zhipuai.api-key` 以提供聊天特定的 api 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.modelspring-doc.cadn.net.cn

这是要使用的智谱AI聊天模型spring-doc.cadn.net.cn

GLM-3-TurboGLM-3-TurboGLM-4GLM-4-AirGLM-4-AirXGLM-4-FlashGLM-4V 指向最新模型版本)spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.maxTokensspring-doc.cadn.net.cn

聊天补全时生成的最大Tokens数量。输入Tokens和生成Tokens的总长度受到模型上下文长度的限制。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.temperaturespring-doc.cadn.net.cn

什么是合适的采样温度?在0到1之间。较高的值,如0.8,会导致输出更加随机,而较低的值,如0.2,则会更加集中和确定性。通常我们建议仅调整或top_p,但不要同时调整两者。spring-doc.cadn.net.cn

0.7spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.topPspring-doc.cadn.net.cn

除了温度采样,还有一种叫做核仁采样的方法,其中模型考虑的是概率质量的top_p部分。因此,0.1表示只有包含概率质量前10%的那些token被考虑进去。通常我们不建议同时调整这两个参数,但有时候可能需要尝试。spring-doc.cadn.net.cn

1.0spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.stopspring-doc.cadn.net.cn

模型将停止生成由stop指定的字符,目前仅支持格式为["停止词1"]的单个停止词。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.userspring-doc.cadn.net.cn

一个代表你的终端用户的唯一标识符,有助于ZhiPuAI进行监控和检测滥用行为。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.requestIdspring-doc.cadn.net.cn

客户端提供的参数必须保证唯一性,以便区分每个请求的唯一标识符。如果客户端未提供该参数,平台将默认生成该标识符。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.doSamplespring-doc.cadn.net.cn

当 do_sample 设置为 true 时,采样策略被启用。如果 do_sample 为 false,则采样策略的参数 temperature 和 top_p 将不起作用。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.zhipuai.chat.options.proxy-tool-callsspring-doc.cadn.net.cn

如果为真,Spring AI 将不会在内部处理函数调用,而是将其代理给客户端。此时,由客户端负责处理函数调用、将其分发到相应的函数并返回结果。如果为假(默认值),Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型。spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

你可以覆盖 spring.ai.zhipuai.base-urlspring.ai.zhipuai.api-key 的默认值,用于 ChatModel 的实现。 如果设置了 spring.ai.zhipuai.chat.base-urlspring.ai.zhipuai.chat.api-key 的属性,它们将优先于默认属性。这很有用,如果你希望为不同的模型和不同的模型端点使用不同的 ZhiPuAI 账户。
所有以spring.ai.zhipuai.chat.options为前缀的属性都可以通过向Prompt调用中添加特定于请求的运行时选项在运行时覆盖。

运行时选项

The 智谱聊天选项配置类提供模型配置,例如要使用的模型、温度、频率惩罚等。spring-doc.cadn.net.cn

On start-up, the default options can be configured with the ZhiPuAiChatModel(api, options) constructor or the spring.ai.zhipuai.chat.options.* properties.spring-doc.cadn.net.cn

在运行时,您可以通过向Prompt调用添加新的、针对请求的选项来覆盖默认选项。 例如,要为特定请求覆盖默认模型和温度:spring-doc.cadn.net.cn

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        ZhiPuAiChatOptions.builder()
            .model(ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue())
            .temperature(0.5)
        .build()
    ));
除了模型特定的 ZhiPuAiChatOptions,你还可以使用一个便携的 ChatOptions 实例,通过调用 ChatOptions#builder() 创建。

示例控制器

创建一个新的Spring Boot项目,并将spring-boot-starter-web添加到您的pom(或gradle)依赖中。spring-doc.cadn.net.cn

添加一个 application.properties 文件,位于 src/main/resources 文件夹中,以支持和配置智启微对话模型:spring-doc.cadn.net.cn

spring.ai.zhipuai.api-key=YOUR_API_KEY
spring.ai.zhipuai.chat.options.model=glm-4-air
spring.ai.zhipuai.chat.options.temperature=0.7
请将0替换为您的智脑AI凭证。

这将创建一个ZhiPuAiChatModel实现,您可以将其注入到您的类中。 以下是一个使用聊天模型进行文本生成的简单@Controller类的例子。spring-doc.cadn.net.cn

@RestController
public class ChatController {

    private final ZhiPuAiChatModel chatModel;

    @Autowired
    public ChatController(ZhiPuAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping(value = "/ai/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	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);
    }
}

手动配置

智谱云客服模型 智谱云客服模型 实现了 ChatModelStreamingChatModel,并使用低层智谱云API客户端 低层智谱云API客户端 连接至智谱云服务。spring-doc.cadn.net.cn

spring-ai-zhipuai 依赖添加到您项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

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

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-zhipuai'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

接下来,创建一个ZhiPuAiChatModel并用它来生成文本:spring-doc.cadn.net.cn

var zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));

var chatModel = new ZhiPuAiChatModel(this.zhiPuAiApi, ZhiPuAiChatOptions.builder()
                .model(ZhiPuAiApi.ChatModel.GLM_3_Turbo.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> streamResponse = this.chatModel.stream(
    new Prompt("Generate the names of 5 famous pirates."));

数字ZhiPuAiChatOptions为聊天请求提供了配置信息。 数字ZhiPuAiChatOptions.Builder是流利选项构建器。spring-doc.cadn.net.cn

低级智谱API客户端

The ZhiPuAiApi 提供了一个轻量级的 Java 客户端,用于 ZhiPu AI APIspring-doc.cadn.net.cn

以下是如何以编程方式使用API的简单代码片段:spring-doc.cadn.net.cn

ZhiPuAiApi zhiPuAiApi =
    new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));

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

// Sync request
ResponseEntity<ChatCompletion> response = this.zhiPuAiApi.chatCompletionEntity(
    new ChatCompletionRequest(List.of(this.chatCompletionMessage), ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue(), 0.7, false));

// Streaming request
Flux<ChatCompletionChunk> streamResponse = this.zhiPuAiApi.chatCompletionStream(
        new ChatCompletionRequest(List.of(this.chatCompletionMessage), ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue(), 0.7, true));

遵循 智谱API.java's JavaDoc以获取进一步信息。spring-doc.cadn.net.cn

智普爱API 示例