Constructing Picture Technology Apps with Azure OpenAI in .NET

Harsh Bakshi
4 min readSep 29, 2024

--

On this article, we’ll discover the best way to use Azure OpenAI DALL-E service in .NET to generate photographs primarily based on consumer prompts. By leveraging Azure OpenAI’s highly effective DALL·E mannequin, we are able to create distinctive picture content material dynamically primarily based on our immediate. We’ll stroll by means of an instance of how to connect with Azure’s OpenAI API, submit a immediate, and retrieve generated photographs in .NET Functions.

Conditions

Earlier than we begin, guarantee now we have the next.

  • Azure Subscription: You’ll need entry to the Azure OpenAI service.
  • OpenAI Useful resource: Arrange an OpenAI useful resource in your Azure account.
  • API Key and Endpoint: Retrieve the important thing and endpoint on your Azure OpenAI useful resource.
  • Visual Studio or .NET SDK: Be sure you have a working .NET atmosphere.

Step 1. Arrange Azure OpenAI in Azure Portal

  1. Create an OpenAI useful resource in Azure
  • Navigate to the Azure Portal, seek for “Azure OpenAI” and click on Create.
  • Present primary particulars like useful resource group, area, and pricing tier.
  • After the useful resource is deployed, yow will discover the API endpoint and key within the Keys and Endpoints part.
Endpoints section
  1. Deploy DALL-E: Below our OpenAI useful resource, go to the Fashions deployments part and deploy the `dall-e-3` mannequin. We are going to use this within the API calls later in our functions.
OpenAI

Step 2. Create a .NET C# Mission

  1. Create a brand new Console Utility: Open Visual Studio and create a brand new Console App utilizing .NET 6.0+.
 Visual Studio
  1. Set up NuGet Packages
  • Set up the shopper library for .NET with NuGet:
  • dotnet add bundle Azure.AI.OpenAI — version 1.0.0-beta.12

Step 3. Write Code to DALL-E App

utilizing Azure.AI.OpenAI;
utilizing Azure;
namespace GenerativeAI

public class AzureOpenAIGPT

// Change with your individual key and endpoint
const string key = "***********************************";
const string endpoint = "https://*************.openai.azure.com/";
const string deploymentOrModelName = "imagedemo";
public async Job GetContent(string immediate)

var shopper = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
var imageGenerationOptions = new ImageGenerationOptions

Immediate = immediate,
DeploymentName = deploymentOrModelName,
Measurement = new ImageSize("1024x1024"),
ImageCount = 1
;
var response = await shopper.GetImageGenerationsAsync(imageGenerationOptions);
return response.Worth.Knowledge[0].Url.AbsoluteUri;

class Program

static async Job Predominant(string[] args)

var dalle = new AzureOpenAIGPT();
var dallePrompt = "Cat consuming cake on the sky";
Console.WriteLine($"await dalle.GetContent(dallePrompt)");
Console.ReadLine();

Key Parts

  1. API Key and Endpoint: The important thing and endpoint are crucial to authenticate and talk with Azure OpenAI. These values are particular to our Azure subscription and OpenAI useful resource.
  • const string key = "your-azure-key";
    const string endpoint = "your-endpoint";
  1. OpenAIClient Initialization: The OpenAIClient is the principle shopper for interacting with Azure OpenAI providers. It requires the endpoint and the Azure API key.
  • var shopper = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
  1. Picture Technology Choices: This part configures the immediate and different parameters for picture era. On this instance, we use the “picture demo” mannequin to generate a 1024x1024 picture primarily based on the consumer immediate.
  • var imageGenerationOptions = new ImageGenerationOptions

    Immediate = immediate,
    DeploymentName = deploymentOrModelName,
    Measurement = new ImageSize("1024x1024"),
    ImageCount = 1
    ;
  1. Retrieving and Displaying the Picture URL: The generated picture is returned as a URL, which will be immediately opened or embedded into an internet web page.
  • ​​​​​​​var response = await shopper.GetImageGenerationsAsync(imageGenerationOptions);
    return response.Worth.Knowledge[0].Url.AbsoluteUri;
  1. Console Output: The Predominant perform runs the code and outputs the picture URL to the console, permitting us to view the generated content material.
  • ​​​​​​​Console.WriteLine($"await dalle.GetContent(dallePrompt)");
Namespace
Output

Conclusion

This easy software for the best way to use Azure OpenAI’s DALL·E mannequin to generate photographs in a .NET atmosphere. By integrating Azure OpenAI into our .NET tasks, we are able to unlock highly effective generative AI capabilities that may improve consumer experiences, automate content material creation, and construct modern functions.

Know extra about our firm at Skrots. Know extra about our providers at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots


Know more about our company at Skrots. Know more about our services at Skrots Services, Also checkout all other blogs at Blog at Skrots

Thanks, Harsh
Founder | CEO — Skrots

Learn more about our blog at Blog at Skrots. Checkout our list of services on Skrots. Give a look at our website design at Skrots . Checkout our LinkedIn Page at LinkedIn.com. Check out our original post at https://blog.skrots.com/building-image-generation-apps-with-azure-openai-in-net/?feed_id=7112&_unique_id=66f9c40c80d09

--

--

No responses yet