Conteúdo do Curso
Introduction to Chat GPT
Introduction to Chat GPT
Interaction with ChatGPT: API
Using an API (Application Programming Interface) to interact with ChatGPT allows developers to programmatically integrate ChatGPT's capabilities into their applications, products, or services.
How to use ChatGPT API in Python?
You can use the following code in Python to interact with ChatGPT via API:
Now, let's look at all the steps in more detail!
Accessing ChatGPT via API
To access ChatGPT from your local server using Python, you have to:
- Install
openai
package usingpip install openai
; - Get your secret API key and specify
api_key
argument of theOpenAI
class constructor.
You can generate your personal secret API key via the following Link.
After following the link, click on the following button:
Now, you can use this API key to access ChatGPT!
Making requests
To make requests, follow these steps:
- Create a string variable containing your request (referred to as the
prompt
variable in the code above); - Use
get_completion()
function to generate response. You can specify anothermodel
argument of this function and use anothertemperature
parameter inside the function; - Print the response.
Extraction of the necessary information from ChatGPT response
The response (response
variable) received at the previous stage contains unnecessary metadata. To get a specific answer to the query, you need to use the following construction:
response.choices[0].message.content
.
As a result, you can seamlessly incorporate ChatGPT functionality into your projects with just a few lines of code and an API key, spanning from straightforward chatbots to intricate recommendation systems and virtual consultants.
Obrigado pelo seu feedback!