Perplexity Chat
Perplexity AI 提供了一項獨特的 AI 服務,該服務將其語言模型與即時搜尋功能相結合。它提供了多種模型,並支援流式響應,用於會話式 AI。
Spring AI 透過重用現有的 OpenAI 客戶端來與 Perplexity AI 整合。要開始使用,您需要獲取 Perplexity API 金鑰,配置基礎 URL,並選擇一個支援的模型。

Perplexity API 與 OpenAI API 不完全相容。Perplexity 將即時網路搜尋結果與其語言模型響應相結合。與 OpenAI 不同,Perplexity 不暴露 toolCalls - function call 機制。此外,Perplexity 目前不支援多模態訊息。 |
檢視 PerplexityWithOpenAiChatModelIT.java 測試用例,瞭解如何在 Spring AI 中使用 Perplexity 的示例。
先決條件
-
建立 API 金鑰:訪問此處建立 API 金鑰。使用您 Spring AI 專案中的
spring.ai.openai.api-key
屬性進行配置。 -
設定 Perplexity 基礎 URL:將
spring.ai.openai.base-url
屬性設定為api.perplexity.ai
。 -
選擇 Perplexity 模型:使用
spring.ai.openai.chat.model=<model name>
屬性指定模型。參考 支援的模型 檢視可用選項。 -
設定 chat completions 路徑:將
spring.ai.openai.chat.completions-path
設定為/chat/completions
。參考 chat completions api 檢視更多詳情。
環境變數配置示例
export SPRING_AI_OPENAI_API_KEY=<INSERT PERPLEXITY API KEY HERE>
export SPRING_AI_OPENAI_BASE_URL=https://api.perplexity.ai
export SPRING_AI_OPENAI_CHAT_MODEL=llama-3.1-sonar-small-128k-online
自動配置
Spring AI 自動配置、starter 模組的 artifact 名稱發生了重大變化。請參考升級說明獲取更多資訊。 |
Spring AI 為 OpenAI Chat Client 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到您專案的 Maven pom.xml
或 Gradle build.gradle
構建檔案中
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
參考依賴管理部分將 Spring AI BOM 新增到您的構建檔案中。 |
Chat 屬性
重試屬性
spring.ai.retry
字首用作屬性字首,您可以使用它配置 OpenAI 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,並且不對 |
false |
spring.ai.retry.exclude-on-http-codes |
不應觸發重試的 HTTP 狀態碼列表(例如,丟擲 NonTransientAiException)。 |
空 |
spring.ai.retry.on-http-codes |
應觸發重試的 HTTP 狀態碼列表(例如,丟擲 TransientAiException)。 |
空 |
連線屬性
spring.ai.openai
字首用作屬性字首,您可以使用它連線到 OpenAI。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.openai.base-url |
要連線的 URL。必須設定為 |
- |
spring.ai.openai.chat.api-key |
您的 Perplexity API 金鑰 |
- |
配置屬性
聊天自動配置的啟用和停用現在透過帶有 要啟用,請將 spring.ai.model.chat 設定為 openai(預設已啟用) 要停用,請將 spring.ai.model.chat 設定為 none(或任何與 openai 不匹配的值) 此更改旨在允許配置多個模型。 |
spring.ai.openai.chat
字首是屬性字首,您可以使用它配置 OpenAI 的 chat 模型實現。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.model.chat |
啟用 OpenAI chat 模型。 |
openai |
spring.ai.openai.chat.model |
一種受支援的 Perplexity 模型。例如: |
- |
spring.ai.openai.chat.base-url |
可選地覆蓋 spring.ai.openai.base-url,以提供 chat 特定 URL。必須設定為 |
- |
spring.ai.openai.chat.completions-path |
必須設定為 |
|
spring.ai.openai.chat.options.temperature |
響應中的隨機性量,值在 0(包含)到 2(不包含)之間。值越高越隨機,值越低越確定。必需範圍: |
0.2 |
spring.ai.openai.chat.options.frequencyPenalty |
大於 0 的乘法懲罰項。大於 1.0 的值根據新 token 在目前文字中出現的頻率來懲罰它們,降低模型逐字重複同一行的可能性。值為 1.0 表示沒有懲罰。與 presence_penalty 不相容。必需範圍: |
1 |
spring.ai.openai.chat.options.maxTokens |
API 返回的最大完成 token 數量。max_tokens 中請求的 token 總數加上訊息中傳送的 prompt token 數量不得超過所請求模型的上下文視窗 token 限制。如果未指定,模型將生成 token,直到達到其停止 token 或上下文視窗結束。 |
- |
spring.ai.openai.chat.options.presencePenalty |
介於 -2.0 和 2.0 之間的值。正值根據新 token 是否出現在目前文字中來懲罰它們,增加模型談論新話題的可能性。與 |
0 |
spring.ai.openai.chat.options.topP |
核取樣閾值,值在 0 到 1(包含)之間。對於每個後續 token,模型會考慮 top_p 機率質量的 token 結果。我們建議僅更改 top_k 或 top_p 其中一個,而不要同時更改兩者。必需範圍: |
0.9 |
spring.ai.openai.chat.options.stream-usage |
(僅適用於流式傳輸)設定此項可在整個請求中新增包含 token 使用統計資訊的額外 chunk。此 chunk 的 |
false |
所有以 spring.ai.openai.chat.options 為字首的屬性都可以在執行時透過向 Prompt 呼叫新增請求特定的 執行時選項 來覆蓋。 |
執行時選項
OpenAiChatOptions.java 提供了模型配置,例如要使用的模型、temperature、frequency penalty 等。
在啟動時,可以透過 OpenAiChatModel(api, options)
建構函式或 spring.ai.openai.chat.options.*
屬性配置預設選項。
在執行時,您可以透過向 Prompt
呼叫新增新的、特定於請求的選項來覆蓋預設選項。例如,覆蓋特定請求的預設模型和 temperature
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder()
.model("llama-3.1-sonar-large-128k-online")
.temperature(0.4)
.build()
));
除了模型特定的 OpenAiChatOptions 之外,您還可以使用透過 ChatOptions#builder() 建立的可移植 ChatOptions 例項。 |
示例控制器
建立一個新的 Spring Boot 專案,並將 spring-ai-starter-model-openai
新增到您的 pom (或 gradle) 依賴中。
在 src/main/resources
目錄下新增 application.properties
檔案,以啟用和配置 OpenAi chat 模型
spring.ai.openai.api-key=<PERPLEXITY_API_KEY>
spring.ai.openai.base-url=https://api.perplexity.ai
spring.ai.openai.chat.completions-path=/chat/completions
spring.ai.openai.chat.options.model=llama-3.1-sonar-small-128k-online
spring.ai.openai.chat.options.temperature=0.7
# The Perplexity API doesn't support embeddings, so we need to disable it.
spring.ai.openai.embedding.enabled=false
將 api-key 替換為您自己的 Perplexity Api 金鑰。 |
這將建立一個 OpenAiChatModel
實現,您可以將其注入到您的類中。這是一個使用 chat 模型進行文字生成的簡單 @Controller
類示例。
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
@Autowired
public ChatController(OpenAiChatModel 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) {
Prompt prompt = new Prompt(new UserMessage(message));
return this.chatModel.stream(prompt);
}
}
支援的模型
Perplexity 支援多種為搜尋增強型會話式 AI 最佳化的模型。參考 支援的模型 獲取詳情。