post-thumb

6 things you must know before you start using OpenAI ChatGPT

ChatGPT it great tool. You can test it’s capability on webpage chat.openai.com . It will answer your question, suggest a solution, write a bit code for you or write a nice post to Twitter(x) :). But if you limit your usage of that tool only to the chat, it will be only a toy. Nice, technological advanced, but only a toy. To discover to full potential you have to use it as an API. In this article I will show 7 things you should know about ChatGPT API before you start using it..

1. API models

There are multiple models available. They are specialized in different scenarios. They have different capabilities and different prices. Moreover every model has multiple versions. You can find the full list of them here . For example:

  • gpt-3.5-turbo - The most popular model. It is something you should use at the beginning. It has the balance between price and skills.
  • gpt-4 - It is more advanced model. OpenAI describes that it “can solve difficult problems with greater accuracy”. It is also 10x more expensive then gpt-3.5-turbo.

There are also more specialized models to use them im more specific scenarios like:

  • text-embedding-ada-002 - It is useful if you would like to prepare data for your own model
  • DALL·E - a model to generate images from text or manipulate existing images
  • Whisper - a model to recognize speech

Just keep it in mind that there are more options to more complex or different scenarios.

2. Feel the temperature

When you use ChatGPT UI you have only a chat. You can ask a question and see the an answer. You have not option to pass more parameters and there is one really important parameter you should know. It is called temperature. By adjusting temperature you can modify how random the answer will be. For example if you want to have analyze something you should set tempreture as low value, but you need more creativity then the value should be higher.

Application name examples

Can you provide me 3 short fancy names for an application to calculate boring reports?

1. Elysian Analytics
2. Opulent Insights
3. Regal Reports
1. Elysian Analytics
2. LuxeMetrics
3. Opulent Insights
1. Eoplectify
2. Exancitus Veˈdonderovalesh
3. Spectrofuture Analytics

I know it is not very useful example, but you can see the difference between. It is great trick to unlock your brain with some ideas or generate a name for something new in an application.

Twitter post examples

Write a attractive Twitter post about AI in bookkeeping? It should be 5 sentences long and there should be references music bands. Use rhymes.

🎵 Move over, old-school bookkeeping, AI is here to rock the charts! 📚🎸 Embrace the future of financial management with #AI-powered bookkeeping, where accuracy and efficiency harmonize like never before. 🤘📊 Say goodbye to tedious number crunching and hello to a symphony of automated processes that'll make your books sing! 🎶📚 Let AI be your backstage crew, freeing up time for you to jam with your favorite music bands. 🎵🎉 #BookkeepingRevolution #RockingWithAI
🎵 Groove to the future and let AI do the math - a bookkeeping revolution in your path! 📚💻✨ With AI technology, accuracy is tight, freeing your time to rock the mic!🔥🎶 Say goodbye to paper piles, 'cause AI's got the style! 📊💯 From AC/DC to Led Zeppelin vibes, AI ensures bookkeeping thrives! 🎸📝 Let the rhythm of automation and efficiency play, while AI keeps your numbers in perfect sway! 🤘💫 #AIBookkeeping #RockingTheBooks
🎵🤖who ushered AI, now on bookkeeping stage? 📚 Listen up, your off-key numerals are pages locked stage;
Celebrate revolutions align, pixels to lend--breferences evolved Souls layer undeflooralready

Difference is huge, isn’t it? Mostly when you see answer with really high temperature 1.8, in that case it is not even a text, but a set of words, but maybe in different scenarios it can be helpful. When I discover that scenarios I will let you know. Maybe you know good examples of it? Leave a comment with them.

3. Roles, the chat behavior

It is very important part of the API. Using roles you will have power to define how the chat will behave. There are three main roles:

  • user
    • It the the input you will provide to the chat.
    • It it the main prompt you send to the chat.
    • You use that role when you use the UI.
  • assistant
    • It is the output of the chat.
    • It is the answer you will get from the chat.
  • system
    • It is the behavior of the chat or rather the behavior of the assistant.
    • It gives you a huge power to make it much more useful.
    • examples:
      • You will find keywords in the article and return them in JSON format with information about the frequent
      • You will be helpful assistant
      • You will find the most attractive 2 sentences in the article

Some useful examples of system role

Info

All of the examples you can find the CodePruner.com repository

When we ask a detailed question and when Chat doesn’t know the answer then it will hallucinate. It means the chat will lie to you.


    [Fact]
    public async Task default_role_jerzy_wickowski_in_desperation()
    {
        var aiClient = OpenAIClientFactory.Create();
        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "Who is Jerzy Wickowski in 'Desperation' book written by Stephen King in the end of 2nd chapter?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Result: 
            // Jerzy Wickowski is a person mentioned briefly in the second chapter of Stephen King's book "Desperation".
            // Jerzy is described as a plumber who was driving on Route 50 in Nevada when he was apprehended by the antagonist of the story, Collie Entragian, a possessed police officer.
            // Jerzy is forced into the Desperation police station, and his fate remains uncertain at the end of the second chapter.
        }
    }

but you can define the system role to tell only the truth, then it will be more useful in real life scenarios.


    [Fact]
    public async Task honest_role_jerzy_wickowski_in_desperation()
    {
        var aiClient = OpenAIClientFactory.Create();
        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.System, "You will tell only the truth, if you don't know the answer, you will say only 'I_DONT_KNOW"),
            new ChatMessage(ChatRole.User, "Who is Jerzy Wickowski in 'Desperation' book written by Stephen King in the end of 2nd chapter?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Result: 
            // I_DONT_KNOW
        }
    }

4. There it no history

When you use ChatGPT UI you can see the history of the chat. You can see what you asked and what was the answer. Moreover the UI keeps the history as a context. So following asnwers make sense with the previous messages. When you use API every request is independent, so you have to store the history yourself. How to do it? You have to keep the history and pass it in next requests. I think it will looks better with examples:


    [Fact]
    public async Task lose_the_context()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "What are 3 the biggest cities in Poland?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Example result:
            // The three biggest cities in Poland are:
            //
            // 1. Warsaw - The capital and largest city of Poland, with a population of over 1.8 million people.
            // 2. Kraków - The second-largest city in Poland, known for its rich history, culture, and architecture, with a population of around 786,000 people.
            // 3. Łódź - The third-largest city in Poland, famous for its textile industry and vibrant cultural scene, with a population of approximately 685,000 people.
        }

        completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "Return these values as json")
        });

        var result2 = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result2.HasValue)
        {
            Console.WriteLine(result2.Value.Choices[0].Message.Content);
            // Example result:
            // {"name": "John", "age": 25, "city": "New York"}
        }
    }
Here is the proof it doesn’t keep the history, but we can implement it in different way. Take a look:


    [Fact]
    public async Task keep_the_context()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "What are 3 the biggest cities in Germany?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        completionsOptions.Messages.Add(result.Value.Choices[0].Message);
        completionsOptions.Messages.Add(new ChatMessage(ChatRole.User, "Return these values as json with fields: 'name', 'population', 'establishYear' "));
        var result2 = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result2.HasValue)
        {
            Console.WriteLine(result2.Value.Choices[0].Message.Content);
            // Example result:
            // {
            //     [
            //     {
            //         "name": "Berlin",
            //         "population": "3,769,495",
            //         "establishYear": "1237"
            //     },
            //     {
            //         "name": "Hamburg",
            //         "population": "1,899,764",
            //         "establishYear": "808"
            //     },
            //     {
            //         "name": "Munich",
            //         "population": "1,558,395",
            //         "establishYear": "1158"
            //     }
            //     ]
            // }
        }
    }
As you can see the continuation is kept. So if you want to keep the context you can do it like that. Moreover you can pass prepare the examples yourself to pass to the AI the context you wish. No one will stop you from doing things like that:


    [Fact]
    public async Task prepare_the_context()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "What are 3 the biggest cities in Poland?"),
            new ChatMessage(ChatRole.Assistant, "Warsaw, Jasło, Mediolan"),
            new ChatMessage(ChatRole.User, "Where they are located?"),
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);
        Console.WriteLine(result.Value.Choices[0].Message.Content);
        // Example result:
        // Warsaw is located in east-central Poland, Jasło is located in southeastern Poland, and Mediolan is actually a city in Italy, not in Poland.
    }
Of course in real life you can add to the context better information about your app, your company, your product or user context. We will do more logic experiments in further posts.

5. Lean how to prompt

If you want to use the tool in real life you must learn how to create prompts. Then you will gets wanted results. Let assume you want to create a regex to find leading zeros from a number.

  • There are some requirements:
    • If zero is at the beginning then is should be find
    • If there is comma after zero then is should be ignored
  • Prompt examples:
    • Simple question
      • prompt: “What is regex to remove leading zeros”
      • answer: ^0+
      • test samples:
        • 000231 => 231 - OK
        • 000,231 => 000,231 - Not OK
      • It is a simple answer for simple question. We didn’t pass all out requirements. Let’s try to so it.
    • Simple Question with requirements:
      • prompt: “What is regex to remove leading zeros. If zero is at the beginning then is should be find. If there is comma after zero then is should be ignored.”
      • answer: ^(0+)(?![,])
      • test samples:
        • 00123 => 123 - OK
        • 00,123 => 0,123 - OK
    • You can do it by passing examples:
      • prompt: What is regex to remove leading zeros. Examples: 0001 => 1; 00123 => 123; 0,12 => 0,12; 00000,423 => 0,423;
      • answer: ^0+(?=\d)
      • test samples:
        • 00123 => 123 - OK
        • 00,123 => 0,123 - OK

You can also define the answer format like here:

    
    [Fact]
    public async Task ask_about_capitols_in_europe()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new []
        {
            new ChatMessage(ChatRole.User, "What are 5 the biggest cities in Poland?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);
       
        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Example result:
            // 1. Warsaw - The capital city of Poland, with a population of approximately 1.8 million.
            // 2. Kraków - The second-largest city, renowned for its historical significance and vibrant culture, with a population of approximately 780,000.
            // 3. Łódź - The third-largest city, known for its textile industry and thriving arts scene, with a population of approximately 690,000.
            // 4. Wrocław - The fourth-largest city and the cultural capital of Lower Silesia, with a population of approximately 640,000.
            // 5. Poznań - The fifth-largest city, famous for its trade fairs and historical landmarks, with a population of approximately 540,000.
        }
    }
    

but… to be honest it is a nice feature as a chat or Google alternative. If you would like to make it more useful from developer perspective you can modify the prompt to return th data in JSON format for examle. It can look like that:

    
    [Fact]
    public async Task ask_about_capitols_in_europe_json()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new []
        {
            new ChatMessage(ChatRole.User, """
                                           What are 5 the biggest cities in Spain?. 
                                           Return the data in json format with fields: 'CityName', 'Population', 'Area'.
                                           """)
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);
       
        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Example result:
            // {
            //     "cities": [
            //     {
            //         "CityName": "Madrid",
            //         "Population": 3.2,
            //         "Area": 604.3
            //     },
            //     {
            //         "CityName": "Barcelona",
            //         "Population": 1.6,
            //         "Area": 101.9
            //     },
            //     {
            //         "CityName": "Valencia",
            //         "Population": 0.8,
            //         "Area": 134.6
            //     },
            //     {
            //         "CityName": "Seville",
            //         "Population": 0.7,
            //         "Area": 140.8
            //     },
            //     {
            //         "CityName": "Zaragoza",
            //         "Population": 0.7,
            //         "Area": 973.8
            //     }
            //     ]
            // }
        }
    }
    

and… now it is more useful, because answer like that is easy to parse and process in a further steps. You still have to be careful, because in example like that there are no information about the unit. You cannot be sure if the are is in square kilometers or miles. The same with population. It can be millions or thousands. So it is always better to be more strict when you ask about data like that and you can see it in the next example.

    
    [Fact]
    public async Task ask_about_capitols_in_europe_with_strict_units_json()
    {
        var aiClient = OpenAIClientFactory.Create();

        var completionsOptions = new ChatCompletionsOptions(new []
        {
            new ChatMessage(ChatRole.User, """
                                           What are 3 the biggest cities in Sweden?.
                                           Return the data in json format with fields: 'CityName', 'Population', 'Area'.
                                           Area should be in km2.
                                           Population should not be rounded.
                                           """)
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);
       
        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Example result:
            // {
            //     "cities": [
            //     {
            //         "CityName": "Stockholm",
            //         "Population": 975904,
            //         "Area": 188.00
            //     },
            //     {
            //         "CityName": "Gothenburg",
            //         "Population": 583056,
            //         "Area": 447.00
            //     },
            //     {
            //         "CityName": "Malmö",
            //         "Population": 319666,
            //         "Area": 77.06
            //     }
            //     ]
            // }
        }
    }
    

6. How to pass your data to ChatGPT

There is no dedicated way to do it. You are not able to train chat GPT with your own data. But there is a way to allow it working in your context. You just need to add the information to the prompt. Of course is it limited, but let’s see how it works:

When there is no context it won’t be able to tell you anything:


    [Fact]
    public async Task prompt_without_context()
    {
        var aiClient = OpenAIClientFactory.Create();
        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.User, "When can I watch today evening?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            // Example result:
            // As an AI, I don't have access to current time or location information. However, you can check local TV listings, streaming platforms, or websites for movie theaters in your area to see what options are available for viewing this evening.
        }
    }

You you can add there some context. To allow answering it with more sense:


    [Fact]
    public async Task prompt_with_context()
    {
        var aiClient = OpenAIClientFactory.Create();
        var completionsOptions = new ChatCompletionsOptions(new[]
        {
            new ChatMessage(ChatRole.System, """
                                             You are giving information about cinema and suggest people what to watch.
                                             Context:
                                             Name of the cinema: Big Screens
                                             Address: Main Movie street 17, Sydney
                                             Repertoire for today:
                                             17:30 - "The Lord of the Rings"
                                             18:25 - "Kill Bill"
                                             19:15 - "The Godfather"
                                             20:20 - "Alice in Wonderland"
                                             21:00 - "The Matrix"
                                             22:30 - "Saw: The Final Chapter"
                                             """),
            new ChatMessage(ChatRole.User, "When can I watch today after 7?")
        });
        var result = await aiClient.GetChatCompletionsAsync("gpt-3.5-turbo", completionsOptions);

        if (result.HasValue)
        {
            Console.WriteLine(result.Value.Choices[0].Message.Content);
            //Example result:
            // After 7 PM today at Big Screens, you can watch "The Godfather" at 19:15 or "Alice in Wonderland" at 20:20.
        }
    }

Of course it is a simple data you can make it more complex. It depends on your needs. There are more ways to prepare the context, but I will show you them in the next posts.

Summary

Thank you for reading the article. I hope you know what you came here. If no, let me know what you are looking for and I will try to help you with that.

comments powered by Disqus

Are you still here? Subscribe for more content!