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 .
3. Click Create new secret key to generate API key.
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:
- engine : The template you want to use for the query. Model Davinci may be the most reliable. It trained on data until October 2019.
- prompt : Gather your questions slowly to generate responses from the API.
- 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.
- 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:
Here's how you can use GPT-3 with Python. Hope the article is useful to you.
You should read it
- 5 choose the best Python IDE for you
- How to set up Python to program on WSL
- What is Python? Why choose Python?
- More than 100 Python exercises have solutions (sample code)
- Why should you learn Python programming language?
- Multiple choice quiz about Python - Part 3
- Bookmark 5 best Python programming learning websites
- Python data type: string, number, list, tuple, set and dictionary
- For in Python loop
- Multiple choice quiz about Python - Part 4
- Multiple choice test on Python - Part 11
- Manage files and folders in Python
Maybe you are interested
OpenAI Introduces ChatGPT Projects: New Features to Organize Smarter Conversations
OpenAI Announces ChatGPT Pro Plan for a whopping $200 per month
OpenAI is close to striking a deal with Samsung to use its AI features in Galaxy phones
OpenAI Challenges Google Chrome With Its New Browser
OpenAI is worried that users will 'love' ChatGPT, affecting interactions between people
OpenAI considers adding a watermark to ChatGPT-generated text