OpenAI unveils ChatGPT API and here's how easy it is to connect in C#/.NET
Today, March 01, 2023
The world of conversational AI takes a giant Super Mario leap forward with the launch of OpenAI's ChatGPT API.
This rapid growth in AI capabilities is akin to the growth of the internet, which saw the introduction of Netscape Navigator in 1994 and the subsequent explosion of the internet into a $6 trillion market by 1999. With AI predicted to see similar growth and adoption in the coming years, businesses and developers are looking to leverage this technology to gain a competitive edge in their industries.
Over a lunch hour I was able to integrate with OpenAI and ChatGPT using Microsoft's C#/.NET. Here we go!
C# code
Code output
For example, let's say you want to ask ChatGPT to write a short poem about Super Mario. With the ChatGPT API, you can simply send in input of "Write a short Super Mario poem" and get a response like:
Get started
领英推荐
2. Your organization ID is under Settings
3. Here is the source code. You will need both the api key and organization key to get started.
using System.Web
using System.Text.Json;
class Program
{
? ? static async Task Main(string[] args)
? ? {
? ? ? ? Console.Write("Input: ");
? ? ? ? var prompt = Console.ReadLine();
? ? ? ? Console.WriteLine("-------");
? ? ? ??
? ? ? ? string organizationId = "insert organizationKey";
? ? ? ? string apiKey = "insert apiKey";
? ? ? ? // Set the API endpoint and model name
? ? ? ? string endpoint = "https://api.openai.com/v1/completions";
? ? ? ? // Create an HttpClient object and set the authorization header with your API key
? ? ? ? using (HttpClient client = new HttpClient())
? ? ? ? {
? ? ? ? ? ? client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
? ? ? ? ? ? // Set the request body as a JSON object with the model name, input prompt, and other parameters
? ? ? ? ? ? string requestBody = JsonSerializer.Serialize(new
? ? ? ? ? ? {
? ? ? ? ? ? ? ? model = "text-davinci-003",
? ? ? ? ? ? ? ? prompt = prompt,
? ? ? ? ? ? ? ? temperature = 0.2, //0.1 not creative; 1.0 super creative
? ? ? ? ? ? ? ? max_tokens = 500 //tokens are individual words
? ? ? ? ? ? });
? ? ? ? ? ? // Create a new HttpRequestMessage with the request method and URL
? ? ? ? ? ? HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, endpoint)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/json")
? ? ? ? ? ? };
? ? ? ? ? ? // Send the HTTP request and retrieve the response
? ? ? ? ? ? HttpResponseMessage response = await client.SendAsync(request);
? ? ? ? ? ? // Read the response content as a TextCompletion object and extract the list of choices
? ? ? ? ? ? string responseBody = await response.Content.ReadAsStringAsync();
? ? ? ? ? ? TextCompletion responseJson = JsonSerializer.Deserialize<TextCompletion>(responseBody);
? ? ? ? ? ? List<Choice> choices = responseJson.choices;
? ? ? ? ? ? foreach(var choice in choices) {
? ? ? ? ? ? ? ? Console.WriteLine(choice.text);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
? ? ? ??
class TextCompletion
{
? ? public string id { get; set; }
? ? public string @object { get; set; }
? ? public int created { get; set; }
? ? public string model { get; set; }
? ? public List<Choice> choices { get; set; }
? ? public Usage usage { get; set; }
}
class Choice
{
? ? public string text { get; set; }
? ? public int index { get; set; }
? ? public object logprobs { get; set; }
? ? public string finish_reason { get; set; }
}
class Usage
{
? ? public int prompt_tokens { get; set; }
? ? public int completion_tokens { get; set; }
? ? public int total_tokens { get; set; }
};
With plumbing made this easy, and the recent advancements in language models now doubling in capabilities every 6 months (outpacing Moore's Law by a significant margin) things are going to change fast!