illustration
Blogitekstejä

Observations about ChatGPT using GPT-4

Earlier, I experimented with a version of ChatGPT that used the AI model GPT-3 as a tool for software development. However, there is also a newer AI model version called GPT-4, which I did not look into in my previous post due to limited access. Here are my observations when using the newer chat model.

To test the new model, I decided to undertake a small home project using the OpenAI API to access the AI that powers ChatGPT, thereby killing two birds with one stone. Previously, I had developed a simple home automation script running on a Docker container on my QNAP network drive. I could connect to it with a browser to control and schedule my Philips Hue home lighting.

For this project, I decided to add a system that checks the weather forecast for the day and determines whose name day it is. It would then generate a short text for that day using the API connection to OpenAI's AI models. To make things more interesting, it would select a random literary style, such as a joke, a rhyming poem, or a short story, as well as the subject of the story, such as "a historical place in Finland with people" or "ancient Roman or Greek individuals."

My script would then parse those parameters into a query, something in the form of:

“Please write me a [style] about [target] called [name day name list]. Mention that the temperature is [temperature] celsius and the weather is [weather type].”

So I could get, for example, a query like this:

“Please write me a rhyming poem about historical places in Finland with people called Outi. Mention that the temperature is +1 celsius and the weather is sunny.”

The name-day debacle

But before I could accomplish this, I encountered a surprising problem. It turned out that the complete list of name day names is copyrighted, and I couldn't find a single internet source with a usable list of names. For a moment, I was worried that I would have to manually type these names day by day. However, I soon realized something: Wikipedia provides a list of name day names for each individual day, like this:

The URL format for the page seems quite straightforward:

So maybe I could ask ChatGPT-4 to help me a bit here?

write me a python script that fetches web pages such as this

https://fi.wikipedia.org/wiki/8._tammikuuta for each day of the year, modifying the format of the URL so that it will be (using Finnish month names): https://fi.wikipedia.org/wiki/{day}._{month}ta

Then once it loads the page, it will search for a part of text that says:

"suomalainen kalenteri" and will print out all characters that appear between that and the text "suomenruotsalainen"

In a few seconds ChatGPT-4 provided me quite a reasonable looking piece of script.

Notice how it automatically included the dates, as if it understood my intention. I saved that code to a file, executed the script, and I must admit, I burst out laughing when the program started gradually printing the following:

2.1: : Aapeli

3.1: : Elmeri, Elmo, Elmer

4.1: : Tiitus, Ruut

5.1: : Lea, Leea

6.1: : Harri

It successfully retrieved all the names, which I then cleaned up and saved to a file for future use. This design decision prioritized reliability over staying up-to-date. From there, it was simply a matter of coding the section that would read the file's contents into an array.

Oh, did I say "write"? My mistake. I meant, "time to politely request."

Excellent! It works! Now, please write me a python function that tests if a dictionary called nameDays is empty, and if so, the function opens up a file called nimipaivat.txt, that has contents defined like this:

2.1,Aapeli
3.1,Elmeri,Elmo,Elmer
4.1,Tiitus,Ruut
5.1,Lea,Leea
6.1,Harri
7.1,Aku,Aukusti,August

the first bit before the , is the date, in D.M format. After the date on each line is a list of names, separated by ,

After opening the file, the function fills the dictionary with the contents of the file, so that the date is used as a key that is given to the function as a date object.

The function then returns a string array of the name strings that come after the date portion on that line.

This is what I got:

There is a bug that causes it to crash immediately. So I asked it to fix it:

Can you fix that so that it doesn't crash if there's only one name?

And of course it did. This one would have worked perfectly, if not for the wrong separator character for the names, that was easy to fix:

Now that I had all the names loaded into an array, the next step was to parse them into a format that would fit the query. At this point, I must confess that I had become quite lazy. So, I decided to let ChatGPT handle that task as well:

Please write me a python function that receives an array of strings, joins them into a single string so that the character ',' is added in the back of every string except the last one, and put “ja” in front of the last one.

Certainly, I could have written that small code snippet myself, but it would have consumed valuable minutes of my time. There was also a possibility of making a silly mistake, leading to an additional ten minutes of troubleshooting. In contrast, letting ChatGPT handle it took mere seconds. Once it was done, it was done, without any further hassle.

Weather data

Next, I needed to obtain weather data for my prompt. I opted to use the free API provided by OpenWeatherMap.org. Using this service, I received a convenient weather forecast for the upcoming three days in JSON format, which appeared as follows:

{"cod":"200","message":0,"cnt":40,"list":[{"dt":1683277200,"main": {"temp":275.51,"feels_like":271.67,"temp_min":275.51,"temp_max":275.66,"pressure ":1030,"sea_level":1030,"grnd_level":1028,"humidity":56,"temp_kf":- 0.15},"weather":[{"id":801,"main":"Clouds","description":"vähän pilviä","icon":"02d"}],"clouds":{"all":15},"wind": {"speed":4.16,"deg":320,"gust":6},"visibility":10000,"pop":0,"sys": {"pod":"d"},"dt_txt":"2023-05-05 09:00:00"},{"dt":1683288000,"main": {"temp":275.65,"feels_like":271.93,"temp_min":275.65,"temp_max":275.76,"pressure ":1030,"sea_level":1030,"grnd_level":1028,"humidity":61,"temp_kf":- 0.11},"weather":[{"id":802,"main":"Clouds","description":"ajoittaisia pilviä","icon":"03d"}],"clouds":{"all":47},"wind": {"speed":4.01,"deg":308,"gust":5.07},"visibility":10000,"pop":0,"sys": {"pod":"d"},"dt_txt":"2023-05-05 12:00:00"},{"dt":1683298800,"main":

...

I wanted to extract the highest temperature for each day in my story prompt. I suppose I could have read the specifications and written a short script to retrieve the highest temperature from the data...

...or I could simply instruct ChatGPT to handle it for me:

I got this piece of json data from the weather service. Parse it to get the highest temperature for the current day. Not for the next 24 hours, but specifically for this day.

[maybe 2000 characters from beginning of the received json]

You can probably guess why I was so specific. Anyway, here's the answer:

After some tweaking to incorporate that code into my project, it was successfully implemented. During this process, I also discovered other useful features of ChatGPT when it comes to working with JSON. For instance, you can ask it to format JSON in a more readable way. Additionally, it's not necessary to provide the entire JSON; you can simply provide the specific portions that you want to extract or read.

Can you make this more readable?

[maybe 2000 characters from beginning of the received json]

Interesting how it seems to ignore a lot of data, though. When asked, it said it “selectively focused on relevant data”, which certainly seems to be the case. Maybe later I study this further, because

this seems to be a really promising way to go through json data.

Connection to the OpenAI API

It took me longer than I have recently become accustomed to in order to establish the connection to the OpenAPI service. This was due to the fact that ChatGPT's knowledge is limited to information available until 2021, and it is unaware of any API changes that have occurred since then. Consequently, using a traditional search engine felt rather outdated at this point.

Fortunately, I quickly discovered a user-friendly Python module called "openai" that facilitated a seamless integration. Once you create your OpenAI API account (which incurs a small cost for each query sent), and include your account-specific API key in your Python code, utilizing the OpenAI API is as straightforward as this:

completion = openai.ChatCompletion.create
     
( model="gpt-3.5-turbo",
     max_tokens=3800,
     n=1, temperature=0.2,
     messages=[
          {"role": "user", "content": "Say hello!"}
     ]
   )

print(completion.choices[0].message['content'])

Unfortunately, as of the time I'm writing this, the OpenAI API used in the above code only grants access to GPT-3.5, not GPT-4. This means that the stories it creates may not be as impressive as if they were generated by GPT-4. However, despite this small setback (which will likely be resolved in the near future), I had my story generator up and running in no time at all:

or in English:

Melina, Melissa, and Maini were outside playing when suddenly they noticed that the air was a bit cooler than before. They decided to call their friend out to play, but he replied that he didn't want to come out because it was too cold.

Melina, Melissa, and Maini didn't give up, but instead, they came up with a fun way to get their friend outside. They all dressed warmly and went out to build a snow castle. Their friend was hesitant at first, but eventually, he joined in, and they all had fun together.

Although the temperature was only 2.6 degrees Celsius and there were occasional clouds in the sky, the children didn't let that interfere with their enjoyment. They enjoyed the spring day and building the snow castle together.

At one point, I realized that it was necessary to specify that the generated stories should be happy or, at the very least, avoid being overly dark. This realization came after a series of happy and cute stories, as when I requested to write an interesting story about a historical event, it suddenly produced the following text (translated from Finnish to English):

"It was a winter day in 1918 when Roosa, Rosa, and Ruusu were on their way to the city of Tampere. They were part of the Reds' group that had rebelled against the government. The temperature was only 2.4 degrees Celsius and a small snowfall covered the land in white.

However, their journey was cut short when they were stopped on their way to the city. They were confronted by a group of Whites who had come to halt the rebels. Roosa, Rosa, and Ruusu tried to escape, but they were caught.

They were imprisoned, and their fate was sealed. They were shot the next day outside the city of Tampere. Their names went down in history as victims of the Reds' rebellion."

That's not exactly something to cheer you up in the morning.

My thoughts about all this

After experimenting with ChatGPT-4, I must admit that there were no unexpected surprises when compared to GPT-3. As anticipated, the new version is even more advanced than its predecessor. The generated code exhibits a noticeable improvement in quality, and it demonstrates a greater level of consideration in its writing. Furthermore, ChatGPT-4 provides additional information in its suggested solutions compared to ChatGPT-3, as exemplified by its response following the name- ripping script I mentioned earlier:

I have to repeat how impressed I really am with its ability to create the script for ripping the names from Wikipedia. It worked perfectly immediately, even better than I had told to make it. The way how it seems to understand the context is one of the things that makes it a clear step-up from GPT- 3.

Another example of how good it is at understanding the context is when I asked if ChatGPT-4

would have access to current time because I was thinking about making the stories it created to happen in the current season of the year. Without me asking, it instead provided me a Python function that I could use to figure that out myself. Based on what we had been talking previously, it immediately knew what I was going for and solved my problem before I could even ask it to:

It is quite literally funny how good it is at doing what it does.

It is hard to express how much this new tool changes the software development process. It is like construction sites switching to use wall elements instead of individual bricks. At times I feel more like an artist than an engineer, as if I'm having ideas and then using a brush to quickly create them. The amount of problem-solving required per line of code is drastically reduced, leading to a reduction in frustration. I can just let my creativity flow. After 20 years of writing code professionally in the Hard Way, being able to use AI like this to do most of the heavy lifting feels like suddenly gaining a superpower.

I can honestly say that programming has never been so much fun!

Haluatko kuulla lisää tästä aiheesta? Jätä yhteystietosi niin olemme yhteydessä.

Lähettämällä lomakkeen hyväksyn tietojeni tallentamisen ja käsittelyn tietosuojaselosteen mukaisesti.