Skip to main content
You are currently on a page documenting the use of OpenAI text completion models. The latest and most popular OpenAI models are chat completion models.Unless you are specifically using gpt-3.5-turbo-instruct, you are probably looking for this page instead.
OpenAI is an artificial intelligence (AI) research laboratory. This will help you get started with OpenAI completion models (LLMs) using LangChain. For detailed documentation on OpenAI features and configuration options, please refer to the API reference.

Overview

Integration details

ClassPackageLocalSerializablePY supportDownloadsVersion
OpenAI@langchain/openaiNPM - DownloadsNPM - Version

Setup

To access OpenAI models you’ll need to create an OpenAI account, get an API key, and install the @langchain/openai integration package.

Credentials

Head to platform.openai.com to sign up to OpenAI and generate an API key. Once you’ve done this set the OPENAI_API_KEY environment variable:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain OpenAI integration lives in the @langchain/openai package:

Instantiation

Now you can instantiate the model and generate text completions:

Invocation

Custom URLs

You can customize the base URL the SDK sends requests to by passing a configuration parameter like this:
You can also pass other ClientOptions parameters accepted by the official SDK. If you are hosting on Azure OpenAI, see the dedicated page instead.

API reference

For detailed documentation of all OpenAI features and configurations head to the API reference.