Core language model abstractions.
LangChain has two main classes to work with language models: chat models and "old-fashioned" LLMs (string-in, string-out).
Chat models
Language models that use a sequence of messages as inputs and return chat messages as outputs (as opposed to using plain text).
Chat models support the assignment of distinct roles to conversation messages, helping to distinguish messages from the AI, users, and instructions such as system messages.
The key abstraction for chat models is
BaseChatModel. Implementations should
inherit from this class.
See existing chat model integrations.
LLMs (legacy)
Language models that takes a string as input and returns a string.
These are traditionally older models (newer models generally are chat models).
Although the underlying models are string in, string out, the LangChain wrappers also allow these models to take messages as input. This gives them the same interface as chat models. When messages are passed in as input, they will be formatted into a string under the hood before being passed to the underlying model.
Import an attribute from a module located in a package.
This utility function is used in custom __getattr__ methods within __init__.py
files to dynamically import attributes.
Check whether a block contains multimodal data in OpenAI Chat Completions format.
Supports both data and ID-style blocks (e.g. 'file_data' and 'file_id')
If additional keys are present, they are ignored / will not affect outcome as long as the required keys are present and valid.
Get a GPT-2 tokenizer instance.
This function is cached to avoid re-loading the tokenizer every time it is called.
Abstract base class for interfacing with language models.
All language model wrappers inherited from BaseLanguageModel.
LangSmith parameters for tracing.
Base class for chat models.
Simplified implementation for a chat model to inherit from.
This implementation is primarily here for backwards compatibility. For new
implementations, please use BaseChatModel directly.
Fake LLM for testing purposes.
Fake streaming list LLM for testing purposes.
An LLM that will return responses from a list in order.
This model also supports optionally sleeping between successive chunks in a streaming implementation.
Fake chat model for testing purposes.
Fake chat model for testing purposes.
Generic fake chat model that can be used to test the chat model interface.
on_llm_new_token to allow for testing of callback related code for new
tokens.Generic fake chat model that can be used to test the chat model interface.
Simple interface for implementing a custom LLM.
You should subclass this class and implement the following:
_call method: Run the LLM on the given prompt and input (used by invoke)._identifying_params property: Return a dictionary of the identifying parameters
This is critical for caching and tracing purposes. Identifying parameters
is a dict that identifies the LLM.
It should mostly include a model_name.Optional: Override the following methods to provide more optimizations:
_acall: Provide a native async version of the _call method.
If not provided, will delegate to the synchronous version using
run_in_executor. (Used by ainvoke)._stream: Stream the LLM on the given prompt and input.
stream will use _stream if provided, otherwise it
use _call and output will arrive in one chunk._astream: Override to provide a native async version of the _stream method.
astream will use _astream if provided, otherwise it will implement
a fallback behavior that will use _stream if _stream is implemented,
and use _acall if _stream is not implemented.Base LLM abstract interface.
It should take in a prompt and return a string.
Model profile.
This is a beta feature. The format of model profiles is subject to change.
Provides information about chat model capabilities, such as context window sizes and supported features.
Fake chat models for testing purposes.
Base interface for traditional large language models (LLMs) to expose.
These are traditionally older models (newer models generally are chat models).
Fake LLMs for testing purposes.
Chat models for conversational AI.
Model profile types and utilities.
Base language models class.