Spring AI接入Google Vertex中Anthropic Claude模型的配置方法咨询
Spring AI接入Google Vertex中Anthropic Claude模型的配置方法咨询
我之前也碰到过类似的情况,官方文档确实主要盯着Gemini在讲,对Vertex Model Garden里的第三方模型(比如Anthropic、Mistral这些)提得很少,不过其实只要找对模型ID,配置起来并不复杂,给你捋捋具体步骤:
一、先确认依赖
首先得确保你的项目里已经引入了Spring AI Vertex的starter依赖,不管是Maven还是Gradle,这个是基础:
- Maven:
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-vertex-ai-spring-boot-starter</artifactId> <version>你的Spring AI版本(建议用1.0.0+稳定版)</version> </dependency>
- Gradle:
implementation 'org.springframework.ai:spring-ai-vertex-ai-spring-boot-starter:你的Spring AI版本'
二、核心配置(application.properties/yml)
基础的GCP项目和区域配置不能少,然后重点是把模型ID换成Anthropic Claude的官方ID,不同版本的Claude对应不同的ID,比如常用的几个:
- Claude 3 Sonnet:
anthropic-claude-3-sonnet@20240229 - Claude 3 Opus:
anthropic-claude-3-opus@20240229 - Claude 3 Haiku:
anthropic-claude-3-haiku@20240307
以properties为例,配置如下:
# GCP基础配置 spring.ai.vertex.ai.project-id=你的GCP项目ID spring.ai.vertex.ai.location=你的部署区域(比如us-central1) # 配置使用Claude模型 spring.ai.vertex.ai.chat.options.model=anthropic-claude-3-sonnet@20240229 # 可选:根据需求调整生成参数,这些参数Claude都支持 spring.ai.vertex.ai.chat.options.temperature=0.7 spring.ai.vertex.ai.chat.options.max-tokens=4096
如果用yml的话:
spring: ai: vertex: ai: project-id: 你的GCP项目ID location: us-central1 chat: options: model: anthropic-claude-3-sonnet@20240229 temperature: 0.7 max-tokens: 4096
三、编程式配置(可选)
如果需要更灵活的控制,比如在代码里动态切换模型,可以手动创建ChatClient的Bean,指定模型ID:
import org.springframework.ai.vertex.api.VertexAiChatOptions; import org.springframework.ai.vertex.chat.VertexAiChatClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class VertexAiConfig { @Bean public VertexAiChatClient vertexAiChatClient(VertexAiChatOptions defaultOptions) { return new VertexAiChatClient(defaultOptions.toBuilder() .setModel("anthropic-claude-3-sonnet@20240229") // 这里也可以直接在代码里设置温度、最大token等参数 .setTemperature(0.7) .setMaxTokens(4096) .build()); } }
四、几个要注意的点
- 先确认你的GCP项目已经在Vertex AI Model Garden中启用了Anthropic的Claude模型,并且当前账号有
Vertex AI User这类能调用模型的权限 - 模型ID要对应正确,不同版本的Claude后缀的日期号会变,要是拿不准可以去GCP控制台的Vertex Model Garden里找Claude模型的详情页,里面会明确标出来
- 尽量用Spring AI 1.0.0以上的版本,更早的版本对第三方模型的适配可能有问题,比如输入输出格式的兼容
这样配置完之后,你用Spring AI的ChatClient接口调用的时候,就会自动走Vertex里的Claude模型了,和用Gemini的调用代码完全一样,不用改业务逻辑~




