运行时选项
AzureOpenAiChatOptions提供模型配置,如要使用的模型、温度、频率惩罚等。
在启动时,可以使用AzureOpenAiChatModel(api, options)构造函数或spring.ai.azure.openai.chat.options.*属性来配置默认选项。
在运行时,您可以通过向Prompt调用添加新的、特定于请求的选项来覆盖默认选项。例如,要为特定请求覆盖默认模型和温度:
ChatResponse response = chatModel.call(new Prompt("Generate the names of 5 famous pirates.",AzureOpenAiChatOptions.builder().deploymentName("gpt-4o").temperature(0.4).build()));
除了特定于模型的AzureOpenAiChatOptions.java之外,您还可以使用一个便携式的ChatOptions实例,该实例是通过ChatOptions#builder()创建的。
函数调用
您可以使用AzureOpenAiChatModel注册自定义Java函数,并让模型智能地选择输出一个JSON对象,该对象包含调用一个或多个已注册函数的参数。这是一种将大型语言模型(LLM)功能与外部工具和应用程序编程接口(API)连接起来的强大技术。
多模态
多模态是指模型能够同时理解和处理来自不同来源的信息,包括文本、图像、音频和其他数据格式。目前,Azure OpenAI的GPT-4O模型提供了多模态支持。
Azure OpenAI可以将一系列base64编码的图像或图像URL与消息结合在一起。Spring AI的消息接口通过引入Media类型来促进多模态AI模型的发展。此类型包含消息中媒体附件的数据和详细信息,并使用Spring的org.springframework.util.MimeType和java.lang.Object来表示原始媒体数据。
以下是从OpenAiChatModelIT中摘录的代码示例,展示了如何使用GPT_4_O模型将用户文本与图像融合。
URL url = new URL("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png");String response = ChatClient.create(chatModel).prompt().options(AzureOpenAiChatOptions.builder().deploymentName("gpt-4o").build()).user(u -> u.text("Explain what do you see on this picture?").media(MimeTypeUtils.IMAGE_PNG, this.url)).call().content();
它接收multimodal.test.png图像作为输入:

伴随着信息“解释一下你在这张图片上看到了什么?”,并生成如下回复:
This is an image of a fruit bowl with a simple design. The bowl is made of metal with curved wire edges thatcreate an open structure, allowing the fruit to be visible from all angles. Inside the bowl, there are twoyellow bananas resting on top of what appears to be a red apple. The bananas are slightly overripe, asindicated by the brown spots on their peels. The bowl has a metal ring at the top, likely to serve as a handlefor carrying. The bowl is placed on a flat surface with a neutral-colored background that provides a clearview of the fruit inside.
您也可以像下面的示例所示,传入一个类路径资源而不是URL
Resource resource = new ClassPathResource("multimodality/multimodal.test.png");String response = ChatClient.create(chatModel).prompt().options(AzureOpenAiChatOptions.builder().deploymentName("gpt-4o").build()).user(u -> u.text("Explain what do you see on this picture?").media(MimeTypeUtils.IMAGE_PNG, this.resource)).call().content();
采样控制器
创建一个新的Spring Boot项目,并将spring-ai-starter-model-azure-openai添加到您的pom(或gradle)依赖项中。
在src/main/resources目录下添加一个application.properties文件,以启用和配置OpenAi聊天模型:
spring.ai.azure.openai.api-key=YOUR_API_KEYspring.ai.azure.openai.endpoint=YOUR_ENDPOINTspring.ai.azure.openai.chat.options.deployment-name=gpt-4ospring.ai.azure.openai.chat.options.temperature=0.7
将api-key和endpoint替换为您的Azure OpenAI凭据。
这将创建一个AzureOpenAiChatModel实现,你可以将其注入到你的类中。以下是一个使用聊天模型进行文本生成的简单@Controller类的示例。
@RestControllerpublic class ChatController {private final AzureOpenAiChatModel chatModel;@Autowiredpublic ChatController(AzureOpenAiChatModel 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);}}
手动配置
AzureOpenAiChatModel实现了ChatModel和StreamingChatModel接口,并使用了Azure OpenAI Java客户端。
要启用它,请将 spring-ai-azure-openai 依赖项添加到项目的 Maven pom.xml 文件中:
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-azure-openai</artifactId></dependency>
或者添加到你的Gradle build.gradle构建文件中。
dependencies {implementation 'org.springframework.ai:spring-ai-azure-openai'}
spring-ai-azure-openai 依赖项还提供了对 AzureOpenAiChatModel 的访问。
接下来,创建一个AzureOpenAiChatModel实例,并使用它来生成文本回复:
var openAIClientBuilder = new OpenAIClientBuilder().credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY"))).endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"));var openAIChatOptions = AzureOpenAiChatOptions.builder().deploymentName("gpt-5").temperature(0.4).maxCompletionTokens(200).build();var chatModel = AzureOpenAiChatModel.builder().openAIClientBuilder(openAIClientBuilder).defaultOptions(openAIChatOptions).build();ChatResponse response = chatModel.call(new Prompt("Generate the names of 5 famous pirates."));// Or with streaming responsesFlux<ChatResponse> streamingResponses = chatModel.stream(new Prompt("Generate the names of 5 famous pirates."));
gpt-4o实际上是Azure AI门户中显示的部署名称。