智譜 AI 聊天
Spring AI 支援智譜 AI 的各種 AI 語言模型。您可以與智譜 AI 語言模型互動,並基於智譜 AI 模型建立多語言會話助手。
前提條件
您需要使用智譜 AI 建立 API 來訪問智譜 AI 語言模型。
在 智譜 AI 註冊頁面 建立賬戶,並在 API 金鑰頁面 生成令牌。Spring AI 專案定義了一個名為 spring.ai.zhipuai.api-key
的配置屬性,您應該將其設定為從 API 金鑰頁面 獲取的 API Key
值。匯出環境變數是設定該配置屬性的一種方法
export SPRING_AI_ZHIPU_AI_API_KEY=<INSERT KEY HERE>
自動配置
Spring AI 自動配置、啟動器模組的工件名稱發生了重大變化。更多資訊請參閱 升級說明。 |
Spring AI 為智譜 AI 聊天客戶端提供了 Spring Boot 自動配置。要啟用它,請將以下依賴新增到專案的 Maven pom.xml
檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>
或新增到您的 Gradle build.gradle
構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-zhipuai'
}
請參閱 依賴管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
聊天屬性
重試屬性
字首 spring.ai.retry
用作屬性字首,允許您配置智譜 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 狀態碼列表(例如,用於丟擲 NonTransientAiException)。 |
空 |
spring.ai.retry.on-http-codes |
應觸發重試的 HTTP 狀態碼列表(例如,用於丟擲 TransientAiException)。 |
空 |
連線屬性
字首 spring.ai.zhiPu
用作屬性字首,允許您連線到智譜 AI。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.zhipuai.base-url |
連線的 URL |
|
spring.ai.zhipuai.api-key |
API 金鑰 |
- |
配置屬性
聊天自動配置的啟用和停用現在透過字首為 要啟用,設定 spring.ai.model.chat=zhipuai(預設啟用) 要停用,設定 spring.ai.model.chat=none(或任何不匹配 zhipuai 的值) 此更改是為了允許多模型配置。 |
字首 spring.ai.zhipuai.chat
是屬性字首,允許您配置智譜 AI 的聊天模型實現。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.zhipuai.chat.enabled (已移除且不再有效) |
啟用智譜 AI 聊天模型。 |
true |
spring.ai.model.chat |
啟用智譜 AI 聊天模型。 |
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 金鑰 |
- |
spring.ai.zhipuai.chat.options.model |
這是要使用的智譜 AI 聊天模型 |
|
spring.ai.zhipuai.chat.options.maxTokens |
聊天完成中生成的最大 token 數。輸入 token 和生成 token 的總長度受模型上下文長度限制。 |
- |
spring.ai.zhipuai.chat.options.temperature |
要使用的取樣溫度,介於 0 和 1 之間。較高的值(如 0.8)會使輸出更隨機,而較低的值(如 0.2)會使其更集中和確定。我們通常建議只調整此引數或 top_p,但不要同時調整兩者。 |
0.7 |
spring.ai.zhipuai.chat.options.topP |
一種替代溫度取樣的技術,稱為 nucleus 取樣,模型考慮 top_p 機率質量的 token 結果。因此,0.1 表示只考慮機率質量在前 10% 的 token。我們通常建議只調整此引數或 temperature,但不要同時調整兩者。 |
1.0 |
spring.ai.zhipuai.chat.options.stop |
模型將停止生成由 stop 指定的字元,目前只支援一個停止詞,格式為 [\"stop_word1\"] |
- |
spring.ai.zhipuai.chat.options.user |
表示您的終端使用者的唯一識別符號,可幫助智譜 AI 監控和檢測濫用行為。 |
- |
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.proxy-tool-calls |
如果為 true,Spring AI 將不會在內部處理函式呼叫,而是將其代理給客戶端。然後由客戶端負責處理函式呼叫,將其分派到適當的函式,並返回結果。如果為 false(預設值),Spring AI 將在內部處理函式呼叫。僅適用於支援函式呼叫的聊天模型 |
false |
您可以為 ChatModel 實現覆蓋通用的 spring.ai.zhipuai.base-url 和 spring.ai.zhipuai.api-key 。如果設定了 spring.ai.zhipuai.chat.base-url 和 spring.ai.zhipuai.chat.api-key 屬性,它們將優先於通用屬性。如果您想為不同的模型和不同的模型端點使用不同的智譜 AI 賬戶,這會很有用。 |
所有帶有 spring.ai.zhipuai.chat.options 字首的屬性都可以在執行時透過向 Prompt 呼叫新增請求特定的 執行時選項 來覆蓋。 |
執行時選項
ZhiPuAiChatOptions.java 提供了模型配置,例如要使用的模型、溫度、頻率懲罰等。
啟動時,可以使用 ZhiPuAiChatModel(api, options)
建構函式或 spring.ai.zhipuai.chat.options.*
屬性配置預設選項。
在執行時,您可以透過向 Prompt
呼叫新增新的、針對特定請求的選項來覆蓋預設選項。例如,為特定請求覆蓋預設模型和溫度
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 外,您還可以使用透過 ChatOptionsBuilder#builder() 建立的可移植 ChatOptions 例項。 |
示例控制器
建立 一個新的 Spring Boot 專案,並將 spring-ai-starter-model-zhipuai
新增到您的 pom (或 gradle) 依賴中。
在 src/main/resources
目錄下新增一個 application.properties
檔案,以啟用和配置智譜 AI 聊天模型
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 替換為您的智譜 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("/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 客戶端 連線到智譜 AI 服務。
將 spring-ai-zhipuai
依賴新增到專案的 Maven pom.xml
檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai</artifactId>
</dependency>
或新增到您的 Gradle build.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_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
是流式選項構建器。
低階 ZhiPuAiApi 客戶端
ZhiPuAiApi 是用於 智譜 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 測試提供了一些關於如何使用此輕量級庫的通用示例。