Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Interaction with ChatGPT: API | ChatGPT Overview
Introduction to Chat GPT
course content

Course Content

Introduction to Chat GPT

Introduction to Chat GPT

1. ChatGPT Overview
2. Prompt Engineering Techniques
3. Formatting the Output of the Chat

book
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:

  1. Install openai package using pip install openai;
  2. Get your secret API key and specify api_key argument of the OpenAI 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:

  1. Create a string variable containing your request (referred to as the prompt variable in the code above);
  2. Use get_completion() function to generate response. You can specify another model argument of this function and use another temperature parameter inside the function;
  3. 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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt