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 offers a spectrum of models with different levels of power suitable for different tasks. This example goes over how to use LangChain to interact with OpenAI models

Overview

Integration details

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’re routing requests through a proxy or service emulator, you can set the base URL via env var instead of passing base_url. Resolution order (first match wins):
  1. Explicit base_url (or openai_api_base) kwarg.
  2. OPENAI_API_BASE — read by LangChain at init.
  3. OPENAI_BASE_URL — read by the underlying openai SDK client.
To enable automated tracing of your model calls, set your LangSmith API key:

Installation

The LangChain OpenAI integration lives in the langchain-openai package:
Should you need to specify your organization ID, you can use the following cell. However, it is not required if you are only part of a single organization or intend to use your default organization. You can check your default organization on the OpenAI API keys page. To specify your organization, you can use this:

Instantiation

Now we can instantiate our model object and generate chat completions:

Invocation

Chaining

Using a proxy

If you are behind an explicit proxy, you can specify the http_client to pass through

API reference

For detailed documentation of all OpenAI llm features and configurations head to the API reference: reference.langchain.com/python/langchain-openai/llms/base/OpenAI