#Windows form C# how to turn "completionresult" into output.

15 messages · Page 1 of 1 (latest)

wintry remnant
#

` private void send_Click(object sender, EventArgs e)
{
string question = textBox1.Text;

        var apiKey = "apikey";

        var gpt3 = new OpenAIService(new OpenAiOptions()
        {
            ApiKey = apiKey
        });

        var completionResult = gpt3.Completions.CreateCompletion(new CompletionCreateRequest()
        {
            //Prompt = "What is the meaning of life?",
            Prompt = question,

            Model = Models.TextDavinciV2,
            Temperature = 0.5F,
            MaxTokens = 100,
            N = 3
        });

        label1.Text = Convert.ToString(completionResult.Result);

    }`
#

I think everything runs well except for label1.Text = Convert.ToString(completionResult.Result);

#

I have tried something similar like this yesterday in console but want to do it now in windows form.

jovial lava
# wintry remnant I think everything runs well except for `label1.Text = Convert.ToString(complet...

The completionResult is an async Task, containing a CompletionCreateResponse. Therefore, your completionResult.Result is not a string, but an object of type CompletionCreateResponse. This response object contains additional data like the model you used (completionResult.Result.Model). I think you're looking for completionResult.Result.Choices.

Also, accessing the result of an async Task like this is discouraged, as the ui will just lock as it's waiting for the response.
If you don't want to lock your whole ui, please check how async and Tasks work in C#.

wintry remnant
jovial lava
#

The choices are still an object of type list containing objects of the type ChoiceResponse. You will need to provide an object of type string (or in other words, a text) to the label. So in your code, you will need to go through the choices, select one, and show the Text from that.
If you're using an IDE like VisualStudio, you can ctrl+click to jump to the type definiton and see it's properties.

jovial lava
wintry remnant
#

thanks I did not think about that.

wintry remnant
wintry remnant
#

Nothing it just keeps running and the ui locks like you said.

jovial lava
#

Then you need to convert your send_click function to an async function. Since it's called by an event handler anyways, you can just make it an async void

  private async void send_Click(object sender, EventArgs e){}

this allows you to await gpt3.Completions.CreateCompletion() or any other method that returns a Task.

So instead of this:

Task<CompletionCreateResponse> completionTask = gpt3.Completions.CreateCompletion(new CompletionCreateRequest());

You have to await the Task to get the inner CompletionCreateResponse. Previously, you did this synchronously with completionResult.Result, but you can do it async with

CompletionCreateResponse completionResult = await gpt3.Completions.CreateCompletion(new CompletionCreateRequest()));
wintry remnant
#

Thank you so much

#

This is the full thing if someone else wants to use it