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
- 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
May be interested
- How to set up Python to program on WSLget started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
- Multiple choice quiz about Python - Part 4continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
- How to use Closure in Pythonin this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
- Functions in Pythonwhat is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
- Multiple choice quiz about Python - Part 7following the previous test set, part 7 continues with the topic built-in functions in python. let's try with quantrimang to try the 10 questions below.
- The next () function in Pythonthe next () function in python returns the next element in the iterator. you can add a default value to return if iterable is already the last element.
- Multiple choice quiz about Python - Part 10following the previous test, part 10 returned with the python function. let's try with quantrimang to try the 10 questions below.
- Why should you learn Python programming language?python is a multi-purpose programming language created in the late 1980s and named after monty python drama group. let's tipsmake.com find out 3 reasons you should learn python programming language in this article!
- Package in Pythonwe will learn how to divide code into efficient, clear modules, using python packages. plus, the way to import and use your own package, or the package you download from somewhere into the python program.
- Multiple choice test on Python - Part 11python's multiple-choice questions series help you update and review useful knowledge for your work and learn your python programming language.