Skip to content

Latest commit

 

History

History
187 lines (135 loc) · 7.54 KB

quickstart-openai-generate-images.md

File metadata and controls

187 lines (135 loc) · 7.54 KB
title description ms.date ms.topic ms.custom author ms.author zone_pivot_groups
Quickstart - Generate images using Azure AI with .NET
Create a simple app using Semantic Kernel or the .NET Azure OpenAI SDK to generate postal card images.
03/04/2024
quickstart
devx-track-dotnet, devx-track-dotnet-ai
fboucher
frbouche
openai-library

Generate images using Azure AI with .NET

:::zone target="docs" pivot="semantic-kernel"

Get started with Semantic Kernel by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI dell-e-3 model to generate postal card and invite your friends for a hike! Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel.

:::zone-end

:::zone target="docs" pivot="azure-openai-sdk"

Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI dell-e-3 model to generate postal card and invite your friends for a hike! Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK.

:::zone-end

[!INCLUDE download-alert]

Trying Generate Hiking Images sample

:::zone target="docs" pivot="semantic-kernel"

  1. From a terminal or command prompt, navigate to the semantic-kernel\05-HikeImages directory.

:::zone-end

:::zone target="docs" pivot="azure-openai-sdk"

  1. From a terminal or command prompt, navigate to the azure-openai-sdk\05-HikeImages directory.

:::zone-end

  1. It's now time to try the console application. Type in the following to run the app:

    dotnet run
    

    If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again.

:::zone target="docs" pivot="semantic-kernel"

Understanding the code

Our application uses the Microsoft.SemanticKernel package, which is available on NuGet, to send and receive requests to an Azure OpenAI service deployed in Azure.

The entire application is contained within the Program.cs file. The first several lines of code load secrets and configuration values that were set in the dotnet user-secrets for you during the application provisioning.

// == 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"];

The AzureOpenAITextToImageService service facilitates the requests and responses.

AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, key, null);

Once the textToImageService service is created, we we provide more context to the model by adding a system prompt. A good prompt to generate images requires a clear description: what is in the images, specific color to use, style (drawing, painting, realistic or cartoony). The model will use this prompt to generate the image. To have the model generate a response based off the user request, use the GenerateImageAsync function, and specify the size and quality.

// 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}");

Customize the prompt to personalize the images generated by the model.

:::zone-end

:::zone target="docs" pivot="azure-openai-sdk"

Understanding the code

Our application uses the Azure.AI.OpenAI client SDK, which is available on NuGet, to send and receive requests to an Azure OpenAI service deployed in Azure.

The entire application is contained within the Program.cs file. The first several lines of code load secrets and configuration values that were set in the dotnet user-secrets for you during the application provisioning.

// == 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);

The OpenAIClient class facilitates the requests and responses. ChatCompletionOptions specifies parameters of how the model will respond.

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
};

Once the OpenAIClient client is created, we we provide more context to the model by adding a system prompt. A good prompt to generate images requires a clear description: what is in the images, specific color to use, style (drawing, painting, realistic or cartoony). The model will use this prompt to generate the image.

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!'
""";

To have the model generate a response based off the user request, use the GetImageGenerationsAsync function, and specify the size and quality.

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}");

Customize the prompt to personalize the images generated by the model.

:::zone-end

Clean up resources

When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

azd down

Next steps