How to add a function definition to an OpenAI API for ChatGPT in C#?

I would like to use ChatGPT to extract a multiple choice question from a body of text and format the response in a standard way. I’ve written a C# .Net Core Windows Service that accepts a request from a client and passes the text to ChatGPT through the OpenAI API. That works fine but I’d like to get the response in a standardized format so I can process that easier. I want to use the functions in gpt-3.5-turbo-0613 or gpt-4-0613 but can’t find any documentation on how to use functions in C# with the OpenAI API.

using OpenAI.API;
using OpenAI.API.Completions;
OpenAIAPI client = new OpenAIAPI(api_key);
var parameters = new CompletionRequest
{
    Model = model,
    Prompt = CADEmain.decodeXML(o["content"].ToString()),
    Temperature = temperature,
    MaxTokens = maxTokens
};
var response = await client.Completions.CreateCompletionAsync(parameters);
string generatedText = response.Completions[0].Text;

The format I’d like to use would be something like:

    public class CustomResponse
    {
        public string Question { get; set; }
        public string[] Answers { get; set; }
        public int CorrectAnswer { get; set; } //index to correct in Answers
        public string feedback { get; set; }   
    }

I know I need to take the initial response from ChatGPT and send it back with the function definition but none of the documentation I’ve seen shows how to do that in C# using OpenAI.

I was hoping someone here would know or point me to a location that shows how do send the function definition back to ChatGPT through the OpenAI API.

Leave a Comment