智拍AI对话
智源AI为各种AI语言模型提供支持。您可以通过智源AI语言模型实现交互,并基于ZhiPuAI模型创建一个多语言对话助手。
If you’re not a Chinese user, you can visit ZhiPuAI’s international site Z.ai
前提条件
你需要通过ZhiPuAI创建一个API,以访问ZhiPu AI语言模型。
创建账户,请访问 智谱AI注册页面(或 Z.ai注册页面),并在 API密钥页面(或 Z.ai API密钥页面)生成密钥。
Spring AI项目定义了一个名为spring.ai.zhipuai.api-key的配置属性,您应将其设置为从API密钥页面获取的API Key值。
您可以在application.properties文件中设置此配置属性:
spring.ai.zhipuai.api-key=<your-zhipuai-api-key>
为了在处理API密钥等敏感信息时增强安全性,您可以使用Spring表达式语言(SpEL)引用自定义环境变量:
# 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>
您也可以在应用程序代码中以编程方式设置此配置:
// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("ZHIPUAI_API_KEY");
自动配置
|
There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。 |
Spring AI提供针对ZhiPuAI聊天客户端的自动生成配置。
要使它工作,将以下依赖项添加到项目Maven pom.xml文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-zhipuai'
}
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
聊天属性
重试属性
The prefix spring.ai.retry 是用于配置 Zhi算AI 智能模型重试机制的属性前缀。
| 属性 | 描述 | 默认 |
|---|---|---|
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,且不重试客户端错误代码 |
false |
spring.ai.retry.exclude-on-http-codes |
不应触发重试的HTTP状态代码列表(例如,用于抛出非暂时性AI异常)。 |
empty |
spring.ai.retry.on-http-codes |
应触发重试的HTTP状态码列表(例如,用于抛出TransientAiException)。 |
empty |
连接属性
The prefix spring.ai.zhipuai is used as the property prefix that lets you connect to ZhiPuAI.
| 属性 | 描述 | 默认 |
|---|---|---|
spring.ai.zhipuai.base-url |
连接到智谱AI API的URL。 |
|
spring.ai.zhipuai.api-key |
API 密钥 |
- |
配置属性
|
启用和禁用聊天自动配置现在通过顶级属性使用前缀 要启用 spring.ai.model.chat=zhipaiu(这是默认设置) 要禁用,设置 spring.ai.model.chat 为 none(或者任何不匹配 zhipai 的值) 这种修改是为了允许配置多个模型。 |
茨 之 爱 spring.ai.zhipuai.chat 是茨 之 爱 配置 节 点 位 置 的 性 质 前 缀,让 您 可 以 配置 节 点 位 置 的 部 分。
| 属性 | 描述 | 默认 |
|---|---|---|
spring.ai.zhipai.chat.Enabled(已移除,不再有效) |
启用智派AI聊天模型。 |
true |
spring.ai.model.chat |
启用智派AI聊天模型。 |
智图AI |
spring.ai.zhipuai.chat.base-url |
可选地修改spring.ai.zhipuai.base-url,以提供适用于聊天的特定URL。 |
|
spring.ai.zhipuai.chat.api-key |
可选地覆盖 spring.ai.zhipua.api-key 以提供特定于聊天的 API 密钥。 |
- |
spring.ai.zhipuai.chat.options.model |
智拍AI聊天模型可用。您可以选择以下模型: |
|
spring.ai.zhipuai.chat.options.maxTokens |
聊天补全时生成的最大Tokens数量。输入Tokens和生成Tokens的总长度受到模型上下文长度的限制。 |
- |
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指定的字符,目前仅支持格式为["停止词1"]的单个停止词。 |
- |
spring.ai.zhipuai.chat.options.user |
一个代表你的终端用户的唯一标识符,有助于ZhiPuAI进行监控和检测滥用行为。 |
- |
spring.ai.zhipuai.chat.options.requestId |
客户端提供的参数必须保证唯一性,以便区分每个请求的唯一标识符。如果客户端未提供该参数,平台将默认生成该标识符。 |
- |
spring.ai.zhipuai.chat.options.doSample |
当 do_sample 设置为 true 时,采样策略被启用。如果 do_sample 为 false,则采样策略的参数 temperature 和 top_p 将不起作用。 |
true |
spring.ai.zhipuai.chat.options.response-format.type |
控制模型输出格式。将字段设置为 |
- |
spring.ai.zhipuai.chat.options.thinking.type |
控制大模型链式思考的启用与否。可用选项为:0或1。 |
- |
spring.ai.zhipuai.chat.options.tool-names |
用于在单个提示请求中启用函数调用的工具列表,这些工具通过它们的名称来识别。具有这些名称的工具必须存在于ToolCallback注册表中。 |
- |
spring.ai.zhipuai.chat.options.tool-callbacks |
向ChatModel注册的工具回调。 |
- |
spring.ai.zhipuai.chat.options.internal-tool-execution-enabled |
如果为 false,Spring AI 将不内部处理工具调用,而是将它们代理给客户端。此后,处理工具调用、将其分派给适当函数并返回结果的责任在于客户端。如果为 true(默认值),Spring AI 将内部处理函数调用。此设置仅适用于支持函数调用的聊天模型。 |
true |
你可以覆盖 spring.ai.zhipuai.base-url 和 spring.ai.zhipuai.api-key 的默认值,用于 ChatModel 的实现。
如果设置了 spring.ai.zhipuai.chat.base-url 和 spring.ai.zhipuai.chat.api-key 的属性,它们将优先于默认属性。这很有用,如果你希望为不同的模型和不同的模型端点使用不同的 ZhiPuAI 账户。 |
所有以spring.ai.zhipuai.chat.options为前缀的属性都可以通过向Prompt调用中添加特定于请求的运行时选项在运行时覆盖。 |
运行时选项
The 智谱聊天选项配置类提供模型配置,例如要使用的模型、温度、频率惩罚等。
On start-up, the default options can be configured with the ZhiPuAiChatModel(api, options) constructor or the spring.ai.zhipuai.chat.options.* properties.
在运行时,您可以通过向Prompt调用添加新的、针对请求的选项来覆盖默认选项。
例如,要为特定请求覆盖默认模型和温度:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
ZhiPuAiChatOptions.builder()
.model(ZhiPuAiApi.ChatModel.GLM_4_Air.getValue())
.temperature(0.5)
.build()
));
| 除了模型特定的 ZhiPuAiChatOptions,你还可以使用一个便携的 ChatOptions 实例,通过调用 ChatOptions#builder() 创建。 |
示例控制器
创建一个新的Spring Boot项目,并将spring-boot-starter-web添加到您的pom(或gradle)依赖中。
添加一个 application.properties 文件,位于 src/main/resources 文件夹中,以支持和配置智启微对话模型:
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类的例子。
@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);
}
}
手动配置
智谱云客服模型 智谱云客服模型 实现了 ChatModel 和 StreamingChatModel,并使用低层智谱云API客户端 低层智谱云API客户端 连接至智谱云服务。
将 spring-ai-zhipuai 依赖添加到您项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-zhipuai'
}
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
接下来,创建一个ZhiPuAiChatModel并用它来生成文本:
var zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));
var chatModel = new ZhiPuAiChatModel(this.zhiPuAiApi, ZhiPuAiChatOptions.builder()
.model(ZhiPuAiApi.ChatModel.GLM_4_Air.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是流利选项构建器。
低级智谱API客户端
The ZhiPuAiApi 提供了一个轻量级的 Java 客户端,用于 ZhiPu AI API。
以下是如何以编程方式使用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_4_Air.getValue(), 0.7, false));
// Streaming request
Flux<ChatCompletionChunk> streamResponse = this.zhiPuAiApi.chatCompletionStream(
new ChatCompletionRequest(List.of(this.chatCompletionMessage), ZhiPuAiApi.ChatModel.GLM_4_Air.getValue(), 0.7, true));
遵循 智谱API.java's JavaDoc以获取进一步信息。
智普爱API 示例
-
The ZhiPuAiApiIT.java test provides some general examples how to use the lightweight library.