Using the Open AI API

March 09, 2023

OpenAI is a well trodden blog and YouTube path - in this post, I’m going to cover the idea of using it to gather information. Let’s say, for the sake of argument, that you have a big list of TV programs, for which, you want to get a description. The first step is to check that the Open AI model will cover your requirement (which you can do here). Let’s try a simple check:

Chat GPT

Now that we know the format that works, we can call out to the API:

var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

var requestBody = new
{
    model = engine,
    prompt = prompt,
    max_tokens = maxTokens,
    temperature = temperature
};
var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var json = JsonSerializer.Serialize(requestBody, jsonOptions);

var content = new StringContent(json, Encoding.UTF8, "application/json");

var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.openai.com/v1/completions", content);

var responseString = await response.Content.ReadAsStringAsync();

var options = new JsonSerializerOptions 
{ 
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase 
};
var completionResult = JsonSerializer.Deserialize<TextCompletionResult>(responseString, options);
string result = completionResult!.Choices[0].Text!;

The prompt is basically whatever you would type into Chat GPT; there’s a number of engines, for example:

private const string engine = "text-davinci-003";

And the API key you get from the Open AI site by registering.

Obviously, you’re going to have to be savvy about the specific question that you ask. Also, if you’re using the free version, it has a pretty severe rate limit. You can pay them for an increased throughput if you so wish.



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024