Explore a novel feature in a handy NuGet package that empowers .NET developers to seamlessly create custom images from textual descriptions using OpenAI's API. Whether you need unique images for your project or wish to reduce reliance on graphic designers, this article guides you through the process. With a few lines of code, transform textual prompts into visual creativity, enhancing your application's interactive and aesthetic appeal. The article also hints at a previous discussion on generating image variations, broadening the spectrum of image manipulation capabilities within .NET applications.
Introduction
This article unveils a new feature in the ConnectingApps.Refit.OpenAI NuGet package that enables developers to create images using OpenAI's API in .NET applications based on textual descriptions. This feature is a boon for those needing to generate custom images without the reliance on graphic designers or stock images.
Background
The ability to generate images from textual descriptions is a remarkable advancement facilitated by OpenAI. The ConnectingApps.Refit.OpenAI NuGet package brings this capability to the fingertips of .NET developers. For a more comprehensive understanding, you may refer to the introductory article on CodeProject. Moreover, if you're interested in generating image variations, the follow-up article on image variations is a worthy read.
Using the Code
Let’s delve into how you can leverage this feature to generate images from textual descriptions in a .NET application.
Firstly, ensure that the ConnectingApps.Refit.OpenAI NuGet package is installed in your project. Now, set up your OpenAI API key as an environment variable:
using System;
using System.Net.Http;
using ConnectingApps.Refit.OpenAI;
using ConnectingApps.Refit.OpenAI.ImageCreations;
using ConnectingApps.Refit.OpenAI.ImageCreations.Request;
using Refit;
var apiKey = Environment.GetEnvironmentVariable("OPENAI_KEY");
Create an instance of the IImageCreation
interface, specifying the base address of the OpenAI API:
var creationApi = RestService.For<IImageCreation>(new HttpClient
{
BaseAddress = new Uri("https://api.openai.com")
}, OpenAiRefitSettings.RefitSettings);
Now, initiate a call to the CreateImageAsync
method, passing in an ImageCreationRequest
object with the desired parameters:
var response = await creationApi.CreateImageAsync(new ImageCreationRequest
{
N = 2,
Prompt = "A cute baby sea otter.",
Size = "1024x1024",
}, $"Bearer {apiKey}");
This method returns a response containing the status code and URLs of the generated images. You can access and use the URLs as needed:
Console.WriteLine($"Returned response status code {response.StatusCode}");
Console.WriteLine($"Number of urls {response.Content!.Data.Length}" );
Console.WriteLine($"First url {response.Content!.Data.First().Url}");
Console.WriteLine($"Last url {response.Content!.Data.Last().Url}");
The console output will be as follows:
Returned response status code OK
Number of urls 2
First url [FIRST URL]
Last url [SECOND URL]
For more insights and examples, feel free to explore the package’s GitHub repository.
Points of Interest
The seamless interaction between the ConnectingApps.Refit.OpenAI NuGet package and OpenAI’s API simplifies the image generation process, making it an efficient solution for developers needing custom images for their projects.
History
- 15th October, 2023: Initial version of the article showcasing the new image creation feature of the ConnectingApps.Refit.OpenAI NuGet package