OpenAI Class — langchain Architecture
Architecture documentation for the OpenAI class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d["OpenAI"] 6bee45b2_b649_e251_1fdc_dcf49f8bb331["BaseOpenAI"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|extends| 6bee45b2_b649_e251_1fdc_dcf49f8bb331 bcd5af42_a82b_f85e_ed42_ee1d1f435ba0["base.py"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|defined in| bcd5af42_a82b_f85e_ed42_ee1d1f435ba0 26320b12_5216_f838_8c35_d1d80b3ebdea["get_lc_namespace()"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|method| 26320b12_5216_f838_8c35_d1d80b3ebdea 330eddc2_312c_f44e_2872_cdcf350f514c["is_lc_serializable()"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|method| 330eddc2_312c_f44e_2872_cdcf350f514c cf1be869_4303_f452_9b24_60e5d94442c6["_invocation_params()"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|method| cf1be869_4303_f452_9b24_60e5d94442c6 a56e80b7_35a6_2589_4b3d_c7ba8db080fd["lc_secrets()"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|method| a56e80b7_35a6_2589_4b3d_c7ba8db080fd af75db77_08ff_e43e_0bbd_02bcae589034["lc_attributes()"] a6418a9e_fe6b_f3f5_22b9_f30ef322ae2d -->|method| af75db77_08ff_e43e_0bbd_02bcae589034
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/llms/base.py lines 740–872
class OpenAI(BaseOpenAI):
"""OpenAI completion model integration.
Setup:
Install `langchain-openai` and set environment variable `OPENAI_API_KEY`.
```bash
pip install -U langchain-openai
export OPENAI_API_KEY="your-api-key"
```
Key init args — completion params:
model:
Name of OpenAI model to use.
temperature:
Sampling temperature.
max_tokens:
Max number of tokens to generate.
logprobs:
Whether to return logprobs.
stream_options:
Configure streaming outputs, like whether to return token usage when
streaming (`{"include_usage": True}`).
Key init args — client params:
timeout:
Timeout for requests.
max_retries:
Max number of retries.
api_key:
OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`.
base_url:
Base URL for API requests. Only specify if using a proxy or service
emulator.
organization:
OpenAI organization ID. If not passed in will be read from env
var `OPENAI_ORG_ID`.
See full list of supported init args and their descriptions in the params section.
Instantiate:
```python
from langchain_openai import OpenAI
model = OpenAI(
model="gpt-3.5-turbo-instruct",
temperature=0,
max_retries=2,
# api_key="...",
# base_url="...",
# organization="...",
# other params...
)
```
Invoke:
```python
input_text = "The meaning of life is "
model.invoke(input_text)
```
```txt
"a philosophical question that has been debated by thinkers and scholars for centuries."
```
Stream:
```python
for chunk in model.stream(input_text):
print(chunk, end="|")
```
```txt
a| philosophical| question| that| has| been| debated| by| thinkers| and| scholars| for| centuries|.
```
```python
"".join(model.stream(input_text))
```
```txt
"a philosophical question that has been debated by thinkers and scholars for centuries."
```
Async:
Extends
Source
Frequently Asked Questions
What is the OpenAI class?
OpenAI is a class in the langchain codebase, defined in libs/partners/openai/langchain_openai/llms/base.py.
Where is OpenAI defined?
OpenAI is defined in libs/partners/openai/langchain_openai/llms/base.py at line 740.
What does OpenAI extend?
OpenAI extends BaseOpenAI.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free