VertexAI PaLM2 聊天

生成式语言 PaLM API 允许开发者使用 PaLM 模型构建生成式 AI 应用程序。大型语言模型(LLMs)是一种强大、多功能的机器学习模型,通过一系列提示使计算机理解和生成自然语言。PaLM API 基于谷歌的下一代 LLM,PaLM。它在代码生成、推理和写作等各种不同任务方面表现出色。您可以使用 PaLM API 构建生成式 AI 应用程序,用例包括内容生成、对话代理、摘要和分类系统等。

基于 模型 REST API

先决条件

要访问 PaLM2 REST API,您需要从 makersuite 获取访问 API 密钥。

目前,PaLM API 在美国以外不可用,但您可以使用 VPN 进行测试。

Spring AI 项目定义了一个名为 spring.ai.vertex.ai.api-key 的配置属性,您应该将其设置为获取的 API 密钥 的值。导出环境变量是设置该配置属性的一种方法:

export SPRING_AI_VERTEX_AI_API_KEY=<在此插入密钥>

添加存储库和 BOM

Spring AI 构件发布在 Spring 里程碑和快照存储库中。请参考 存储库 部分将这些存储库添加到您的构建系统中。

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

自动配置

Spring AI(Spring AI)为VertexAI聊天客户端提供了Spring Boot自动配置。要启用它,请将以下依赖项添加到您项目的Maven pom.xml文件中:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-vertex-ai-palm2-spring-boot-starter</artifactId>
</dependency>

或者添加到您的Gradle build.gradle构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-palm2-spring-boot-starter'
}
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。

聊天属性

前缀spring.ai.vertex.ai用作属性前缀,可让您连接到VertexAI。

属性 描述 默认值

spring.ai.vertex.ai.ai.base-url

连接到的URL

generativelanguage.googleapis.com/v1beta3

spring.ai.vertex.ai.api-key

API密钥

-

前缀spring.ai.vertex.ai.chat是用来配置连接到VertexAI聊天客户端实现的属性前缀。

属性 描述 默认值

聊天选项

VertexAiPaLm2ChatOptions.java 提供模型配置,例如温度、topK等。

在启动时,可以使用 VertexAiPaLm2ChatClient(api, options) 构造函数或 spring.ai.vertex.ai.chat.options.* 属性来配置默认选项。

在运行时,您可以通过将新的、请求特定的选项添加到 Prompt 调用中来覆盖默认选项。例如,要覆盖特定请求的默认温度:

ChatResponse response = chatClient.call(
    new Prompt(
        "生成5位著名海盗的姓名。",
        VertexAiPaLm2ChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));
除了特定于模型的 VertexAiPaLm2ChatOptions 外,还可以使用可移植的 ChatOptions 实例,使用 ChatOptionsBuilder#builder() 创建。

示例控制器(自动配置)

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

src/main/resources 目录下添加一个 application.properties 文件,以启用并配置 VertexAi Chat 客户端:

spring.ai.vertex.ai.api-key=YOUR_API_KEY
spring.ai.vertex.ai.chat.model=chat-bison-001
spring.ai.vertex.ai.chat.options.temperature=0.5
api-key 替换为您的 VertexAI 凭据。

这将创建一个 VertexAiPaLm2ChatClient 实现,您可以将其注入到您的类中。下面是一个使用聊天客户端进行文本生成的简单 @Controller 类示例。

@RestController
public class ChatController {

    private final VertexAiPaLm2ChatClient chatClient;

    @Autowired
    public ChatController(VertexAiPaLm2ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatClient.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 chatClient.stream(prompt);
    }
}

手动配置

这个VertexAiPaLm2ChatClient实现了ChatClient并使用低级别VertexAiPaLm2Api客户端来连接到VertexAI服务。

spring-ai-vertex-ai-palm2依赖项添加到您项目的 Maven pom.xml 文件中:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-vertex-ai-palm2</artifactId>
</dependency>

或者添加到您的 Gradle build.gradle 构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-palm'
}
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。

接下来,创建一个VertexAiPaLm2ChatClient并将其用于文本生成:

VertexAiPaLm2Api vertexAiApi = new VertexAiPaLm2Api(< 您的PALM_API_KEY>);

var chatClient = new VertexAiPaLm2ChatClient(vertexAiApi,
    VertexAiPaLm2ChatOptions.builder()
        .withTemperature(0.4)
    .build());

ChatResponse response = chatClient.call(
    new Prompt("生成5个著名海盗的名字。"));

VertexAiPaLm2ChatOptions提供了聊天请求的配置信息。 VertexAiPaLm2ChatOptions.Builder是一个流畅的选项构建器。

低级别VertexAiPaLm2Api客户端

VertexAiPaLm2Api是VertexAiPaLm2Api聊天API的轻量级Java客户端。

以下类图说明了VertexAiPaLm2Api聊天接口和构建块:

<