langchain.schema.language_model.BaseLanguageModel¶
- class langchain.schema.language_model.BaseLanguageModel[source]¶
Bases:
Serializable,Runnable[Union[PromptValue,str,List[BaseMessage]],LanguageModelOutput],ABCAbstract base class for interfacing with language models.
All language model wrappers inherit from BaseLanguageModel.
Exposes three main methods: - generate_prompt: generate language model outputs for a sequence of prompt
values. A prompt value is a model input that can be converted to any language model input format (string or messages).
- predict: pass in a single string to a language model and return a string
prediction.
- predict_messages: pass in a sequence of BaseMessages (corresponding to a single
model call) to a language model and return a BaseMessage prediction.
Each of these has an equivalent asynchronous method.
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.
- abstract async agenerate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Callbacks = None, **kwargs: Any) LLMResult[source]¶
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:
take advantage of batched calls,
need more output from the model than just the top generated value,
- are building chains that are agnostic to the underlying language model
type (e.g., pure text completion models vs chat models).
- Parameters
prompts – 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 – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
callbacks – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.
**kwargs – 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.
- abstract async apredict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str[source]¶
Asynchronously pass a string to the model and return a string prediction.
- Use this method when calling pure text generation models and only the top
candidate generation is needed.
- Parameters
text – String input to pass to the model.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a string.
- abstract async apredict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage[source]¶
Asynchronously pass messages to the model and return a message prediction.
- Use this method when calling chat models and only the top
candidate generation is needed.
- Parameters
messages – A sequence of chat messages corresponding to a single model input.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a message.
- abstract generate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Callbacks = None, **kwargs: Any) LLMResult[source]¶
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:
take advantage of batched calls,
need more output from the model than just the top generated value,
- are building chains that are agnostic to the underlying language model
type (e.g., pure text completion models vs chat models).
- Parameters
prompts – 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 – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
callbacks – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.
**kwargs – 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.
- get_num_tokens(text: str) int[source]¶
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 – The string input to tokenize.
- Returns
The integer number of tokens in the text.
- get_num_tokens_from_messages(messages: List[BaseMessage]) int[source]¶
Get the number of tokens in the messages.
Useful for checking if an input will fit in a model’s context window.
- Parameters
messages – The message inputs to tokenize.
- Returns
The sum of the number of tokens across the messages.
- get_token_ids(text: str) List[int][source]¶
Return the ordered ids of the tokens in a text.
- Parameters
text – The string input to tokenize.
- Returns
- A list of ids corresponding to the tokens in the text, in order they occur
in the text.
- abstract predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str[source]¶
Pass a single string input to the model and return a string prediction.
- Use this method when passing in raw text. If you want to pass in specific
types of chat messages, use predict_messages.
- Parameters
text – String input to pass to the model.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a string.
- abstract predict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage[source]¶
Pass a message sequence to the model and return a message prediction.
- Use this method when passing in chat messages. If you want to pass in raw text,
use predict.
- Parameters
messages – A sequence of chat messages corresponding to a single model input.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a message.
- to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
- to_json_not_implemented() SerializedNotImplemented¶
- property lc_attributes: Dict¶
Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.
- property lc_namespace: List[str]¶
Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]
- property lc_secrets: Dict[str, str]¶
Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}
- property lc_serializable: bool¶
Return whether or not the class is serializable.