VertexAI 嵌入
Generative Language PaLM API 允许开发人员使用 PaLM 模型构建生成式 AI 应用程序。 大型语言模型(LLMs)是一种强大而多才多艺的机器学习模型,通过一系列提示使计算机能够理解和生成自然语言。 PaLM API 基于 Google 的下一代 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=<在此处插入密钥>
自动配置
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 |
|
|
spring.ai.vertex.ai.api-key |
API密钥 |
- |
前缀spring.ai.vertex.ai.embedding是允许您配置VertexAI聊天嵌入客户端实现的属性前缀。
| 属性 | 描述 | 默认值 |
|---|---|---|
|
spring.ai.vertex.ai.embedding.enabled |
启用Vertex AI PaLM API嵌入客户端。 |
true |
|
spring.ai.vertex.ai.embedding.model |
这是要使用的Vertex嵌入模型 |
embedding-gecko-001 |
示例控制器(自动配置)
创建一个新的Spring Boot项目,并将spring-ai-vertex-ai-palm2-spring-boot-starter添加到您的pom(或gradle)依赖中。
在src/main/resources目录下添加一个application.properties文件,以启用和配置VertexAi聊天客户端:
spring.ai.vertex.ai.api-key=您的API密钥
spring.ai.vertex.ai.embedding.model=embedding-gecko-001
将api-key替换为您的VertexAI凭据。 |
这将创建一个VertexAiPaLm2EmbeddingClient实现,您可以将其注入到您的类中。以下是一个使用嵌入客户端生成文本的简单@Controller类的示例。
@RestController
public class EmbeddingController {
private final EmbeddingClient embeddingClient;
@Autowired
public EmbeddingController(EmbeddingClient embeddingClient) {
this.embeddingClient = embeddingClient;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
return Map.of("embedding", embeddingResponse);
}
}
手动配置
VertexAiPaLm2EmbeddingClient 实现了 EmbeddingClient 并使用 低级 VertexAiPaLm2Api 客户端 连接到 VertexAI 服务。
将 spring-ai-vertex-ai 依赖项添加到您项目的 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-palm2'
}
| 参考依赖管理部分,将 Spring AI BOM 添加到您的构建文件中。 |
接下来,创建一个 VertexAiPaLm2EmbeddingClient 并用它进行文本生成:
VertexAiPaLm2Api vertexAiApi = new VertexAiPaLm2Api(< YOUR PALM_API_KEY>);
var embeddingClient = new VertexAiPaLm2EmbeddingClient(vertexAiApi);
EmbeddingResponse embeddingResponse = embeddingClient
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
低级 VertexAiPaLm2Api 客户端
VertexAiPaLm2Api 提供了适用于 VertexAiPaLm2Api 聊天 API 的轻量级 Java 客户端。
下图显示了 VertexAiPaLm2Api 嵌入接口和构建块:
这是使用该 API 进行编程的简单片段:
VertexAiPaLm2Api vertexAiApi = new VertexAiPaLm2Api(< YOUR PALM_API_KEY>);
// 生成
var prompt = new MessagePrompt(List.of(new Message("0", "Hello, how are you?")));
GenerateMessageRequest request = new GenerateMessageRequest(prompt);
GenerateMessageResponse response = vertexAiApi.generateMessage(request);
// 嵌入文本
Embedding embedding = vertexAiApi.embedText("Hello, how are you?");
// 批量嵌入
List<Embedding> embeddings = vertexAiApi.batchEmbedText(List.of("Hello, how are you?", "I am fine, thank you!"));