运行时选项
GoogleGenAiChatOptions提供模型配置,如温度、topK等。
启动时,可以使用GoogleGenAiChatModel(client, options)构造函数或spring.ai.google.genai.chat.options.*属性来配置默认选项。
在运行时,您可以通过向Prompt调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认温度:
ChatResponse response = chatModel.call(new Prompt("Generate the names of 5 famous pirates.",GoogleGenAiChatOptions.builder().temperature(0.4).build()));
除了特定于模型的GoogleGenAiChatOptions之外,您还可以使用一个可移植的ChatOptions实例,该实例是通过ChatOptions#builder()创建的。
工具调用
谷歌GenAI模型支持工具调用(函数调用)功能,允许模型在对话过程中使用工具。以下是如何定义和使用基于@Tool的工具的示例:
public class WeatherService {@Tool(description = "Get the weather in location")public String weatherByLocation(@ToolParam(description= "City or state name") String location) {...}}String response = ChatClient.create(this.chatModel).prompt("What's the weather like in Boston?").tools(new WeatherService()).call().content();
你也可以将java.util.function beans作为工具来使用:
@Bean@Description("Get the weather in location. Return temperature in 36°F or 36°C format.")public Function<Request, Response> weatherFunction() {return new MockWeatherService();}String response = ChatClient.create(this.chatModel).prompt("What's the weather like in Boston?").toolNames("weatherFunction").inputType(Request.class).call().content();
多模态
多模态指的是一个模型能够同时理解和处理来自各种(输入)源的信息,包括文本、PDF、图像、音频和其他数据格式。
图像,音频,视频
谷歌的Gemini人工智能模型通过理解和整合文本、代码、音频、图像和视频来支持这一功能。
Spring AI的消息接口通过引入Media类型来支持多模态AI模型。此类型包含消息中媒体附件的数据和信息,使用Spring的org.springframework.util.MimeType和java.lang.Object来表示原始媒体数据。
以下是从GoogleGenAiChatModelIT中提取的一个简单代码示例,展示了如何将用户文本与图像相结合。
byte[] data = new ClassPathResource("/vertex-test.png").getContentAsByteArray();var userMessage = UserMessage.builder().text("Explain what do you see o this picture?").media(List.of(new Media(MimeTypeUtils.IMAGE_PNG, data))).build();ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));
Google GenAI 支持 PDF 输入类型。使用 application/pdf 媒体类型将 PDF 文件附加到消息中:
var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");var userMessage = UserMessage.builder().text("You are a very professional document summarization specialist. Please summarize the given document.").media(List.of(new Media(new MimeType("application", "pdf"), pdfData))).build();var response = this.chatModel.call(new Prompt(List.of(userMessage)));
采样控制器
创建一个新的Spring Boot项目,并将spring-ai-starter-model-google-genai添加到您的pom(或gradle)依赖项中。
在src/main/resources目录下添加一个application.properties文件,以启用和配置Google GenAI聊天模型:
使用Gemini Developer API(API密钥)
spring.ai.google.genai.api-key=YOUR_API_KEYspring.ai.google.genai.chat.options.model=gemini-2.0-flashspring.ai.google.genai.chat.options.temperature=0.5
使用Vertex AI
spring.ai.google.genai.project-id=PROJECT_IDspring.ai.google.genai.location=LOCATIONspring.ai.google.genai.chat.options.model=gemini-2.0-flashspring.ai.google.genai.chat.options.temperature=0.5
将`project-id`替换为您的Google Cloud项目ID,`location`替换为Google Cloud区域,如us-central1、europe-west1等。
每个模型都有其支持的地区列表,您可以在模型页面上找到该列表。
这将创建一个GoogleGenAiChatModel实现,你可以将其注入到你的类中。以下是一个使用聊天模型进行文本生成的简单@Controller类的示例。
@RestControllerpublic class ChatController {private final GoogleGenAiChatModel chatModel;@Autowiredpublic ChatController(GoogleGenAiChatModel chatModel) {this.chatModel = chatModel;}@GetMapping("/ai/generate")public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return Map.of("generation", this.chatModel.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 this.chatModel.stream(prompt);}}