How ChatGPT Generates Code and ChatGPT Coding Examples

One of the main events in the information technology market this year is the release of artificial intelligence technology Chat GPT. Also, Chat GPT can write code and this is starting to gain popularity among developers as the ability to use ChatGPT for coding. This technology immediately conquered the market and brought the concept of AI to a completely new level. The technology turned the concept of data search on the Internet upside down and made it possible not just to search for information on indexed pages and keywords, but to immediately find a detailed solution and, not least, a description of how to come to this solution. Immediately after the publication of ChatGPT, other AI systems appeared that have similar functionality somewhere.

Chat GPT Coding as a Programmer’s Assistant

The AI ​​that writes code is what allowed IT professionals to also not be left behind when using this technology. It made it possible to speed up the writing of routine qc operations when writing complex code and also acts as a knowledge base to help novice programmers write code. As mentioned earlier, a good feature of ChatGPT coding is the ability to describe an action or algorithm, that is, when asking a question, the user not only receives a ready-made solution or piece of code but also a description of what this code does and why, as well as a description of the algorithm itself. At the initial stages of Chat GPT coding training this AI can be compared to a more experienced mentor with an impossible patience for help and hints:).

 Chat GPT Coding Examples

Let’s take a look at some examples where ChatGPT helps you like a program that writes code.

For example, let’s take a list of users who have multiple dates. We need to get the maximum date for each user.

Let’s ask:

​C# There is a List with PersonID and Date fields. Get Lise with max Date for each PersonID

We get solution with chat bot code:

​To get a list with the maximum date for each PersonID, you can use a LINQ query like this:

using System.Linq;

/ /Defining class andDatefields
class Person
{
  public int PersonID { get; set; }
  public DateTime Date { get; set; }
}

// Source List of Person
List<Person> Persons = new List<Person>
{
  new Person { PersonID = 1, Date = new DateTime(2022, 1, 1) },
  new Person { PersonID = 1, Date = new DateTime(2022, 2, 1) },
  new Person { PersonID = 2, Date = new DateTime(2022, 3, 1) },
  new Person { PersonID = 2, Date = new DateTime(2022, 4, 1) },
  new Person { PersonID = 2, Date = new DateTime(2022, 5, 1) },
};

/ /LINQrequest for getting a list with the maximum date for eachPersonID
List<Person> result = Persons
.GroupBy(Person => Person.PersonID) // Grouping by PersonID
.Select(group => group.OrderByDescending(Person => Person.Date).First()).ToList();
As a result of executing this code, the variable resultwill contain a list consisting of two elements:

result[0]— list element Personswith maximum date for PersonID = 1;
result[1]— list element Personswith the maximum date for PersonID = 2.

As we can see, we got a solution in one second that solves our problem in just two lines of code.

Let’s take the second example. For searching databases, it is advisable to use well-tested engines, one of which is Elasticsearch. Writing a query for this engine can sometimes be difficult, and getting the right result takes a lot of trial and error. What was the surprise when asking a question in ChatGPT, you can not only get the correct query for Elasticsearch but also translate it into the desired programming language. Consider example:

Request:

​ElasticSearch There are Person objects with an expiryDate field. Print all Persons where expiryDate is null

Answer – AI writing code:

​To accomplish this task, you need to use a query on the selection (query) in ElasticSearch with a filter on ​the expiryDate field.

​An example of a select query that you can use to get all Person objects with an empty expiryDate field:

GET /Person/_search
{
  "query": 
  {
    "bool": 
    {
      "must_not": 
      {
        "exists": 
        {
           "field": "expiryDate"
        }
      }
    }
  }
}

In this query, we use a must_not bool filter to exclude from the results all Person objects that have an expiryDate field that exists (i.e., is not null).

If you only want to retrieve those Person objects whose expiryDate field is explicitly null, you can use the following select query:

GET /Person/_search
{
  "query": 
  {
    "bool": 
    {
      "must": 
       {
        "match": 
         {
         "expiryDate": null
          }
        }
      }
   }
}

In this query, we use a bool filter with must and match to select only those Person objects whose expiryDate field is explicitly null. Note that this will not select objects where the expiryDate field does not exist.

Rewrite Code in C#

To execute a query in C#, you need to use the official .NET Elasticsearch client Elasticsearch.Net and the Nest query library.

An example query to select all Person objects with an empty expiryDate field in C# using Nest:

using Nest;

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex("Person");
var client = new ElasticClient(settings);

var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Bool(b => b
.MustNot(mn => mn
.Exists(e => e.Field(f => f.ExpiryDate))
)
)
)
);

In this example, we create an instance ElasticClientwith Elasticsearch connection settings, set a default index name, and send a fetch request.

Note that in this example we are using the type Personas the document type we want to select. This assumes that you have an appropriate index mapping for the Person.

If you only want to get those Person objects whose expiryDate field is explicitly null, you can use the following code:

var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Match(ma => ma
.Field(f => f.ExpiryDate)
.Query(null)
)
)
)
)
);

In this example, we used .Match()to select only those objects whose field expiryDate is explicitly equal to null.

Conclusions

In this article, we have reviewed only a small part of the ChatGPT coding usage examples. It should also be noted that now additions or extensions have been released for almost all development environments, which allows you to use the functionality of artificial intelligence directly when writing code. With proper skill and learning ability, over time, more and more programming tasks will pass under AI that can write code. Looking at all the areas where AI is rapidly being implemented, and whether it will replace specialists from your field of activity, or you. I think it is difficult to answer this question, but I think that as long as there are customers with non-trivial desires, we will be needed in our field.