跳到主要内容

Perplexity Chat

DeepSeek V3 中英对照 Perplexity AI Perplexity Chat

Perplexity AI 提供了一项独特的 AI 服务,该服务将其语言模型与实时搜索功能相结合。它提供了多种模型,并支持流式响应以用于对话式 AI。

Spring AI 通过复用现有的 OpenAI 客户端与 Perplexity AI 进行集成。要开始使用,您需要获取一个 Perplexity API Key,配置基础 URL,并选择一个支持的 模型

spring ai perplexity 集成

备注

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](https://api.perplexity.ai)

  • 选择 Perplexity 模型:使用 spring.ai.openai.chat.model=<model name> 属性指定模型。请参阅支持的模型以获取可用选项。

  • 设置聊天补全路径:将 spring.ai.openai.chat.completions-path 设置为 /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
shell

添加仓库和 BOM

Spring AI 的构件已发布在 Maven Central 和 Spring Snapshot 仓库中。请参考 Repositories 部分,将这些仓库添加到您的构建系统中。

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

自动配置

Spring AI 为 OpenAI Chat Client 提供了 Spring Boot 自动配置功能。要启用该功能,请将以下依赖项添加到项目的 Maven pom.xml 或 Gradle build.gradle 构建文件中:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
xml

:::提示
请参考依赖管理部分,将 Spring AI BOM 添加到您的构建文件中。
:::

聊天属性

重试属性

前缀 spring.ai.retry 用作属性前缀,允许你为 OpenAI 聊天模型配置重试机制。

属性描述默认值
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,并且不对 4xx 客户端错误代码进行重试。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。必须设置为 [api.perplexity.ai](https://api.perplexity.ai)-
spring.ai.openai.chat.api-key你的 Perplexity API 密钥-

配置属性

前缀 spring.ai.openai.chat 是用于配置 OpenAI 聊天模型实现的属性前缀。

属性描述默认值
spring.ai.openai.chat.model支持的 Perplexity 模型 之一。例如:llama-3.1-sonar-small-128k-online-
spring.ai.openai.chat.base-url可选覆盖 spring.ai.openai.base-url 以提供特定于聊天的 URL。必须设置为 [api.perplexity.ai](https://api.perplexity.ai)-
spring.ai.openai.chat.completions-path必须设置为 /chat/completions/v1/chat/completions
spring.ai.openai.chat.options.temperature响应中的随机性程度,值在 0(包含)到 2(不包含)之间。值越高,响应越随机;值越低,响应越确定。要求范围:0 < x < 20.2
spring.ai.openai.chat.options.frequencyPenalty大于 0 的乘法惩罚值。值大于 1.0 会根据新标记在文本中已有的频率对其进行惩罚,从而降低模型重复相同行的可能性。值为 1.0 表示没有惩罚。与 presence_penalty 不兼容。要求范围:x > 01
spring.ai.openai.chat.options.maxTokensAPI 返回的最大完成标记数。max_tokens 中请求的标记总数加上 messages 中发送的提示标记数不得超过所请求模型的上下文窗口标记限制。如果未指定,则模型将生成标记,直到达到停止标记或上下文窗口结束。-
spring.ai.openai.chat.options.presencePenalty介于 -2.0 和 2.0 之间的值。正值会根据新标记是否出现在文本中对其进行惩罚,增加模型讨论新话题的可能性。与 frequency_penalty 不兼容。要求范围:-2 < x < 20
spring.ai.openai.chat.options.topP核心采样阈值,值介于 0 和 1 之间(包含)。对于每个后续标记,模型会考虑具有 top_p 概率质量的标记结果。我们建议更改 top_k 或 top_p,但不要同时更改两者。要求范围:0 < x < 10.9
spring.ai.openai.chat.options.stream-usage(仅适用于流式传输)设置为添加包含整个请求的标记使用统计信息的额外块。此块的 choices 字段为空数组,所有其他块也将包含一个 usage 字段,但值为 null。false
提示

所有以 spring.ai.openai.chat.options 为前缀的属性都可以通过在 Prompt 调用中添加请求特定的运行时选项来在运行时覆盖。

运行时选项

OpenAiChatOptions.java 提供了模型的配置选项,例如使用的模型、温度(temperature)、频率惩罚(frequency penalty)等。

在启动时,默认选项可以通过 OpenAiChatModel(api, options) 构造函数或 spring.ai.openai.chat.options.* 属性进行配置。

在运行时,你可以通过在 Prompt 调用中添加新的、特定于请求的选项来覆盖默认选项。例如,要为特定请求覆盖默认的模型和温度参数:

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()
));
java
提示

除了特定于模型的 OpenAiChatOptions 外,你还可以使用一个可移植的 ChatOptions 实例,该实例是通过 ChatOptions#builder() 创建的。

函数调用

备注

Perplexity 不支持显式的函数调用。相反,它会将搜索结果直接集成到响应中。

多模态

备注

目前,Perplexity API 不支持媒体内容。

示例控制器

创建 一个新的 Spring Boot 项目,并将 spring-ai-openai-spring-boot-starter 添加到你的 pom(或 gradle)依赖中。

src/main/resources 目录下添加一个 application.properties 文件,以启用并配置 OpenAi 聊天模型:

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
application.properties
提示

api-key 替换为你的 Perplexity API 密钥。

这将创建一个 OpenAiChatModel 实现,你可以将其注入到你的类中。以下是一个简单的 @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);
}
}
java

支持的模型

Perplexity 支持多种针对搜索增强的对话式 AI 优化的模型。详情请参阅支持的模型

参考文献