Open AI
What is Open AI?
OpenAI is a private research laboratory that aims to develop and direct artificial intelligence (AI) in ways that benefit humanity as a whole. The company was founded by Elon Musk, Sam Altman and others in 2015 and is headquartered in San Francisco.
The OpenAI API can be applied to virtually any task. We offer a range of models with different capabilities and price points, as well as the ability to fine-tune custom models.
Text generation models
OpenAI’s text generation models (often referred to as generative pre-trained transformers or “GPT” models for short), like GPT-4 and GPT-3.5, have been trained to understand natural and formal language. Models like GPT-4 allows text outputs in response to their inputs. The inputs to these models are also referred to as “prompts”. Designing a prompt is essentially how you “program” a model like GPT-4, usually by providing instructions or some examples of how to successfully complete a task. Models like GPT-4 can be used across a great variety of tasks including content or code generation, summarization, conversation, creative writing, and more. Read more in our introductory text generation guide and in our prompt engineering guide.
Assistants
Assistants refer to entities, which in the case of the OpenAI API are powered by large language models like GPT-4, that are capable of performing tasks for users. These assistants operate based on the instructions embedded within the context window of the model. They also usually have access to tools which allows the assistants to perform more complex tasks like running code or retrieving information from a file. Read more about assistants in our Assistants API Overview.
Embeddings
An embedding is a vector representation of a piece of data (e.g. some text) that is meant to preserve aspects of its content and/or its meaning. Chunks of data that are similar in some way will tend to have embeddings that are closer together than unrelated data. OpenAI offers text embedding models that take as input a text string and produce as output an embedding vector. Embeddings are useful for search, clustering, recommendations, anomaly detection, classification, and more. Read more about embeddings in our embeddings guide.
Tokens
Text generation and embeddings models process text in chunks called tokens. Tokens represent commonly occurring sequences of characters. For example, the string ” tokenization” is decomposed as ” token” and “ization”, while a short and common word like ” the” is represented as a single token. Note that in a sentence, the first token of each word typically starts with a space character. Check out our tokenizer tool to test specific strings and see how they are translated into tokens. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text.
One limitation to keep in mind is that for a text generation model the prompt and the generated output combined must be no more than the model’s maximum context length. For embeddings models (which do not output tokens), the input must be shorter than the model’s maximum context length. The maximum context lengths for each text generation and embeddings model can be found in the model index.
Quickstart for developer
Go to Open AI and then to Quick Start
The OpenAI API provides a simple interface for developers to create an intelligence layer in their applications, powered by OpenAI’s state of the art models. The Chat Completions endpoint powers ChatGPT and provides a simple way to take text as input and use a model like GPT-4 to generate an output.
This quickstart is designed to help get your local development environment setup and send your first API request. If you are an experienced developer or want to just dive into using the OpenAI API, the API reference of GPT guide are a great place to start. Throughout this quickstart, you will learn:
- How to setup your development environment
- How to install the latest SDKs
- Some of the basic concepts of the OpenAI API
- How to send your first API request
Account setup
First, create an OpenAI account or sign in. Next, navigate to the API key page and “Create new secret key”, optionally naming the key. Make sure to save this somewhere safe and do not share it with anyone.
Quickstart language selection
Select the tool or language you want to get started using the OpenAI API with.
I have select Python.
Python is a popular programming language that is commonly used for data applications, web development, and many other programming tasks due to its ease of use. OpenAI provides a custom Python library which makes working with the OpenAI API in Python simple and efficient.
Step 1: Setup Python
Install Python
To use the OpenAI Python library, you will need to ensure you have Python installed. Some computers come with Python pre-installed while others require that you set it up yourself. To test if you have Python installed, you can navigate to your Terminal or Command line:
- MacOS: Open Terminal: You can find it in the Applications folder or search for it using Spotlight (Command + Space).
- Windows: Open Command Prompt: You can find it by searching “cmd” in the start menu.
Next, enter the word python
and then press return/enter. If you enter into the Python interpreter, then you have Python installed on your computer already and you can go to the next step. If you get an error message that says something like “Error: command python not found”, you likely need to install Python and make it available in your terminal / command line.
To download Python, head to the official Python website and download the latest version. To use the OpenAI Python library, you need at least Python 3.7.1 or newer. If you are installing Python for the first time, you can follow the official Python installation guide for beginners.
Setup a virtual environment (optional)
openai-env\Scripts\activate
pip install --upgrade openai
pip list
will show you the Python libraries you have installed in your current environment, which should confirm that the OpenAI Python library was successfully installed.Step 2: Setup your API key
Setup your API key for all projects (recommended)
The main advantage to making your API key accessible for all projects is that the Python library will automatically detect it and use it without having to write any code.
Windows
- Open Command Prompt: You can find it by searching “cmd” in the start menu.
- Set environment variable in the current session: To set the environment variable in the current session, use the command below, replacing
your-api-key-here
with your actual API key:
setx OPENAI_API_KEY "your-api-key-here"
This command will set the OPENAI_API_KEY environment variable for the current session.
3. Permanent setup: To make the setup permanent, add the variable through the system properties as follows:
-
- Right-click on ‘This PC’ or ‘My Computer’ and select ‘Properties’.
- Click on ‘Advanced system settings’.
- Click the ‘Environment Variables’ button.
- In the ‘System variables’ section, click ‘New…’ and enter OPENAI_API_KEY as the variable name and your API key as the variable value.
4. Verification: To verify the setup, reopen the command prompt and type the command below. It should display your API key: echo %OPENAI_API_KEY%
Setup your API key for a single project
f you only want your API key to be accessible to a single project, you can create a local .env
file which contains the API key and then explicitly use that API key with the Python code shown in the steps to come. Start by going to the project folder you want to create the .env
file in.
Note: In order for your .env file to be ignored by version control, create a .gitignore file in the root of your project directory. Add a line with .env on it which will make sure your API key or other secrets are not accidentally shared via version control.
Once you create the .gitignore
and .env
files using the terminal or an integrated development environment (IDE), copy your secret API key and set it as the OPENAI_API_KEY
in your .env
file. If you haven’t created a secret key yet, you can do so on the API key page.
The .env
file should look like the following:
# Once you add your API key below, make sure to not share it with anyone! The API key should remain private.
OPENAI_API_KEY=abc123
from openai import OpenAI
client = OpenAI()
# defaults to getting the key using os.environ.get("OPENAI_API_KEY")
# if you saved the key under a different environment variable name, you can do something like:
# client = OpenAI(
# api_key=os.environ.get("CUSTOM_ENV_NAME"),
# )
Step 3: Sending your first API request