How to use GPT-3 with Python

Feel free to use cool GPT-3 technology with your own Python scripts using OpenAI's handy API. Here are details on how to use GPT-3 with Python.

AI 'storm' is sweeping the globe. OpenAI's release of ChatGPT has excited developers and curious users alike. OpenAI has now amassed around 100 million active users within two months of its launch. This is really an impressive number. Currently, many programmers have started building applications using it.

CopyAI uses it to edit content for web, blog, advertising, email and social media. Lex uses GPT-3 to answer research questions, Replier to generate responses to customer reviews… In this article, let's learn how to use OpenAI's GPT-3 model with Python in building AI-powered applications.

What is GPT-3?

OpenAI's GPT-3 is a 3rd generation pre-trained Transformer. It's basically a machine learning model with over 175 billion parameters, almost the entire Internet. This gives it tremendous power to answer many types of questions, while also performing laborious tasks.

Open AI has developed a Python module that contains predefined compatibility classes to interact with the API. If you want to install it on your system, open a terminal and run:

pip install openai

Generate API key

To use GPT-3 with Python, you need to generate an API key. To view it, follow these steps:

1. Register an account on the OpenAI site . Select the account type as Personal .

2. Click on the profile and select the View API Keys button .

Picture 1 of How to use GPT-3 with Python

3. Click Create new secret key to generate API key.

Picture 2 of How to use GPT-3 with Python

4. Copy the API key and keep it in a safe place because you cannot review it.

OpenAI's GPT-3 API charges a fee based on the number of tokens (the words you use to interact with it). Luckily, OpenAI gives away $18 for the first 3 months for free, so you can explore and test it as you like.

Build a Python program to use the GPT-3 API

Now that you have access to the API, you can use it to build your Python communication program. Start creating the program from importing the OpenAI module. Specify a function, askGPT(), that takes text as an input argument. This text will contain the query you intend to ask GPT-3. Copy the API key created from scratch and launch it.

 

import openai def askGPT(text):     openai.api_key = "your_api_key"

Create a query by specifying the following parameters:

  1. engine : The template you want to use for the query. Model Davinci may be the most reliable. It trained on data until October 2019.
  2. prompt : Gather your questions slowly to generate responses from the API.
  3. temperature : Set the level of professionalism or creativity of the text. With a low value, the answer is concise, focusing on the main idea. With a higher value, you get a more creative answer. 0.6 is a good compromise.
  4. max_tokens : The number of words in the generated response. You can set it to a maximum of 2,048 words.

For example, here's how you can submit a query and save the response:

    response = openai.Completion.create(         engine = "text-davinci-003",         prompt = text,         temperature = 0.6,         max_tokens = 150,     ) 

Show GPT-3's response by taking the text parameter of the first result:

 return print(response.choices[0].text)

To call this function, define a main function and its infinite loop. Ask the user to enter a question and pass it to the askGpt() function .

def main():     while True:         print('GPT: Ask me a questionn')         myQn = input()         askGPT(myQn) main()

Put it all together and use artificial intelligence to answer your questions.

Result:

When running this program, it will ask you to enter a question. For example: "Write a poem in 5 lines about how Iron Man is the greatest superhero of all time," it will give the answer Impressive words are as follows:

Picture 3 of How to use GPT-3 with Python

Here's how you can use GPT-3 with Python. Hope the article is useful to you.

Update 15 March 2023
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile