配合使用 Azure AI 和 .NET 生成图像
通过创建简单的 .NET 8 控制台聊天应用程序,开始使用语义内核。 应用程序将在本地运行,并使用 OpenAI dell-e-3
模型生成明信片,邀请你的朋友去徒步旅行! 按照以下步骤预配 Azure OpenAI,并了解如何使用语义内核。
通过创建简单的 .NET 8 控制台聊天应用程序,开始使用 .NET Azure OpenAI SDK。 应用程序将在本地运行,并使用 OpenAI dell-e-3
模型生成明信片,邀请你的朋友去徒步旅行! 按照以下步骤预配 Azure OpenAI,并了解如何使用 .NET Azure OpenAI SDK。
先决条件
- .NET 8.0 SDK - 安装 .NET 8.0 SDK
- Azure 订阅 - 免费创建订阅
- Azure Developer CLI - 安装或更新 Azure Developer CLI
- 访问 Azure OpenAI 服务。
- 在 Windows 上,必须安装 PowerShell
v7+
。 若要验证版本,请在终端中运行pwsh
。 此命令会返回当前版本。 如果返回错误,请执行以下命令dotnet tool update --global PowerShell
。
部署 Azure 资源
确保遵循先决条件以访问 Azure OpenAI 服务以及 Azure Developer CLI,然后按照以下指南设置并开始使用示例应用程序。
克隆存储库:dotnet/ai-samples
在终端或命令提示符下,导航到 quickstarts 目录。
这会预配 Azure OpenAI 资源。 创建 Azure OpenAI 服务并部署模型可能需要几分钟时间。
azd up
注意
如果已有可用的 Azure OpenAI 服务,则可跳过部署并在 Program.cs 中使用该值(最好是从 IConfiguration
中使用)。
疑难解答
在 Windows 上,运行 azd up
后,可能会收到以下错误消息:
postprovision.ps1 未进行数字签名。 该脚本不会在系统上执行
执行脚本 postprovision.ps1 以设置应用程序中使用的 .NET 用户机密。 若要避免此错误,请运行以下 PowerShell 命令:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
然后,重新运行 azd up
命令。
另一个可能的错误:
“pwsh”没有被识别为内部或外部命令、可运行程序或批处理文件。 警告:“postprovision”挂钩失败,退出代码为“1”,路径:“.\infra\post-script\postprovision.ps1”。 :退出代码:1 执行将继续,因为 ContinueOnError 已设置为 true。
执行脚本 postprovision.ps1 以设置应用程序中使用的 .NET 用户机密。 若要避免此错误,请使用以下 PowerShell 命令手动运行脚本:
.\infra\post-script\postprovision.ps1
.NET AI 应用现已配置了用户机密,可以对其进行测试。
尝试“生成徒步旅行图像”示例
- 在终端或命令提示符下,导航到
semantic-kernel\05-HikeImages
目录。
- 在终端或命令提示符下,导航到
azure-openai-sdk\05-HikeImages
目录。
现在可试用控制台应用程序了。 键入以下内容以运行应用:
dotnet run
如果收到错误消息,Azure OpenAI 资源可能尚未完成部署。 等待几分钟,然后重试。
了解代码
我们的应用程序使用 NuGet 上提供的 Microsoft.SemanticKernel
包向 Azure 中部署的 Azure OpenAI 服务发送和接收请求。
整个应用程序包含在 Program.cs 文件中。 前几行代码加载在应用程序预配期间在 dotnet user-secrets
中设置的机密和配置值。
// == Retrieve the local secrets saved during the Azure deployment ==========
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.Build();
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];
AzureOpenAITextToImageService
服务有助于请求和响应。
AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, key, null);
创建 textToImageService
服务后,我们通过添加系统提示向模型提供更多上下文。 生成图像的良好提示需要清晰的说明:图像中的内容、要使用的具体颜色、风格(绘图、绘画、写实或卡通)。 模型将使用此提示来生成图像。 若要让模型根据用户请求生成响应,请使用 GenerateImageAsync
函数,并指定大小和质量。
// Generate the image
string imageUrl = await textToImageService.GenerateImageAsync("""
A postal card with an happy hiker waving and a beautiful mountain in the background.
There is a trail visible in the foreground.
The postal card has text in red saying: 'You are invited for a hike!'
""", 1024, 1024);
Console.WriteLine($"The generated image is ready at:\n{imageUrl}");
自定义提示以对模型生成的图像进行个性化设置。
了解代码
应用程序使用 NuGet 上提供的 Azure.AI.OpenAI
客户端 SDK 向 Azure 中部署的 Azure OpenAI 服务发送和接收请求。
整个应用程序包含在 Program.cs 文件中。 前几行代码加载在应用程序预配期间在 dotnet user-secrets
中设置的机密和配置值。
// == Retrieve the local secrets saved during the Azure deployment ==========
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.Build();
string openAIEndpoint = config["AZURE_OPENAI_ENDPOINT"];
string openAIDeploymentName = config["AZURE_OPENAI_GPT_NAME"];
string openAiKey = config["AZURE_OPENAI_KEY"];
// == Creating the AIClient ==========
var endpoint = new Uri(openAIEndpoint);
var credentials = new AzureKeyCredential(openAiKey);
OpenAIClient
类有助于请求和响应。 ChatCompletionOptions
指定模型响应方式的参数。
var openAIClient = new OpenAIClient(endpoint, credentials);
var completionOptions = new ChatCompletionsOptions
{
MaxTokens = 400,
Temperature = 1f,
FrequencyPenalty = 0.0f,
PresencePenalty = 0.0f,
NucleusSamplingFactor = 0.95f, // Top P
DeploymentName = openAIDeploymentName
};
创建 OpenAIClient
客户端后,我们通过添加系统提示向模型提供更多上下文。 生成图像的良好提示需要清晰的说明:图像中的内容、要使用的具体颜色、风格(绘图、绘画、写实或卡通)。 模型将使用此提示来生成图像。
string imagePrompt = """
A postal card with an happy hiker waving, there a beautiful mountain in the background.
There is a trail visible in the foreground.
The postal card has text in red saying: 'You are invited for a hike!'
""";
若要让模型根据用户请求生成响应,请使用 GetImageGenerationsAsync
函数,并指定大小和质量。
Response<ImageGenerations> response = await openAIClient.GetImageGenerationsAsync(
new ImageGenerationOptions()
{
DeploymentName = openAIDalleName,
Prompt = imagePrompt,
Size = ImageSize.Size1024x1024,
Quality = ImageGenerationQuality.Standard
});
ImageGenerationData generatedImage = response.Value.Data[0];
if (!string.IsNullOrEmpty(generatedImage.RevisedPrompt))
{
Console.WriteLine($"\n\nInput prompt automatically revised to:\n {generatedImage.RevisedPrompt}");
}
Console.WriteLine($"\n\nThe generated image is ready at:\n {generatedImage.Url.AbsoluteUri}");
自定义提示以对模型生成的图像进行个性化设置。
清理资源
不再需要示例应用程序或资源时,请删除相应的部署和所有资源。
azd down
后续步骤
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈