使用VertexAI API在Kotlin创建Gemini缓存内容时遇404错误排查
Kotlin创建Gemini缓存内容触发404错误的问题
问题代码
我尝试用以下Kotlin代码创建Gemini缓存内容:
import com.google.cloud.aiplatform.v1.* import com.google.protobuf.Duration val instructions = "hello ".repeat(40000) val cachedContent = CachedContent.newBuilder() .setSystemInstruction( Content.newBuilder().addParts(Part.newBuilder().setText(instructions)).build() ) .setName("example-cache-2") .setTtl(Duration.newBuilder().setSeconds(60 * 60).build()) .setModel("gemini-1.5-flash") .build() val request = CreateCachedContentRequest.newBuilder().setCachedContent(cachedContent).build() GenAiCacheServiceClient.create().createCachedContent(request)
触发的错误
执行代码后出现如下错误:
[...] <p>The requested URL <code>/google.cloud.aiplatform.v1.GenAiCacheService/CreateCachedContent</code> was not found on this server. <ins>That’s all we know.</ins> [...] com.google.api.gax.rpc.UnimplementedException: io.grpc.StatusRuntimeException: UNIMPLEMENTED: HTTP status code 404 [...]
已验证的正常情况
- 环境变量配置正确:
GOOGLE_APPLICATION_CREDENTIALS已正确指向服务账号文件,以下调用模型的Kotlin代码可正常运行:
import com.google.cloud.vertexai.VertexAI import com.google.cloud.vertexai.api.GenerationConfig import com.google.cloud.vertexai.generativeai.ContentMaker import com.google.cloud.vertexai.generativeai.GenerativeModel import com.google.cloud.vertexai.generativeai.ResponseHandler fun foo() { val vertexAi = VertexAI(MY_PROJECT, "us-central1") val model = GenerativeModel.Builder() .setModelName("gemini-1.5-flash") .setVertexAi(vertexAi) .setSystemInstruction(ContentMaker.fromString("Hi!")!!) .build() val response = ResponseHandler.getText( model .withGenerationConfig(GenerationConfig.newBuilder().setTemperature(0.0f).build()) .generateContent(listOf(ContentMaker.forRole("user").fromString("Hello."))) ) println(response) }
- 服务账号权限足够:使用同一服务账号的Python代码可成功创建缓存内容:
import datetime import os import random import string import vertexai from vertexai.generative_models import Part from vertexai.preview import caching vertexai.init(project=MY_PROJECT, location="us-central1") long_text = "hello " * 40000 cached_content = caching.CachedContent.create( model_name="gemini-1.5-flash-002", system_instruction="Hi!", contents=[Part.from_text(long_text)], ttl=datetime.timedelta(minutes=60), display_name="example-cache", ) print(cached_content.name)
当前依赖版本
使用的依赖库为最新版本:
implementation(group = "com.google.cloud", name = "google-cloud-vertexai", version = "1.18.0") implementation(group = "com.google.cloud", name = "google-cloud-aiplatform", version = "3.59.0")
问题分析与解决
你的Kotlin代码存在三个关键问题:
未指定服务端点与父资源路径
GenAiCacheServiceClient.create()默认端点不支持缓存服务,需要显式指定区域端点,同时必须在请求中设置项目+区域的父资源路径,否则服务无法定位到你的资源空间。错误设置了
name字段CachedContent的name是服务端生成的资源标识,不能手动设置;如果需要自定义显示名称,应该使用setDisplayName()方法。模型ID不完整
需要使用带版本号的完整模型ID(如gemini-1.5-flash-002),而非仅模型名称,否则服务无法识别支持缓存的特定模型版本。
修改后的代码示例
import com.google.cloud.aiplatform.v1.* import com.google.protobuf.Duration import com.google.api.gax.core.FixedCredentialsProvider import com.google.auth.oauth2.GoogleCredentials import java.io.File // 替换为你的项目ID和区域 val projectId = "MY_PROJECT" val location = "us-central1" val endpoint = "$location-aiplatform.googleapis.com:443" // 加载凭据并配置客户端 val credentials = GoogleCredentials.fromStream(File(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")).inputStream()) val clientSettings = GenAiCacheServiceSettings.newBuilder() .setEndpoint(endpoint) .setCredentialsProvider(FixedCredentialsProvider.create(credentials)) .build() val instructions = "hello ".repeat(40000) val cachedContent = CachedContent.newBuilder() .setSystemInstruction(Content.newBuilder().addParts(Part.newBuilder().setText(instructions)).build()) .setDisplayName("example-cache-2") // 使用自定义显示名称 .setTtl(Duration.newBuilder().setSeconds(60 * 60).build()) .setModel("gemini-1.5-flash-002") // 使用完整模型版本ID .build() val request = CreateCachedContentRequest.newBuilder() .setCachedContent(cachedContent) .setParent("projects/$projectId/locations/$location") // 设置父资源路径 .build() // 使用use块自动管理客户端生命周期 GenAiCacheServiceClient.create(clientSettings).use { client -> val response = client.createCachedContent(request) println("缓存内容创建成功:${response.name}") }
内容的提问来源于stack exchange,提问作者Tobias Hermann
相关产品推荐
相关产品推荐

