[Python Examples] 2. Create a Simple Assistant Using OpenAI's ChatGPT

10 May 2024 By Code Bricks

Tutorial: Create a Simple Assistant Using OpenAI’s ChatGPT

Introduction

Welcome to this fun and easy tutorial where we’ll learn how to create a simple assistant using OpenAI’s ChatGPT. We’ll use Python, a beginner-friendly programming language, to build our assistant.

What You Will Need

  1. A computer with Python installed. You can download Python from python.org.
  2. An OpenAI API key. You can get this by signing up on the OpenAI website.
  3. A code editor like VS Code, PyCharm, or even a simple text editor like Notepad.

Step 1: Install Required Libraries

First, we need to install the openai library. Open your command prompt or terminal and type:

pip install openai

Step 2: Import Libraries and Set Up API Key

Create a new Python file, let’s call it assistant.py. Start by importing the necessary libraries and setting up your API key.

import openai

# Replace 'your-api-key' with your actual OpenAI API key
openai.api_key = 'your-api-key'

Step 3: Create a Function to Call ChatGPT

Next, we’ll create a function to interact with the ChatGPT API.

def ask_chatgpt(question):
    response = openai.Completion.create(
        engine="text-davinci-003",  # Using the GPT-3 model
        prompt=question,
        max_tokens=150  # Limiting the response length
    )
    return response.choices[0].text.strip()

# Example question
question = "What is the capital of France?"
answer = ask_chatgpt(question)
print("Assistant:", answer)

Step 4: Make the Assistant Interactive

Let’s make our assistant interactive by allowing it to ask questions repeatedly.

def chat_with_assistant():
    print("Hello! I am your assistant. You can ask me anything. Type 'exit' to stop.")
    while True:
        user_input = input("You: ")
        if user_input.lower() == 'exit':
            print("Goodbye!")
            break
        answer = ask_chatgpt(user_input)
        print("Assistant:", answer)

# Start the chat
chat_with_assistant()

Step 5: Run Your Assistant

Save your file and run it using the command:

python assistant.py

Complete Code

Here is the complete code for your simple assistant:

import openai

# Replace 'your-api-key' with your actual OpenAI API key
openai.api_key = 'your-api-key'

def ask_chatgpt(question):
    response = openai.Completion.create(
        engine="text-davinci-003",  # Using the GPT-3 model
        prompt=question,
        max_tokens=150  # Limiting the response length
    )
    return response.choices[0].text.strip()

def chat_with_assistant():
    print("Hello! I am your assistant. You can ask me anything. Type 'exit' to stop.")
    while True:
        user_input = input("You: ")
        if user_input.lower() == 'exit':
            print("Goodbye!")
            break
        answer = ask_chatgpt(user_input)
        print("Assistant:", answer)

# Start the chat
chat_with_assistant()

Conclusion

Congratulations! You’ve just created a simple assistant using OpenAI’s ChatGPT. Feel free to experiment with different questions and modify the code to make it more interesting. Happy coding!