Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

passing <string> to string is causing error

I have this function

    static async Task<string> ChatGPT(string token, string model, string message)
    {
        ChatGPTClient chatGPTClient = new ChatGPTClient();
        string result = await chatGPTClient.GetChatGPTResponse(token, model, message);
        return result;
    }

result is a string inside the function and passed when it called

when i debug i can read the value of result = "Hello! How can I assist you today?"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

calling it from this event

    private void btnCall_Click(object sender, EventArgs e)
    {
        string token = txtToken.Text;
        string model = cmbModel.Text;
        string message = txtPrompt.Text;

        txtDisplay.text = ChatGPT(token, model, message).ToString();
    }

but the value of txtDisplay is

System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.String,APIApp.frmChatGPT+<ChatGPT>d__1]

what am i doing wrong and how to fix it?

>Solution :

The result type of the ChatGPT method is Task<string>. If you want to assign the string result from the task then you have to await the task.

private async void btnCall_Click(object sender, EventArgs e)
{
    string token = txtToken.Text;
    string model = cmbModel.Text;
    string message = txtPrompt.Text;

    txtDisplay.text = await ChatGPT(token, model, message);
}

You can read more about async/await at the blog from Stephen Cleary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading