爱站seo查询软件沈阳定制网站方案
2026/5/21 18:06:47 网站建设 项目流程
爱站seo查询软件,沈阳定制网站方案,公司介绍ppt模板免费下载,万表网一、Google VertexAI 文本嵌入模型 Vertex AI 支持两种类型的嵌入模型#xff1a;文本嵌入和多模态嵌入。本文档介绍如何使用 Vertex AI 文本嵌入 API 创建文本嵌入。 Vertex AI 文本嵌入 API 使用稠密向量表示。与直接映射单词到数字的稀疏向量不同#xff0c;稠密向量旨在…一、Google VertexAI 文本嵌入模型Vertex AI 支持两种类型的嵌入模型文本嵌入和多模态嵌入。本文档介绍如何使用 Vertex AI 文本嵌入 API 创建文本嵌入。Vertex AI 文本嵌入 API 使用稠密向量表示。与直接映射单词到数字的稀疏向量不同稠密向量旨在更好地表示文本的含义。在生成式 AI 中使用稠密向量嵌入的优点是您可以更好地搜索与查询含义相符的段落而不仅仅是搜索直接的单词或语法匹配即使这些段落使用的语言不同。二、先决条件安装适用于您操作系统的 gcloud CLI。通过运行以下命令进行身份验证。请将 PROJECT_ID 替换为您的 Google Cloud 项目 ID将 替换为您的 Google Cloud 用户名。gcloud configsetprojectPROJECT_IDgcloud auth application-default loginACCOUNT添加仓库和 BOMSpring AI 的构件发布在 Maven Central 和 Spring Snapshot 仓库中。请参阅 构件仓库 部分将这些仓库添加到您的构建系统中。为便于依赖管理Spring AI 提供了 BOM物料清单以确保在整个项目中使用统一版本的 Spring AI。请参阅 依赖管理 部分将 Spring AI BOM 添加到您的构建系统中。三、自动配置Spring AI 的自动配置和启动器模块的构件名称发生了重大变化。更多信息请参阅升级说明。Spring AI 为 VertexAI 嵌入模型提供了 Spring Boot 自动配置。要启用它请在项目的 Maven pom.xml 文件中添加以下依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-vertex-ai-embedding/artifactId/dependency或在您的 Gradle build.gradle 构建文件中添加dependencies{implementationorg.springframework.ai:spring-ai-starter-model-vertex-ai-embedding}请参阅 依赖管理 部分将 Spring AI BOM 添加到您的构建文件中。嵌入属性前缀 spring.ai.vertex.ai.embedding 用作属性前缀用于连接到 VertexAI 嵌入 API。现在通过前缀为 spring.ai.model.embedding 的顶级属性来配置嵌入自动配置的启用和禁用。启用spring.ai.model.embedding.textvertexai默认已启用禁用spring.ai.model.embedding.textnone或任何与 vertexai 不匹配的值此项更改是为了支持配置多个模型。前缀 spring.ai.vertex.ai.embedding.text 是用于配置 VertexAI 文本嵌入的嵌入模型实现的属性前缀。四、示例控制器创建一个新的 Spring Boot 项目并将 spring-ai-starter-model-vertex-ai-embedding 添加到您的 pom或 gradle依赖中。在 src/main/resources 目录下添加一个 application.properties 文件以启用和配置 VertexAi 聊天模型spring.ai.vertex.ai.embedding.project-idYOUR_PROJECT_IDspring.ai.vertex.ai.embedding.locationYOUR_PROJECT_LOCATIONspring.ai.vertex.ai.embedding.text.options.modeltext-embedding-004这将创建一个 VertexAiTextEmbeddingModel 实现您可以将其注入到您的类中。以下是一个使用嵌入模型生成嵌入的简单 Controller 类示例。RestControllerpublicclassEmbeddingController{privatefinalEmbeddingModelembeddingModel;AutowiredpublicEmbeddingController(EmbeddingModelembeddingModel){this.embeddingModelembeddingModel;}GetMapping(/ai/embedding)publicMapembed(RequestParam(valuemessage,defaultValueTell me a joke)Stringmessage){EmbeddingResponseembeddingResponsethis.embeddingModel.embedForResponse(List.of(message));returnMap.of(embedding,embeddingResponse);}}五、手动配置VertexAiTextEmbeddingModel 实现了 EmbeddingModel 接口。在项目的 Maven pom.xml 文件中添加 spring-ai-vertex-ai-embedding 依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-vertex-ai-embedding/artifactId/dependency或在您的 Gradle build.gradle 构建文件中添加dependencies{implementationorg.springframework.ai:spring-ai-vertex-ai-embedding}请参阅依赖管理部分将 Spring AI BOM 添加到您的构建文件中。接下来创建一个 VertexAiTextEmbeddingModel 并使用它进行文本生成VertexAiEmbeddingConnectionDetailsconnectionDetailsVertexAiEmbeddingConnectionDetails.builder().projectId(System.getenv(VERTEX_AI_GEMINI_PROJECT_ID)).location(System.getenv(VERTEX_AI_GEMINI_LOCATION)).build();VertexAiTextEmbeddingOptionsoptionsVertexAiTextEmbeddingOptions.builder().model(VertexAiTextEmbeddingOptions.DEFAULT_MODEL_NAME).build();varembeddingModelnewVertexAiTextEmbeddingModel(this.connectionDetails,this.options);EmbeddingResponseembeddingResponsethis.embeddingModel.embedForResponse(List.of(Hello World,World is big and salvation is near));从 Google 服务账户加载凭据要以编程方式从服务账户 JSON 文件加载 GoogleCredentials您可以使用以下代码GoogleCredentialscredentialsGoogleCredentials.fromStream(INPUT_STREAM_TO_CREDENTIALS_JSON).createScoped(https://www.googleapis.com/auth/cloud-platform);credentials.refreshIfExpired();VertexAiEmbeddingConnectionDetailsconnectionDetailsVertexAiEmbeddingConnectionDetails.builder().projectId(System.getenv(VERTEX_AI_GEMINI_PROJECT_ID)).location(System.getenv(VERTEX_AI_GEMINI_LOCATION)).apiEndpoint(endpoint).predictionServiceSettings(PredictionServiceSettings.newBuilder().setEndpoint(endpoint).setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build());

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询