# OpenAI

> **Class** in `langchain_openai`

📖 [View in docs](https://reference.langchain.com/python/langchain-openai/llms/base/OpenAI)

OpenAI completion model integration.

## Signature

```python
OpenAI()
```

## Description

**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:**

```python
await model.ainvoke(input_text)

# stream:
# async for chunk in (await model.astream(input_text)):
#    print(chunk)

# batch:
# await model.abatch([input_text])
```
```txt
"a philosophical question that has been debated by thinkers and scholars for centuries."
```

## Extends

- `BaseOpenAI`

## Properties

- `lc_secrets`
- `lc_attributes`

## Methods

- [`get_lc_namespace()`](https://reference.langchain.com/python/langchain-openai/llms/base/OpenAI/get_lc_namespace)
- [`is_lc_serializable()`](https://reference.langchain.com/python/langchain-openai/llms/base/OpenAI/is_lc_serializable)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/9f232caa7a8fe1ca042a401942d5d90d54ceb1a6/libs/partners/openai/langchain_openai/llms/base.py#L750)