Published in blog / machine-learning
Tags: / /

Introduction

Very recently we have described the meaning of the new much larger context window for GPT4-Turbo. Now I am still mindblown how fast and reliable the new GPT4-Turbo has become, despite some outages in the first days due to overwhelming load.

What I didn't realize was that OpenAI has changed the API quite recently, see a GitHub post here, "v1.0.0 Migration Guide".

This of course broke my layman code, which I had to fix.

As an example I want to show you a simple implementation for a chat bot on the console using python.

A simple Python Console GPT4-Turbo Chatbot Code

Without further due, here is the code based on "How to Build your own custom ChatGPT Using Python & OpenAI":

import os
from openai import OpenAI

def chatbot():
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
    ]

    client = OpenAI(api_key="your-secret-key")

    while True:
        message = input("User: ")

        if message.lower() == "quit":
            break

        messages.append({"role": "user", "content": message})

        response = client.chat.completions.create(
            model="gpt-4-1106-preview", # you may also use gpt-3.5-turbo here
            messages=messages
        )

        chat_message = response.choices[0].message.content
        print(f"Bot: {chat_message}")
        messages.append({"role": "assistant", "content": chat_message})

if __name__ == "__main__":
    print("Start chatting with the bot (type 'quit' to stop)!")
    chatbot()

Simply copy the code into a file. Note to provide your OpenAI API key for "your-secret-key".

Now run the file with python3:

python3 myawesomechatbot.py

Example Usage

Here's what happens when you execute the program.

Start chatting with the bot (type 'quit' to stop)!

User: Hi GPT4-Turbo! Can you tell us a bit about yourself? You are just a few days old but already famous!

Bot: Hello! I'm actually not GPT-4-Turbo, but I am a model created by OpenAI designed to assist with a wide variety of tasks and answer questions to the best of my ability, based on the information up to my training cutoff in early 2023.

While I can't comment on being "famous" or a few days old since I don't have personal experiences or a sense of time, I am programmed to assist users by generating text-based responses. My purpose is to help provide information, facilitate understanding, and collaborate on creative endeavors across a multitude of topics, from science and technology to culture and entertainment.

If you have any questions or need assistance with something, feel free to ask!

User: Well, that's fine for now! Can you tell me who won the last soccer world cup?

Bot: The most recent FIFA World Cup before my knowledge cutoff in early 2023 was held in 2018, and the winner was France. They defeated Croatia 4-2 in the final match to secure their second World Cup title. The next FIFA World Cup was scheduled to take place in Qatar in 2022, but I do not have the records for that event. For the latest results, you would need to refer to the most recent sources.

User: quit

Note that GPT4-Turbo does not know it is actually GPT4-Turbo as stated by OpenAI themselves. Also it says it has a cutoff early 2023 but does not recognize who won the FIFA World Cup 2022. Interestingly, it did know that at another try! So there seems to be a bit of additional work ahead for OpenAI before the production release.

I hope you found this very simple implementation somewhat helpful. Happy coding!