Python ChatGPT Chatbot Tutorial

How to set up your own Chatbot interface with Python in 3 minutes?

  1. Get an OpenAI account and get your API key

    https://platform.openai.com
  2. Install Python and the OpenAI Python library
pip install openai

3. Run Python in Visual Studio Code (or wherever)

import openai
openai.api_key = "sk-YOUR_KEY_COMES_HERE_MATE"

def chat_with_gpt(prompt):
    response = openai.chat.completions.create(
        model = "o1-mini",
        messages=[
            {"role": "assistant", "content": "You are a helpful assistant." },
            {"role": "user", "content": prompt}
            ],
        #temperature = 0.7,
    )
    return response.choices[0].message.content.strip()

if __name__ == "__main__":
    while True:
        user_input = input("Tom: ")
        if user_input.lower() in ["quit", "exit", "bye"]:
            break
        
        response = chat_with_gpt(user_input)
        print("AIgnostic: ", response)

You can specify any model you would like to test.