BaseOpenAI Class — langchain Architecture
Architecture documentation for the BaseOpenAI class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 190518eb_9019_a413_fc06_0adb29ead9b3["BaseOpenAI"] 76008743_94c2_792b_9e4f_e1278ae47565["BaseLLM"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|extends| 76008743_94c2_792b_9e4f_e1278ae47565 e54d99db_ca92_8f97_0046_21d281a42f0f["base.py"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|defined in| e54d99db_ca92_8f97_0046_21d281a42f0f 7ec19fac_81f8_6160_2e5b_754736331c98["build_extra()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 7ec19fac_81f8_6160_2e5b_754736331c98 944ab591_8966_402d_62ce_053674ebe290["validate_environment()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 944ab591_8966_402d_62ce_053674ebe290 c7368a7b_fbd7_90d9_0c60_ea8e846e5a97["_default_params()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| c7368a7b_fbd7_90d9_0c60_ea8e846e5a97 7526e23a_b28d_ad39_20da_1fe94b57b40b["_stream()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 7526e23a_b28d_ad39_20da_1fe94b57b40b a9522cce_6ee6_5804_3f31_16471dcd2d07["_astream()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| a9522cce_6ee6_5804_3f31_16471dcd2d07 682d7743_2cd3_2c05_9c44_448c5a16ced7["_generate()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 682d7743_2cd3_2c05_9c44_448c5a16ced7 633d1165_fbf7_f90d_48ca_8ea517427584["_agenerate()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 633d1165_fbf7_f90d_48ca_8ea517427584 93852942_5e33_1789_8b8f_a171f4255f5c["get_sub_prompts()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 93852942_5e33_1789_8b8f_a171f4255f5c b2feae0d_5dc9_29cb_61e0_b9cfd00af24f["create_llm_result()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| b2feae0d_5dc9_29cb_61e0_b9cfd00af24f d7c431d0_c0be_5ba6_4654_63879d85502f["_invocation_params()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| d7c431d0_c0be_5ba6_4654_63879d85502f 09d0892e_ba31_0266_2dbe_ede1869624c5["_identifying_params()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| 09d0892e_ba31_0266_2dbe_ede1869624c5 c5c1419c_9a26_a692_50d4_8fb578fa4802["_llm_type()"] 190518eb_9019_a413_fc06_0adb29ead9b3 -->|method| c5c1419c_9a26_a692_50d4_8fb578fa4802
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/llms/base.py lines 53–737
class BaseOpenAI(BaseLLM):
"""Base OpenAI large language model class.
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:
Name of OpenAI model to use.
temperature:
Sampling temperature.
max_tokens:
Max number of tokens to generate.
top_p:
Total probability mass of tokens to consider at each step.
frequency_penalty:
Penalizes repeated tokens according to frequency.
presence_penalty:
Penalizes repeated tokens.
n:
How many completions to generate for each prompt.
best_of:
Generates best_of completions server-side and returns the "best".
logit_bias:
Adjust the probability of specific tokens being generated.
seed:
Seed for generation.
logprobs:
Include the log probabilities on the logprobs most likely output tokens.
streaming:
Whether to stream the results or not.
Key init args — client params:
openai_api_key:
OpenAI API key. If not passed in will be read from env var
`OPENAI_API_KEY`.
openai_api_base:
Base URL path for API requests, leave blank if not using a proxy or
service emulator.
openai_organization:
OpenAI organization ID. If not passed in will be read from env
var `OPENAI_ORG_ID`.
request_timeout:
Timeout for requests to OpenAI completion API.
max_retries:
Maximum number of retries to make when generating.
batch_size:
Batch size to use when passing multiple documents to generate.
See full list of supported init args and their descriptions in the params section.
Instantiate:
```python
from langchain_openai.llms.base import BaseOpenAI
model = BaseOpenAI(
model_name="gpt-3.5-turbo-instruct",
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
# openai_api_key="...",
# openai_api_base="...",
# openai_organization="...",
# other params...
)
```
Invoke:
```python
input_text = "The meaning of life is "
response = model.invoke(input_text)
print(response)
```
Extends
Source
Frequently Asked Questions
What is the BaseOpenAI class?
BaseOpenAI is a class in the langchain codebase, defined in libs/partners/openai/langchain_openai/llms/base.py.
Where is BaseOpenAI defined?
BaseOpenAI is defined in libs/partners/openai/langchain_openai/llms/base.py at line 53.
What does BaseOpenAI extend?
BaseOpenAI extends BaseLLM.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free