What is Chat GPT?
Tags: programming, ai
Published: 2023-03-21 12:27:54

## Hi, I am Arun Kumar Palani, Senior software engineer in Luxoft. Let’s discuss chat GPT in detail.

<u>Note: I have implemented it in .NET, since I am from .NET background.</u>

**<u>What is Chat GPT?</u>**

Chat GPT is an AI based (LLM) large language model designed by Open AI which is trained by large amount of data. In Chat GPT the data is trained up to 2021. One of the Open AI co-founders was Elon Musk.

Chat GPT working is based on GPT (Generative Pre-trained Transformer) model, an AI-based conversational agent, to produce intelligent responses to user inputs. A neural network architecture used for understanding and processing natural language is used in GPT model implementation.

Simply, the Chat GPT system tries to understand what a user is saying and then reacts to it in accordance with its training data. It can respond to a variety of topics and is intended to be helpful, knowledgeable, and informative.

The main benefit of Chat GPT is that it can communicate with users in natural language and gives tailored answers to each question. By removing the need for human interaction, this gives users instant access to pertinent information and contributes to time savings.

Mainly in Customer service, e-commerce, and education are just a few sectors where Chat GPT is extensively used. In the big data and machine learning era, it has become an essential tool. Large amounts of data can be analyzed by the software, which can then deliver insights that are tailored to the requirements of businesses.

**<u>How Chat GPT is used in real life?</u>**

1. Creating a chat bot.
2. Generating text-based applications.
3. Creating a virtual assistant.
4. Implement a customer service system in an automated way.
5. Providing answers to most frequently asked questions in many e-commerce and general-purpose applications.
6. Text summarizing and generating reports.

**<u>How is Chat GPT implemented?</u>**

The fundamental operating principle of Chat GPT is to take input from users, process the input data, and produce an insightful response. 
Here is a step-by-step description of how the system works:
**<u>User input:</u>** The first step entails gathering information from the users. The users contribute by chatting in natural language via text or voice.

**<u>Processing the Input Data:</u>** After receiving user input, the data undergoes pre-processing to clean it up and eliminate unnecessary elements like stop words and punctuation. The text is then divided into smaller units by the software, typically words and phrases, which are then examined pairwise.

**<u>Application of Machine Learning Model:</u>** Following preprocessing of the input data, further analysis is conducted using the machine learning algorithm based on the GPT model. This algorithm creates new sentences with a probability determined by the frequency of appearance in the training sources using previous training data that was learned from millions of lines of publicly available text sources.

**<u>Response Generation:</u>** Using the linguistic patterns discovered in the earlier steps as a basis, Chat GPT applies the algorithm to produce a pertinent output text.

**<u>What is making Chat GPT unique?</u>**

Because of its capacity for instant learning and adaptation, Chat GPT is exceptional. Chat GPT uses machine learning to generate conversations based on its training data, in contrast to rule-based systems that adhere to a predetermined set of rules. The system can pick up on various conversational topics and behaviors and respond appropriately.
Another distinguishing feature of Chat GPT is that its models are trained on vast amounts of data, giving it an advantage over competing products of a similar nature. This guarantees that the system is effective at responding to user inquiries in a way that is both helpful and accurate.

**<u>What’s more interesting as a developer to do with Chat GPT?</u>**

Yes, as a developer we would like to communicate with Chat GPT end points and to create a working model so that we can demonstrate how these models help in real life scenarios and what problems it solves. Along with this we would also like to know the different search models are available and how we can use those models.

**<u>NuGet extension:</u>** OpenAI.
**Different Chat GPT search models:**

<u>**Chat GPT version 3 models:**</u>

1.AdaText
2.BabbageText
3.CurieText
4.Davinci
5.CushmanCode - codex model
6.DavinciCode – codex model
7.AdaTextEmbedding
Let’s see how these models are going to help based on the requirements.

**<u>1.AdaText:</u>** This model is capable of very simple tasks, usually the fastest model in the GPT-3 series, and this is one of the lowest cost models in the GPT series. The string code for this model is represented by “text-ada-001”.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct- 2019

**<u>2.BabbageText:</u>** This model is capable of straightforward tasks, very fast, and lower cost and the string representation for this model is “text-babbage-001”.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct-2019

**<u>3.CurieText:</u>** This model is Very capable, but faster and lower cost than Davinci and the string representation is “text-curie-001”.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct-2019

**<u>4.DavinciText:</u>** This is the most capable GPT-3 model. Can do any task the other models can do, often with higher quality, longer output and better instruction-following.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct-2019

**<u>5.CushmanCode:</u>** Almost as capable as Davinci Codex, but slightly faster. This speed advantage may make it preferable for real-time applications and its representation is “code-cushman-001”.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct-2019

**<u>6.DavinciCode:</u>** Most capable Codex model. Particularly good at translating natural language to code. In addition to completing code, also supports inserting completions within code and its representation is “code-davinci-002”.
     1.Max token supported:  8001 tokens.
     2.Trained data set: up to Jun-2021

**<u>7.AdaTextEmbedding:</u>** OpenAI offers one second-generation embedding model for use with the embeddings API endpoint and its representation is “text-embedding-ada-002”.
     1.Max token supported: 2049 tokens.
     2.Trained data set: up to Oct-2019

**<u>Chat GPT version 3.5 models:</u>**

1.gpt-3.5-turbo
2.gpt-3.5-turbo-0301
3.text-davinci-003
4.text-davinci-002
5.code-davinci-002

**<u>1.gpt-3.5-turbo:</u>** The most powerful GPT-3.5 model, which is chat-optimized, costs only one-tenth as much as the text-davinci-003. updated with the most recent model iteration.
      1.Max token supported:  4096 tokens.
      2.Trained data set: up to Sep-2021

**<u>2.gpt-3.5-turbo-0301:</u>** GPT-3.5-Turbo snapshot taken from gpt-3.5-turbo. Contrary to the GT-3.5 Turbo, this model will only be supported for three months, ending on June 1, 2023, and will not receive any updates.
      1.Max token supported:  4096 tokens.
      2.Trained data set: up to Sep-2021

**<u>3.text-davinci-003:</u>** Can perform any language task better, faster, and with more consistency in following instructions than the curie, babbage, or ada models. Additionally supports text completions being inserted.
      1.Max token supported:  4097 tokens.
      2.Trained data set: up to Sep-2021

**<u>4.text-davinci-002: </u>**Like text-davinci-003 in terms of abilities but trained using supervised fine-tuning rather than reinforcement learning.
      1.Max token supported:  4096 tokens.
      2.Trained data set: up to Jun-2021

**<u>5.code-davinci-002:</u>** Designed code completion task.
      1.Max token supported:  8001 tokens.
      2.Trained data set: up to Jun-2021

**<u>Chat GPT version 4 models:</u>**

1.gpt-4
2.gpt-4-0314
3.gpt-4-32k
4.gpt-4-32k-0314

**<u>1.gpt-4:</u>** More capable than any GPT-3.5 model, able to do more complex tasks, and optimized for chat. Will be updated with our latest model iteration.
      1.Max token supported: 8192 tokens.
      2.Trained data set: up to Sep-2021.

**<u>2.gpt-4-0314:</u>** Snapshot of gpt-4 from March 14th, 2023. Unlike gpt-4, this model will not receive updates, and will only be supported for a three-month period ending on June 14th, 2023.
      1.Max token supported: 8192 tokens.
      2.Trained data set: up to Sep-2021.

**<u>3.gpt-4-32k:</u>** Same capabilities as the base gpt-4 mode but with 4x the context length. Will be updated with our latest model iteration.
      1.Max token supported: 32768 tokens.
      2.Trained data set: up to Sep-2021.

**<u>4.gpt-4-32k-0314:</u>** Snapshot of gpt-4-32 from March 14th, 2023. Unlike gpt-4-32k, this model will not receive updates, and will only be supported for a three-month period ending on June 14th, 2023.
      1.Max token supported: 32768 tokens.
      2.Trained data set: up to Sep-2021.

**<u>Other models:</u>**

1.DALL.E
2.Whisper
3.Embedding
4.Moderation

**<u>DALL.E:</u>** DALLE is an artificial intelligence program that can produce art and realistic images from a description in natural language. We currently provide support for the creation of new images with a specific size, the editing of existing images, and the creation of variations of user-provided images. The DALLE model that is currently available through our API is the second version of DALLE, which has more accurate, realistic, and images with a 4x higher resolution than the first model.

**<u>Whisper:</u>** A general-purpose speech detection model is whisper. It is a multi-task model that can perform multilingual voice recognition, speech translation, and language identification and was trained on a sizable dataset of varied audio. Our API presently offers the Whisper v2-large model under the name whisper-1.
Right now, there is no distinction between the Whisper accessible through our API and the open-source version. However, we provide an optimized inference procedure through our API, which makes using Whisper through our API much faster than using other methods.

**<u>Embedding:</u>** It is a numerical representation of text that can be used to quantify the relatedness between two pieces of text. At a fraction of the price, our second-generation embedding model, text-embedding-ada-002, is intended to take the position of the 16 first-generation embedding models that came before it. 
Tasks involving search, clustering, recommendations, anomaly detection, and categorization can benefit from embeddings.

**<u>Moderation:</u>** The Moderation models' purpose is to determine whether material abides by Open Ai’s usage guidelines. 
The models offer the ability to categorize material by searching for items that fall under the following headings: hate, hate/threat, self-harm, sexual, sexual/minors, violence, and violence/graphic.

**<u>How to integrate Chat GPT in .NET application?</u>**

1. Create an account in OpenAI.com.
2. Get the API-key from the Open AI website once successfully logged in.
3. There are 2 different ways to implement this in .NET
	1. Completion request class
	2. Direct Http client usage to hit Open AI end points.

**<u>Completion request implementation:</u>**
1.Install Open AI from the NuGet package manager.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjd7j3uyqod2vdmfi4ev.PNG)

There are few inputs we need to pass to the completion request. They are.
**<u>a. Prompt:</u>** It is basically the input string where we are sending from the input request.

**<u>b. Model:</u>** This is the place we need to define which Chat GPT model we are going to use when we are making a API call to Open AI endpoint.

**<u>c. Temperature:</u>** It gives sampling rate for the search that we are trying with. If we are sampling rate is higher ex) 0.9f we can get creative answer from the endpoint. If we are using 0f we will get the standard answers.

**<u>d. Max Tokens:</u>** It gives the information how much is the maximum string that the API will give as a response. This can be set manually if we are using either completion request class or from Http Client as well. We need to ensure the maximum token available to generate response for the particular model before specifying the value.

**<u>e. TopP:</u>** An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. It is generally recommend to use this or OpenAI_API.Completions.CompletionRequest.Temperature but not both.

**<u>f. Frequency Penalty:</u>** The scale of the penalty for how often a token is used. Should generally be between 0 and 1, although negative numbers are allowed to encourage token reuse. Defaults to 0.

**<u>g. Presence Penalty:</u>** The scale of the penalty applied if a token is already present at all. Should generally be between 0 and 1, although negative numbers are allowed to encourage token reuse. Defaults to 0.

2.Apply the API key and start running the application.
3.Full code for creating the working model using completion request class.
4.Sample C# code: 
   var gpt = new OpenAIAPI(apiKey);
                CompletionRequest completionRequest = new 
                CompletionRequest()
                {
                    Prompt = input,
                    Model = Model.DavinciText,
                    Temperature = 0.5f,
                    MaxTokens = 4000,
                    TopP = 1.0,
                    FrequencyPenalty = 0.0,
                    PresencePenalty = 0.0,
                };
                completionResult = await 
         gpt.Completions.CreateCompletionAsync(completionRequest);

**<u>Http client Implementation:</u>**
1.We just need some basic idea of how Http client works before implementing this. We need to pass the proper parameters and request and response formats that should match with the Open API endpoint which is officially released.

2.There are different endpoints which was provided by Open AI for chat completion, Image generation, Image edit, Audio translation, Audio transcription and so on. Let's see one how to implement the chat completion in Http client.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb06lj6argagnti9mz99.PNG)

3.It requires a base address to establish a connection to the open API endpoint. For chat completion we need to use this URL: https://api.openai.com/v1/completions

4.Authorization can be done by the unique API-key given by Open AI this will perform authentication for us.

5. I have used .NET 6 so the Http client will have PostAsJsonAsync() method where we need to pass the input request.

6. With the help of ReadFromJsonAsync() method in the response we can get the result from Open AI.
7. Here is the C# code.
         public async Task<ChatCompletionResponce> 
         GenerateCompletion(ChatCompletionRequestcs request, 
         CancellationToken cancellationToken = default)
        {
        try
            {
             HttpClient httpClient = new HttpClient();
             httpClient.BaseAddress = new Uri(chatEndpoint); 
             httpClient.DefaultRequestHeaders.Add("Authorization", 
             $"Bearer {apiKey}");
             httpClient.DefaultRequestHeaders.Add("Accept", 
             "application/json");
             var res = await 
             httpClient.PostAsJsonAsync(chatEndpoint, request,  
             cancellationToken);
             var result = await 
             res.Content.ReadFromJsonAsync<ChatCompletionResponce> 
             (cancellationToken: cancellationToken);
             return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

**<u>Advantages of Chat GPT:</u>**

 1. Automated decision making with AI based system.
 2. Precise use case with safety and security of data we search.
 3. Increased accuracy.
 4. Quick response time and increased engagement.
 5. Reduced expense and increased customer service.

**<u>Disadvantages of Chat GPT?</u>**

 1. Inaccuracies and Ambiguities in some times.
 2. Limited Knowledge of Recent Events - because it is trained 
 with data up to 2021.
 3. Ethical Issues and Concerns.
 4. Other Possible Legal Implications.

**<u>Conclusion:</u>** Chat GPT is a highly powerful language model with the potential to revolutionize a variety of fields and sectors. It is a useful tool for applications like chatbots, virtual assistants, and content production due to its capacity to generate extremely accurate and contextually relevant responses to natural language inputs. Although if bias and interpretability provide difficulties that must be overcome, Chat GPT represents a promising area for future research and development in the field of artificial intelligence.

Thanks for reading the article, if you have any queries or suggestions, please post it in the comments section.