智普 AI 聊天
Spring AI 支持 ZhiPu AI 的各种 AI 语言模型。您可以与 ZhiPu AI 语言模型交互,并基于 ZhiPuAI 模型创建多语言对话助手。
先决条件
您需要使用 ZhiPuAI 创建 API 才能访问 ZhiPu AI 语言模型。
在 智普 AI 注册页面创建账号,并在 API Keys 页面生成 Token。
Spring AI 项目定义了一个名为spring.ai.zhipuai.api-key
,您应该设置为API Key
从 API 密钥页面获取。
导出环境变量是设置该配置属性的一种方法:
export SPRING_AI_ZHIPU_AI_API_KEY=<INSERT KEY HERE>
添加存储库和 BOM
Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。 请参阅 Repositories 部分,将这些存储库添加到您的构建系统中。
为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
Spring AI 为 ZhiPuAI Chat 客户端提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-zhipuai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry
用作属性前缀,允许您为 ZhiPu 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.zhiPu
用作用于连接到 ZhiPuAI 的属性前缀。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.zhipuai.base-url |
要连接到的 URL |
|
spring.ai.zhipuai.api-key |
API 密钥 |
- |
配置属性
前缀spring.ai.zhipuai.chat
是属性前缀,用于为 ZhiPuAI 配置聊天模型实现。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.zhipuai.chat.enabled |
启用 ZhiPuAI 聊天模型。 |
真 |
spring.ai.zhipuai.chat.base-url |
可选覆盖spring.ai.zhipuai.base-url以提供特定于聊天的 url |
|
spring.ai.zhipuai.chat.api-key |
可选覆盖 spring.ai.zhipuai.api-key 以提供特定于聊天的 api-key |
- |
spring.ai.zhipuai.chat.options.model |
这是要使用的 ZhiPuAI Chat 模型 |
|
spring.ai.zhipuai.chat.options.maxTokens |
在聊天完成中生成的最大令牌数。输入标记和生成的标记的总长度受模型的上下文长度限制。 |
- |
spring.ai.zhipuai.chat.options.temperature |
要使用的采样温度,介于 0 和 1 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定。我们通常建议更改此项或top_p但不能同时更改两者。 |
0.7 |
spring.ai.zhipuai.chat.options.topP |
使用温度进行采样的替代方法,称为核抽样,其中模型考虑具有top_p概率质量的标记的结果。所以 0.1 意味着只考虑包含前 10% 概率质量的 token。我们通常建议改变这个或温度,但不能同时改变两者.. |
1.0 |
spring.ai.zhipuai.chat.options.stop |
该模型将停止生成 stop 指定的字符,目前仅支持 [“stop_word1”] 格式的单个停用词 |
- |
spring.ai.zhipuai.chat.options.user |
代表您的最终用户的唯一标识符,可以帮助ZhiPuAI 监控和检测滥用行为。 |
- |
spring.ai.zhipuai.chat.options.requestId |
该参数由客户端传递,必须确保唯一性。它用于区分每个请求的唯一标识符。如果客户端不提供,平台将默认生成。 |
- |
spring.ai.zhipuai.chat.options.do示例 |
当 do_sample 设置为 true 时,将启用采样策略。如果 do_sample 为 false,则采样策略参数 temperature 和 top_p 不会生效。 |
真 |
spring.ai.zhipuai.chat.options.proxy-tool-calls |
如果为true,则 Spring AI 将不会在内部处理函数调用,而是将它们代理给客户端。然后,客户端负责处理函数调用,将它们分派给适当的函数,并返回结果。如果为 false (默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
假 |
您可以覆盖常见的spring.ai.zhipuai.base-url 和spring.ai.zhipuai.api-key 对于ChatModel 实现。
这spring.ai.zhipuai.chat.base-url 和spring.ai.zhipuai.chat.api-key 如果设置了 properties,则优先于 common properties。
如果您想为不同的模型和不同的模型终端节点使用不同的 ZhiPuAI 帐户,这将非常有用。 |
所有前缀为spring.ai.zhipuai.chat.options 可以通过将特定于请求的运行时选项添加到Prompt 叫。 |
运行时选项
ZhiPuAiChatOptions.java 提供模型配置,例如要使用的模型、温度、频率损失等。
启动时,可以使用ZhiPuAiChatModel(api, options)
constructor 或spring.ai.zhipuai.chat.options.*
性能。
在运行时,您可以通过向Prompt
叫。
例如,要覆盖特定请求的默认模型和温度:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
ZhiPuAiChatOptions.builder()
.withModel(ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue())
.withTemperature(0.5)
.build()
));
除了特定于模型的 ZhiPuAiChatOptions 之外,您还可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。 |
Samples控制器
创建一个新的 Spring Boot 项目并添加spring-ai-zhipuai-spring-boot-starter
添加到您的 POM(或 Gradle)依赖项中。
添加application.properties
文件中的src/main/resources
目录下,开启和配置 ZhiPuAi 聊天模型:
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
将api-key 替换为您的 ZhiPuAI 凭据。 |
这将创建一个ZhiPuAiChatModel
实现,您可以将其注入到您的类中。
下面是一个简单的示例@Controller
使用 Chat 模型生成文本的类。
@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("/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);
}
}
手动配置
ZhiPuAiChatModel 实现了ChatModel
和StreamingChatModel
并使用低级 ZhiPuAiApi Client 连接 ZhiPuAI 服务。
添加spring-ai-zhipuai
依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-zhipuai'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
接下来,创建一个ZhiPuAiChatModel
并将其用于文本生成:
var zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));
var chatModel = new ZhiPuAiChatModel(this.zhiPuAiApi, ZhiPuAiChatOptions.builder()
.withModel(ZhiPuAiApi.ChatModel.GLM_3_Turbo.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> streamResponse = this.chatModel.stream(
new Prompt("Generate the names of 5 famous pirates."));
这ZhiPuAiChatOptions
提供聊天请求的配置信息。
这ZhiPuAiChatOptions.Builder
是 Fluent Options Builder。
低级 ZhiPuAiApi 客户端
ZhiPuAiApi 提供的是 ZhiPu AI API 的轻量级 Java 客户端。
以下是如何以编程方式使用 api 的简单代码段:
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));
有关详细信息,请遵循 ZhiPuAiApi.java 的 JavaDoc。
ZhiPuAiApi 示例
-
ZhiPuAiApiIT.java 测试提供了一些如何使用轻量级库的一般示例。