logo.png

Sign Up    FAQs

Find What Matters

Building a ChatBot with The OpenAI Completion API

ci2.png

A ChatBot Powered By The OpenAI's API

The OpenAI Completion API is a powerful tool that allows you to generate natural language text based on a given prompt. With the Completion API, you can build chatbots that can converse with users about a wide range of topics.

In this tutorial, we will show you how to use the OpenAI Completion API to build a simple chatbot. We will start by setting up the OpenAI API, and then we will write a function that uses the Completion API to generate responses to user input. Finally, we will put everything together in a simple chatbot program that allows the user to ask the chatbot questions.

A. Setting Up The OpenAI API

To use the OpenAI API, you will need to sign up for an API key. You can do this by visiting the OpenAI API (https://beta.openai.com/signup/) website. Once you have an API key, you will need to install the OpenAI Python library:

pip install openai

Next, you will need to set your API key as an environment variable. On Unix-like systems, you can do this by adding the following line to your ~/.bashrc file:

export OPENAI_API_KEY="your_api_key_here"

Replace your_api_key_here with your actual API key.

B. Building The ChatBot Function

Now that we have the OpenAI API set up, we can start building our chatbot. The first thing we need to do is write a function that takes a prompt as input and returns a response generated by the OpenAI API.

Code For ChatBot Function:

import openai

def chatbot(prompt):
  response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    temperature=0,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
    stop=["\n"]
  )
  return response.text

The chatbot function uses the openai.Completion.create method to generate a response based on the given prompt. We pass several parameters to this method to control the behavior of the chatbot.

Complete Explanation

'model': The name of the OpenAI model to use for completion. We are using the "text-davinci-003" model, which is a high-quality language model trained on a large dataset of human-generated text.

'prompt': The prompt for the chatbot. This is the user's input, which we will pass to the chatbot function when we call it.

'temperature': Controls the randomness of the chatbot's responses. A higher temperature will make the chatbot more random, while a lower temperature will make it more deterministic.

'max_tokens': The maximum number of tokens (words) in the chatbot's response.

'top_p': Controls the proportion of the mass of the distribution that the chatbot considers when generating responses. A higher value will make the chatbot's responses more predictable, while a lower value will make them more random.

'frequency_penalty': Controls the chatbot's preference for novel words. A positive frequency penalty will encourage the chatbot to use less common words, while a negative frequency penalty will encourage it to use more common words.

'presence_penalty': Controls the chatbot's preference for words that are present in the prompt. A positive presence penalty will encourage the chatbot to use words that are present in the prompt, while a negative presence penalty will encourage it to avoid such words.

'stop': A list of strings that, if encountered by the model, will cause it to stop generating text. In our example, we have specified that the chatbot should stop at each occurrence of the string "\n".   

C. Putting It All Together

Now that we have our chatbot function, we can use it to build a simple chatbot program. The chatbot program will ask the user to input a question, and then it will use the chatbot function to generate a response based on the user's input.

Code For The ChatBot Program:

import openai

def chatbot(prompt):
  response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    temperature=0,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
    stop=["\n"]
  )
  return response.text

while True:
  user_input = input("User: ")
  print("Bot:", chatbot(user_input))

To run the chatbot program, simply execute the script from the command line:

python chatbot.py

You can then ask the chatbot questions by typing them into the command line. The chatbot will use the OpenAI Completion API to generate responses based on your input.

Example

User: What is the capital of France?

Bot: The capital of France is Paris.

User: What is the population of France?

Bot: The population of France is approximately 67 million people.

User: How tall is the Eiffel Tower?

Bot: The Eiffel Tower is 324 meters tall.

User: How do you spell "banana"?

Bot: Banana is spelled "b-a-n-a-n-a".

As you can see, the chatbot is able to answer a variety of questions about different topics.

Conclusion:

In this tutorial, we showed you how to use the OpenAI Completion API to build a simple chatbot. We covered how to set up the OpenAI API and how to write a chatbot function that generates responses based on user input. We then put everything together in a chatbot program that allows the user to ask the chatbot questions.

There are many ways you can customize and extend the chatbot program we have built here. For example, you can change the model used by the chatbot, or you can adjust the parameters passed to the openai.Completion.create method to control the chatbot's behavior. You can also add additional features to the chatbot program, such as a way for the user to choose the topic of conversation or a way to save the chatbot's responses to a file.