Azure OpenAI 图像模型
Spring AI 项目集成了 Azure OpenAI 的图像生成模型。
添加依赖
将 spring-ai-azure-openai-spring-boot-starter
依赖项添加到您的 pom.xml
文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
配置 Azure OpenAI 凭证
请确保在您的 application.properties
文件中设置了以下属性:
spring.ai.azure.openai.api-key
: 您的 Azure OpenAI API 密钥。
spring.ai.azure.openai.endpoint
: 您的 Azure OpenAI 端点。
spring.ai.azure.openai.api-key=YOUR_AZURE_OPENAI_API_KEY
spring.ai.azure.openai.endpoint=YOUR_AZURE_OPENAI_ENDPOINT
您还需要通过设置 spring.ai.azure.openai.image.enabled=true
属性来启用 Azure OpenAI 图像客户端。
此外,请浏览 Azure OpenAI图像选项 (AzureOpenAiImageOptions) 了解可用的调用选项。常用选项也可以通过 spring.ai.azure.openai.image.options.*
属性进行配置。
调用 ImageClient
ImageClient
接口提供了一种与图像生成模型交互的便携方式。
ImageResponse imageResponse = imageClient.call(
new ImagePrompt("A light cream cat on a purple background") // 示例提示内容通常保留英文
);
ImageClient
会自动配置和注入。
@RestController
public class ImageController {
private final ImageClient imageClient;
@Autowired
public ImageController(ImageClient imageClient) {
this.imageClient = imageClient;
}
@GetMapping("/image")
public String image(@RequestParam String prompt) {
ImageResponse response = imageClient.call(new ImagePrompt(prompt));
// 处理响应
return response.getResult().getOutput().getUrl();
}
}
<Card title="文档有误?请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/spring-ai/api/image/azure-openai-image.mdx">
发现文档问题?点击此处直接在 GitHub 上编辑并提交 PR,帮助我们改进文档!
</Card>