top of page
  • Writer's pictureKostiantyn Isaienkov

How to use OpenAI API in Python

Today everyone is familiar with the abilities of the ChatGPT. It provides a wide range of features for solving machine learning tasks and assisting in different routine problems, not depending on the domain. OpenAI proposes a comfortable user interface for this purpose, so each person can log in and ask ChatGPT everything. But what if you need to use the GPT model inside your application? For this purpose, OpenAI prepared an API that simplifies the integration of GPT models into your application. Let's go deeper and understand how to use OpenAI Python API for large language models in a simple coding example.


OpenAI Python library installation


Hi there! Today we are not going to cover any conceptual details about GPT models and LLMs in general - we just cover the topic of usage of the OpenAI API for Python. And the first step is to install the corresponding Python library:

 pip install openai

This step will install openai into your environment and from this point you are able to use this library.


API Authentication


To have the ability to use OpenAI API, you should get an API key from the OpenAI page. If you get your first API key, you will have a trial period with a small initial credit. But in case you decide to upgrade your account for development / commercial purposes, you will start to pay money for the API.

Prices for the API calls are in the picture above, but they are actual for the time this paper is writing. It is always better to visit the official page and check current prices.


API Usage


Now we have the Python library installed and the available API key for openAI, so we are ready to write our first API call to the GPT model using Python. We are not demonstrating here any advanced code constructions - just a simple API call with a simple message.

This code will produce the following output:

{

"id": "chatcmpl-6757575757",

"object": "chat.completion",

"created": 87888,

"model": "gpt-3.5-turbo-0613",

"choices": [

{

"index": 0,

"message": {

"role": "assistant",

"content": "The capital of the United States of America is Washington, D.C."

},

"finish_reason": "stop"

}

],

"usage": {

"prompt_tokens": 14,

"completion_tokens": 14,

"total_tokens": 28

}

}


In case you need to get an exact answer instead of this dictionary, just substitute "answer" with "answer['choices'][0]['message']['content']" in the code snippet above.


Let's take a look at the parameters that we use in our example:

  • model: large language model that will be used for answering. We use the GPT-3.5-turbo, which is currently used by ChatGPT by default. You should remember that the GPT-4 model can be not available even if you have an upgraded account. Probably, you will need to request it manually.

  • max_tokens: the maximum number of tokens that the model will generate for the answer. The value of this parameter will affect the query execution time.

  • messages: your message/input to the model. It should be a list of dictionaries, each of that contains a role (can be user, system, assistant) and content, basically your question.


As you can see, there are no difficulties in using GPT API provided by OpenAI. So you can easily start developing your solution based on modern NLP technologies. Hope this tutorial was helpful to you. In case you like it, we will continue the topic of Large Language models with new tasks and more challenging coding exercises. See you on the following papers and tutorials!

Recent Posts

See All
bottom of page