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?"
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.