Need help to understanding the code

Saeed Pooladzadeh 251 Reputation points
2023-06-04T09:28:54.0266667+00:00

Hi,

This is part of a tutorial for OpenAI development:

https://www.c-sharpcorner.com/article/building-ai-chatbot-app-with-chatgpt-api-and-blazor-a-step-by-step-guide/

Can you please help me to understand this part of the code:

Content = new StringContent("{\"model\": \"text-davinci-001\", \"prompt\": \"" +
                                            query +
                                            "\",\"temperature\": 1,\"max_tokens\": 100}",
                                            Encoding.UTF8,
                                            "application/json")

regards,

Saeed

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,391 questions
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,551 Reputation points Microsoft Vendor
    2023-06-05T03:13:09.5633333+00:00

    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 :

    Building AI-Chatbot App with ChatGPT API and Blazor

    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,656 Reputation points
    2023-06-04T10:17:29.5566667+00:00

    The code is very basic so it is not clear what part you do not understand. There is a constructor and string concatenation. The StringContent constructor creates an instance of the StringContent class which represents string content found in an HTTP request.

    The first parameter is a JSON string, the data, to send to the service. The slash before the double quotes is syntax that allows a string to contain a double quote in the editor. The plus sign (+ query +) is C# string concatenation. The concatenation adds the contents of the query variable to the JSON string. The end result is...

    {
        model: "text-davinci-001",
        prompt: "The query",
        temperature: 1,
        max_tokens: 100
    }
    

    The second parameter is the HTTP encoding and the third parameter is the content type.

    Keep in mind, you can use the Visual Studio debugger to view the what's inside the variables.