It was sending httprequest with httpclient and reading the response text:
var request = new HttpRequestMessage(HttpMethod.Post, _httpClient.BaseAddress)
{
Content = new StringContent("{\"model\": \"text-davinci-001\", \"prompt\": \"" +
query +
"\",\"temperature\": 1,\"max_tokens\": 100}",
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(request);
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
var responseString = JsonConvert.DeserializeObject<dynamic>(responseContent);
return responseString!.choices[0].text;
The codes you mentioned was creating an instance of StringContent to package the serialized JSON
{\"model\": \"text-davinci-001\", \"prompt\": \"" +
query +
"\",\"temperature\": 1,\"max_tokens\": 100}
for sending in the HTTP request's body.You could check the document related
And based on other codes:
<div class="searchArea">
<button @onclick="GetAPIResponse"> Submit </button>
<div class="inputDiv">
<InputText class="form-control" @bind-Value="@AIRequest.Request" placeholder="Ask me anything.." />
</div>
</div>
<div class="searchResult">
<InputTextArea class="form-control" style="height:450px" @bind-Value="@AIRequest.Response" placeholder="AI will respond here..." readonly />
</div>
@code{
Message AIRequest = new();
public async Task GetAPIResponse()
{
try
{
AIRequest.Response = await chatService.GetResponse(AIRequest.Request);
}
catch (Exception)
{
}
}
}
It assign your input in textbox to prompt property, send an request as mentioned above to chatgpt endpoint then read the response and display it :
Here's the document related with Blazor razor componet data-binding,hopes help
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
RuikaiFeng