langchain_openai.chat_models.base.BaseChatOpenAI

Note

BaseChatOpenAI implements the standard Runnable Interface. 🏃

class langchain_openai.chat_models.base.BaseChatOpenAI[source]

Bases: BaseChatModel

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param cache: Union[BaseCache, bool, None] = None

Whether to cache the response.

  • If true, will use the global cache.

  • If false, will not use a cache

  • If None, will use the global cache if it’s set, otherwise no cache.

  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

param callback_manager: Optional[BaseCallbackManager] = None

[DEPRECATED] Callback manager to add to the run trace.

param callbacks: Callbacks = None

Callbacks to add to the run trace.

param custom_get_token_ids: Optional[Callable[[str], List[int]]] = None

Optional encoder to use for counting tokens.

param default_headers: Union[Mapping[str, str], None] = None
param default_query: Union[Mapping[str, object], None] = None
param http_async_client: Union[Any, None] = None

Optional httpx.AsyncClient. Only used for async invocations. Must specify http_client as well if you’d like a custom client for sync invocations.

param http_client: Union[Any, None] = None

Optional httpx.Client. Only used for sync invocations. Must specify http_async_client as well if you’d like a custom client for async invocations.

param max_retries: int = 2

Maximum number of retries to make when generating.

param max_tokens: Optional[int] = None

Maximum number of tokens to generate.

param metadata: Optional[Dict[str, Any]] = None

Metadata to add to the run trace.

param model_kwargs: Dict[str, Any] [Optional]

Holds any model parameters valid for create call not explicitly specified.

param model_name: str = 'gpt-3.5-turbo' (alias 'model')

Model name to use.

param n: int = 1

Number of chat completions to generate for each prompt.

param openai_api_base: Optional[str] = None (alias 'base_url')

Base URL path for API requests, leave blank if not using a proxy or service emulator.

param openai_api_key: Optional[SecretStr] = None (alias 'api_key')

Automatically inferred from env var OPENAI_API_KEY if not provided.

Constraints
  • type = string

  • writeOnly = True

  • format = password

param openai_organization: Optional[str] = None (alias 'organization')

Automatically inferred from env var OPENAI_ORG_ID if not provided.

param openai_proxy: Optional[str] = None
param request_timeout: Union[float, Tuple[float, float], Any, None] = None (alias 'timeout')

Timeout for requests to OpenAI completion API. Can be float, httpx.Timeout or None.

param stop: Optional[Union[List[str], str]] = None (alias 'stop_sequences')

Default stop sequences.

param streaming: bool = False

Whether to stream the results or not.

param tags: Optional[List[str]] = None

Tags to add to the run trace.

param temperature: float = 0.7

What sampling temperature to use.

param tiktoken_model_name: Optional[str] = None

The model name to pass to tiktoken when using this class. Tiktoken is used to count the number of tokens in documents to constrain them to be under a certain limit. By default, when set to None, this will be the same as the embedding model name. However, there are some cases where you may want to use this Embedding class with a model name not supported by tiktoken. This can include when using Azure embeddings or when using one of the many model providers that expose an OpenAI-like API but with different models. In those cases, in order to avoid erroring when tiktoken is called, you can specify a model name to use here.

param verbose: bool [Optional]

Whether to print out response text.

__call__(messages: List[BaseMessage], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) BaseMessage

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use invoke instead.

Parameters
Return type

BaseMessage

async agenerate(messages: List[List[BaseMessage]], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, **kwargs: Any) LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. are building chains that are agnostic to the underlying language model

    type (e.g., pure text completion models vs chat models).

Parameters
  • messages (List[List[BaseMessage]]) – List of list of messages.

  • stop (Optional[List[str]]) – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

  • **kwargs (Any) – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • **kwargs

Returns

An LLMResult, which contains a list of candidate Generations for each input

prompt and additional model provider-specific output.

Return type

LLMResult

async agenerate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) LLMResult

Asynchronously pass a sequence of prompts and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. are building chains that are agnostic to the underlying language model

    type (e.g., pure text completion models vs chat models).

Parameters
  • prompts (List[PromptValue]) – List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models).

  • stop (Optional[List[str]]) – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

  • **kwargs (Any) – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

Returns

An LLMResult, which contains a list of candidate Generations for each input

prompt and additional model provider-specific output.

Return type

LLMResult

async apredict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use ainvoke instead.

Parameters
  • text (str) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

Return type

str

async apredict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use ainvoke instead.

Parameters
  • messages (List[BaseMessage]) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

Return type

BaseMessage

bind_functions(functions: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]], function_call: Optional[Union[_FunctionCall, str, Literal['auto', 'none']]] = None, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage][source]

Bind functions (and other objects) to this chat model.

Assumes model is compatible with OpenAI function-calling API.

NOTE: Using bind_tools is recommended instead, as the functions and

function_call request parameters are officially marked as deprecated by OpenAI.

Parameters
  • functions (Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]) – A list of function definitions to bind to this chat model. Can be a dictionary, pydantic model, or callable. Pydantic models and callables will be automatically converted to their schema dictionary representation.

  • function_call (Optional[Union[_FunctionCall, str, Literal['auto', 'none']]]) – Which function to require the model to call. Must be the name of the single provided function or “auto” to automatically determine which function to call (if any).

  • **kwargs (Any) – Any additional parameters to pass to the Runnable constructor.

Return type

Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]

bind_tools(tools: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]], *, tool_choice: Optional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]] = None, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage][source]

Bind tool-like objects to this chat model.

Assumes model is compatible with OpenAI tool-calling API.

Parameters
  • tools (Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]) – A list of tool definitions to bind to this chat model. Can be a dictionary, pydantic model, callable, or BaseTool. Pydantic models, callables, and BaseTools will be automatically converted to their schema dictionary representation.

  • tool_choice (Optional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]]) –

    Which tool to require the model to call. Options are: name of the tool (str): calls corresponding tool; “auto”: automatically selects a tool (including no tool); “none”: does not call a tool; “any” or “required”: force at least one tool to be called; True: forces tool call (requires tools be length 1); False: no effect;

    or a dict of the form: {“type”: “function”, “function”: {“name”: <<tool_name>>}}.

  • **kwargs (Any) – Any additional parameters to pass to the Runnable constructor.

Return type

Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]

call_as_llm(message: str, stop: Optional[List[str]] = None, **kwargs: Any) str

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use invoke instead.

Parameters
  • message (str) –

  • stop (Optional[List[str]]) –

  • kwargs (Any) –

Return type

str

generate(messages: List[List[BaseMessage]], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, **kwargs: Any) LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. are building chains that are agnostic to the underlying language model

    type (e.g., pure text completion models vs chat models).

Parameters
  • messages (List[List[BaseMessage]]) – List of list of messages.

  • stop (Optional[List[str]]) – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

  • **kwargs (Any) – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • **kwargs

Returns

An LLMResult, which contains a list of candidate Generations for each input

prompt and additional model provider-specific output.

Return type

LLMResult

generate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. are building chains that are agnostic to the underlying language model

    type (e.g., pure text completion models vs chat models).

Parameters
  • prompts (List[PromptValue]) – List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models).

  • stop (Optional[List[str]]) – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

  • **kwargs (Any) – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

Returns

An LLMResult, which contains a list of candidate Generations for each input

prompt and additional model provider-specific output.

Return type

LLMResult

get_num_tokens(text: str) int

Get the number of tokens present in the text.

Useful for checking if an input will fit in a model’s context window.

Parameters

text (str) – The string input to tokenize.

Returns

The integer number of tokens in the text.

Return type

int

get_num_tokens_from_messages(messages: List[BaseMessage]) int[source]

Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.

Requirements: You must have the pillow installed if you want to count image tokens if you are specifying the image as a base64 string, and you must have both pillow and httpx installed if you are specifying the image as a URL. If these aren’t installed image inputs will be ignored in token counting.

OpenAI reference: https://github.com/openai/openai-cookbook/blob/ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb

Parameters

messages (List[BaseMessage]) –

Return type

int

get_token_ids(text: str) List[int][source]

Get the tokens present in the text with tiktoken package.

Parameters

text (str) –

Return type

List[int]

predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use invoke instead.

Parameters
  • text (str) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

Return type

str

predict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage

[Deprecated]

Notes

Deprecated since version langchain-core==0.1.7: Use invoke instead.

Parameters
  • messages (List[BaseMessage]) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

Return type

BaseMessage

with_structured_output(schema: Optional[Union[Dict[str, Any], Type[_BM]]] = None, *, method: Literal['function_calling', 'json_mode'] = 'function_calling', include_raw: Literal[True] = True, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], _AllReturnType][source]
with_structured_output(schema: Optional[Union[Dict[str, Any], Type[_BM]]] = None, *, method: Literal['function_calling', 'json_mode'] = 'function_calling', include_raw: Literal[False] = False, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, _BM]]

Model wrapper that returns outputs formatted to match the given schema.

Args:
schema: The output schema as a dict or a Pydantic class. If a Pydantic class

then the model output will be an object of that class. If a dict then the model output will be a dict. With a Pydantic class the returned attributes will be validated, whereas with a dict they will not be. If method is “function_calling” and schema is a dict, then the dict must match the OpenAI function-calling spec or be a valid JSON schema with top level ‘title’ and ‘description’ keys specified.

method: The method for steering model generation, either “function_calling”

or “json_mode”. If “function_calling” then the schema will be converted to an OpenAI function and the returned model will make use of the function-calling API. If “json_mode” then OpenAI’s JSON mode will be used. Note that if using “json_mode” then you must include instructions for formatting the output into the desired schema into the model call.

include_raw: If False then only the parsed structured output is returned. If

an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys “raw”, “parsed”, and “parsing_error”.

Returns:

A Runnable that takes any ChatModel input and returns as output:

If include_raw is True then a dict with keys:

raw: BaseMessage parsed: Optional[_DictOrPydantic] parsing_error: Optional[BaseException]

If include_raw is False then just _DictOrPydantic is returned, where _DictOrPydantic depends on the schema:

If schema is a Pydantic class then _DictOrPydantic is the Pydantic

class.

If schema is a dict then _DictOrPydantic is a dict.

Example: Function-calling, Pydantic schema (method=”function_calling”, include_raw=False):
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Example: Function-calling, Pydantic schema (method=”function_calling”, include_raw=True):
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Example: Function-calling, dict schema (method=”function_calling”, include_raw=False):
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }
Example: JSON mode, Pydantic schema (method=”json_mode”, include_raw=True):
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel

class AnswerWithJustification(BaseModel):
    answer: str
    justification: str

llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification,
    method="json_mode",
    include_raw=True
)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'.

“What’s heavier a pound of bricks or a pound of feathers?”

) # -> { # ‘raw’: AIMessage(content=’{

“answer”: “They are both the same weight.”, “justification”: “Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.”

}’),

# ‘parsed’: AnswerWithJustification(answer=’They are both the same weight.’, justification=’Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.’), # ‘parsing_error’: None # }

Example: JSON mode, no schema (schema=None, method=”json_mode”, include_raw=True):
from langchain_openai import ChatOpenAI

structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'.

“What’s heavier a pound of bricks or a pound of feathers?”

) # -> { # ‘raw’: AIMessage(content=’{

“answer”: “They are both the same weight.”, “justification”: “Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.”

}’),

# ‘parsed’: { # ‘answer’: ‘They are both the same weight.’, # ‘justification’: ‘Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.’ # }, # ‘parsing_error’: None # }