Openai API interface provides a set of APIs for developers to leverage OpenAI’s language models and other AI capabilities.
API key
To use OpenAI APIs, we need an API key. This API key can be obtained from https:://platform.openai.com/api-keys. Below screen shots show the related pages. Select “create new secret key” and submit “create secret key”:
When a secret key is created, it is listed under API keys. This key is used to authenticate the requests when an API is called. Note that this key should not be shared with others.
openai API provides APIs for many functionalities. Below are some of the APIs:
- Completion API is used to generate text on a given prompt.
- Chat API is used to generate conversational responses.
- Edit API is used to provide suggestions for existing text
- Image generation API used to generate images.
- Embedding API is used to generate vector representation of the text.
- Moderation API is used to check content for policy compliance
- Fine-tune API is used to fine-tune models on custom dataset.
- File API is used to manage files mostly used for fine-tuning.
- Audio API is used to transcribe or to translate audio.
Following are the brief steps to use a openai Completion API:
- install modules python-dotenv, openai packages. python-dotenv package is used to read key-value pairs from a ‘.env’ file and and set them as environment variables. openai package is used for openai API calls.
- “load_dotenv(find_dotenv())” loads the environment variables from the .env file into the system’s environment variables. ‘os.environ[‘OPENAI_API_KEY’] ‘ retrieves the OPENAI_API_KEY environment variable. This key is required to authenticate and make requests to the openai API.
- set the model variable., eg. llm_model = “gpt-3.5-turbo-0301”
- openai offers several language models: GPT-3 models (davinci, curie, babbage, ada) , GPT-3.5 models (gpt-3.5-turbo, gpt-3.5-turbo-0301), GPT-4 models (gpt-4, gpt-4-turbo), Codex models (code-davinci-002 and code-cushman-001).
- openai API invocation:
openai.ChatCompletion.create() method generates the response based on the given model and messages. It takes 3 arguments:
— model: It’s a string having the GPT model name
— messages: It’s a list of dictionaries (key-value pairs) having two types of keys: role and content. role specifies the role of the message’s author. It can have values as “user”, “system”, or “assistant”. “user” value will indicate the messages authored by the user. “system” value will indicate the instructions set to set the behavior of the assistant. “assistant” value will indicate the messages authored by the assistant. content specifies the message.
— temperature: This parameter controls the randomness of the model’s output and the creativity and diversity of the responses generated by the model. Its value ranges from 0 to 1. The default is 1. A low temperature (near to 0) value leads to a more deterministic, focused output with the most probable words and phrases, useful for tasks requiring precise and accurate answers. A high temperature (near to 1) value leads to more random, creative output with more diverse responses, useful for brainstorming and various possible answers.
— max_tokens: To specify the maximum number of tokens.