instance and remote OpenLLM servers.
To use, you should have the openllm library installed:
pip install openllm
Learn more at: https://github.com/bentoml/openllm
Example running an LLM model locally managed by OpenLLM:from langchain.llms import OpenLLM
llm = OpenLLM(
    model_name='flan-t5',
    model_id='google/flan-t5-large',
)
llm("What is the difference between a duck and a goose?")
For all available supported models, you can run ‘openllm models’.
If you have a OpenLLM server running, you can also use it remotely:from langchain.llms import OpenLLM
llm = OpenLLM(server_url='http://localhost:3000')
llm("What is the difference between a duck and a goose?")
Parameters


Parameters
model_name (Optional[str]) –
model_id (Optional[str]) –
server_url (Optional[str]) –
server_type (Literal['grpc', 'http']) –
embedded (bool) –
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
llm_kwargs (Dict[str, Any]) –
Return type
None
attribute embedded: bool = True
Initialize this LLM instance in current process by default. Should
only set to False when using in conjunction with BentoML Service.
attribute llm_kwargs: Dict[str, Any] [Required]
Key word arguments to be passed to openllm.LLM


attribute model_id: Optional[str] = None
Model Id to use. If not provided, will use the default model for the model name.
See ‘openllm models’ for all available model variants.
attribute model_name: Optional[str] = None
Model name to use. See ‘openllm models’ for all available models.
attribute server_type: ServerType = 'http'
Optional server type. Either ‘http’ or ‘grpc’.
attribute server_url: Optional[str] = None
Optional server URL that currently runs a LLMServer with ‘openllm start’.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)


Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)


Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage


Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include


update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type


kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)


int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –


exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:


Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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


property lc_serializable: bool
Return whether or not the class is serializable.
property runner: openllm.LLMRunner
Get the underlying openllm.LLMRunner instance for integration with BentoML.
Example:
.. code-block:: python
llm = OpenLLM(model_name=’flan-t5’,
model_id=’google/flan-t5-large’,
embedded=False,
)
tools = load_tools([“serpapi”, “llm-math”], llm=llm)
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)
svc = bentoml.Service(“langchain-openllm”, runners=[llm.runner])
@svc.api(input=Text(), output=Text())
def chat(input_text: str):
return agent.run(input_text)


return agent.run(input_text)
class langchain.llms.OpenLM(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model='text-davinci-003', temperature=0.7, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0, n=1, best_of=1, model_kwargs=None, openai_api_key=None, openai_api_base=None, openai_organization=None, openai_proxy=None, batch_size=20, request_timeout=None, logit_bias=None, max_retries=6, streaming=False, allowed_special={}, disallowed_special='all', tiktoken_model_name=None)[source]
Bases: langchain.llms.openai.BaseOpenAI
Parameters
cache (Optional[bool]) –
verbose (bool) –


verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
client (Any) –
model (str) –
temperature (float) –
max_tokens (int) –
top_p (float) –
frequency_penalty (float) –
presence_penalty (float) –
n (int) –
best_of (int) –
model_kwargs (Dict[str, Any]) –
openai_api_key (Optional[str]) –
openai_api_base (Optional[str]) –
openai_organization (Optional[str]) –
openai_proxy (Optional[str]) –
batch_size (int) –


batch_size (int) –
request_timeout (Optional[Union[float, Tuple[float, float]]]) –
logit_bias (Optional[Dict[str, float]]) –
max_retries (int) –
streaming (bool) –
allowed_special (Union[Literal['all'], typing.AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], typing.Collection[str]]) –
tiktoken_model_name (Optional[str]) –
Return type
None
attribute allowed_special: Union[Literal['all'], AbstractSet[str]] = {}
Set of special tokens that are allowed。
attribute batch_size: int = 20
Batch size to use when passing multiple documents to generate.
attribute best_of: int = 1
Generates best_of completions server-side and returns the “best”.


attribute disallowed_special: Union[Literal['all'], Collection[str]] = 'all'
Set of special tokens that are not allowed。
attribute frequency_penalty: float = 0
Penalizes repeated tokens according to frequency.
attribute logit_bias: Optional[Dict[str, float]] [Optional]
Adjust the probability of specific tokens being generated.
attribute max_retries: int = 6
Maximum number of retries to make when generating.
attribute max_tokens: int = 256
The maximum number of tokens to generate in the completion.
-1 returns as many tokens as possible given the prompt and
the models maximal context size.
attribute model_kwargs: Dict[str, Any] [Optional]
Holds any model parameters valid for create call not explicitly specified.
attribute model_name: str = 'text-davinci-003' (alias 'model')


Model name to use.
attribute n: int = 1
How many completions to generate for each prompt.
attribute presence_penalty: float = 0
Penalizes repeated tokens.
attribute request_timeout: Optional[Union[float, Tuple[float, float]]] = None
Timeout for requests to OpenAI completion API. Default is 600 seconds.
attribute streaming: bool = False
Whether to stream the results or not.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute temperature: float = 0.7
What sampling temperature to use.
attribute 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.
attribute top_p: float = 1
Total probability mass of tokens to consider at each step.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –


Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters


Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)


Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating


the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
create_llm_result(choices, prompts, token_usage)
Create the LLMResult from the choices and prompts.
Parameters
choices (Any) –
prompts (List[str]) –
token_usage (Dict[str, int]) –
Return type
langchain.schema.LLMResult
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int


Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_sub_prompts(params, prompts, stop=None)
Get the sub prompts for llm call.
Parameters
params (Dict[str, Any]) –
prompts (List[str]) –
stop (Optional[List[str]]) –
Return type
List[List[str]]
get_token_ids(text)
Get the token IDs using the tiktoken package.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)


Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
max_tokens_for_prompt(prompt)
Calculate the maximum number of tokens possible to generate for a prompt.
Parameters
prompt (str) – The prompt to pass into the model.
Returns


Returns
The maximum number of tokens to generate for a prompt.
Return type
int
Example
max_tokens = openai.max_token_for_prompt("Tell me a joke.")
static modelname_to_contextsize(modelname)
Calculate the maximum number of tokens possible to generate for a model.
Parameters
modelname (str) – The modelname we want to know the context size for.
Returns
The maximum context size
Return type
int
Example
max_tokens = openai.modelname_to_contextsize("text-davinci-003")
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters


Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
prep_streaming_params(stop=None)
Prepare the params for streaming.
Parameters
stop (Optional[List[str]]) –
Return type
Dict[str, Any]
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
stream(prompt, stop=None)
Call OpenAI with streaming flag and return the resulting generator.
BETA: this is a beta feature while we figure out the right abstraction.
Once that happens, this interface could change.
Parameters


Parameters
prompt (str) – The prompts to pass into the model.
stop (Optional[List[str]]) – Optional list of stop words to use when generating.
Returns
A generator representing the stream of tokens from OpenAI.
Return type
Generator
Example
generator = openai.stream("Tell me a joke.")
for token in generator:
    yield token
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.
property max_context_size: int
Get max context size for this model.
class langchain.llms.Petals(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, tokenizer=None, model_name='bigscience/bloom-petals', temperature=0.7, max_new_tokens=256, top_p=0.9, top_k=None, do_sample=True, max_length=None, model_kwargs=None, huggingface_api_key=None)[source]
Bases: langchain.llms.base.LLM
Wrapper around Petals Bloom models.


Wrapper around Petals Bloom models.
To use, you should have the petals python package installed, and the
environment variable HUGGINGFACE_API_KEY set with your API key.
Any parameters that are valid to be passed to the call can be passed
in, even if not explicitly saved on this class.
Example
from langchain.llms import petals
petals = Petals()
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
client (Any) –
tokenizer (Any) –
model_name (str) –
temperature (float) –
max_new_tokens (int) –
top_p (float) –


top_p (float) –
top_k (Optional[int]) –
do_sample (bool) –
max_length (Optional[int]) –
model_kwargs (Dict[str, Any]) –
huggingface_api_key (Optional[str]) –
Return type
None
attribute client: Any = None
The client to use for the API calls.
attribute do_sample: bool = True
Whether or not to use sampling; use greedy decoding otherwise.
attribute max_length: Optional[int] = None
The maximum length of the sequence to be generated.
attribute max_new_tokens: int = 256
The maximum number of new tokens to generate in the completion.
attribute model_kwargs: Dict[str, Any] [Optional]
Holds any model parameters valid for create call
not explicitly specified.
attribute model_name: str = 'bigscience/bloom-petals'


The model to use.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute temperature: float = 0.7
What sampling temperature to use
attribute tokenizer: Any = None
The tokenizer to use for the API calls.
attribute top_k: Optional[int] = None
The number of highest probability vocabulary tokens
to keep for top-k-filtering.
attribute top_p: float = 0.9
The cumulative probability for top-p sampling.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters


the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)


Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]


Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –


models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)


classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.


class langchain.llms.PipelineAI(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_key='', pipeline_kwargs=None, pipeline_api_key=None)[source]
Bases: langchain.llms.base.LLM, pydantic.main.BaseModel
Wrapper around PipelineAI large language models.
To use, you should have the pipeline-ai python package installed,
and the environment variable PIPELINE_API_KEY set with your API key.
Any parameters that are valid to be passed to the call can be passed
in, even if not explicitly saved on this class.
Example
from langchain import PipelineAI
pipeline = PipelineAI(pipeline_key="")
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –


callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
pipeline_key (str) –
pipeline_kwargs (Dict[str, Any]) –
pipeline_api_key (Optional[str]) –
Return type
None
attribute pipeline_key: str = ''
The id or tag of the target pipeline
attribute pipeline_kwargs: Dict[str, Any] [Optional]
Holds any pipeline parameters valid for create call not
explicitly specified.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –


Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)


Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating


class langchain.llms.PredictionGuard(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model='MPT-7B-Instruct', output=None, max_tokens=256, temperature=0.75, token=None, stop=None)[source]
Bases: langchain.llms.base.LLM
Wrapper around Prediction Guard large language models.
To use, you should have the predictionguard python package installed, and the
environment variable PREDICTIONGUARD_TOKEN set with your access token, or pass
it as a named parameter to the constructor. To use Prediction Guard’s API along
with OpenAI models, set the environment variable OPENAI_API_KEY with your
OpenAI API key as well.
Example
pgllm = PredictionGuard(model="MPT-7B-Instruct",
                        token="my-access-token",
                        output={
                            "type": "boolean"


"type": "boolean"
                        })
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
client (Any) –
model (Optional[str]) –
output (Optional[Dict[str, Any]]) –
max_tokens (int) –
temperature (float) –
token (Optional[str]) –
stop (Optional[List[str]]) –
Return type
None
attribute max_tokens: int = 256
Denotes the number of tokens to predict per generation.
attribute model: Optional[str] = 'MPT-7B-Instruct'
Model name to use.


Model name to use.
attribute output: Optional[Dict[str, Any]] = None
The output type or structure for controlling the LLM output.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute temperature: float = 0.75
A non-negative float that tunes the degree of randomness in generation.
attribute token: Optional[str] = None
Your Prediction Guard access token.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –


kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)


class langchain.llms.PromptLayerOpenAI(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model='text-davinci-003', temperature=0.7, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0, n=1, best_of=1, model_kwargs=None, openai_api_key=None, openai_api_base=None, openai_organization=None, openai_proxy=None, batch_size=20, request_timeout=None, logit_bias=None, max_retries=6, streaming=False, allowed_special={}, disallowed_special='all', tiktoken_model_name=None, pl_tags=None, return_pl_id=False)[source]
Bases: langchain.llms.openai.OpenAI
Wrapper around OpenAI large language models.
To use, you should have the openai and promptlayer python
package installed, and the environment variable OPENAI_API_KEY


and PROMPTLAYER_API_KEY set with your openAI API key and
promptlayer key respectively.
All parameters that can be passed to the OpenAI LLM can also
be passed here. The PromptLayerOpenAI LLM adds two optional
Parameters
pl_tags (Optional[List[str]]) – List of strings to tag the request with.
return_pl_id (Optional[bool]) – If True, the PromptLayer request ID will be
returned in the generation_info field of the
Generation object.
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
client (Any) –
model (str) –
temperature (float) –


temperature (float) –
max_tokens (int) –
top_p (float) –
frequency_penalty (float) –
presence_penalty (float) –
n (int) –
best_of (int) –
model_kwargs (Dict[str, Any]) –
openai_api_key (Optional[str]) –
openai_api_base (Optional[str]) –
openai_organization (Optional[str]) –
openai_proxy (Optional[str]) –
batch_size (int) –
request_timeout (Optional[Union[float, Tuple[float, float]]]) –
logit_bias (Optional[Dict[str, float]]) –
max_retries (int) –
streaming (bool) –
allowed_special (Union[Literal['all'], typing.AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], typing.Collection[str]]) –


tiktoken_model_name (Optional[str]) –
Return type
None
Example
from langchain.llms import PromptLayerOpenAI
openai = PromptLayerOpenAI(model_name="text-davinci-003")
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –


Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model


Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
create_llm_result(choices, prompts, token_usage)
Create the LLMResult from the choices and prompts.
Parameters


Parameters
choices (Any) –
prompts (List[str]) –
token_usage (Dict[str, int]) –
Return type
langchain.schema.LLMResult
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)


Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_sub_prompts(params, prompts, stop=None)
Get the sub prompts for llm call.
Parameters
params (Dict[str, Any]) –


params (Dict[str, Any]) –
prompts (List[str]) –
stop (Optional[List[str]]) –
Return type
List[List[str]]
get_token_ids(text)
Get the token IDs using the tiktoken package.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –


by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
max_tokens_for_prompt(prompt)
Calculate the maximum number of tokens possible to generate for a prompt.
Parameters
prompt (str) – The prompt to pass into the model.
Returns
The maximum number of tokens to generate for a prompt.
Return type
int
Example
max_tokens = openai.max_token_for_prompt("Tell me a joke.")
static modelname_to_contextsize(modelname)
Calculate the maximum number of tokens possible to generate for a model.
Parameters


Parameters
modelname (str) – The modelname we want to know the context size for.
Returns
The maximum context size
Return type
int
Example
max_tokens = openai.modelname_to_contextsize("text-davinci-003")
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
prep_streaming_params(stop=None)
Prepare the params for streaming.
Parameters
stop (Optional[List[str]]) –
Return type


Return type
Dict[str, Any]
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
stream(prompt, stop=None)
Call OpenAI with streaming flag and return the resulting generator.
BETA: this is a beta feature while we figure out the right abstraction.
Once that happens, this interface could change.
Parameters
prompt (str) – The prompts to pass into the model.
stop (Optional[List[str]]) – Optional list of stop words to use when generating.
Returns
A generator representing the stream of tokens from OpenAI.
Return type
Generator
Example
generator = openai.stream("Tell me a joke.")
for token in generator:
    yield token


for token in generator:
    yield token
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.
property max_context_size: int


property max_context_size: int
Get max context size for this model.
class langchain.llms.PromptLayerOpenAIChat(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model_name='gpt-3.5-turbo', model_kwargs=None, openai_api_key=None, openai_api_base=None, openai_proxy=None, max_retries=6, prefix_messages=None, streaming=False, allowed_special={}, disallowed_special='all', pl_tags=None, return_pl_id=False)[source]
Bases: langchain.llms.openai.OpenAIChat
Wrapper around OpenAI large language models.
To use, you should have the openai and promptlayer python
package installed, and the environment variable OPENAI_API_KEY
and PROMPTLAYER_API_KEY set with your openAI API key and
promptlayer key respectively.


promptlayer key respectively.
All parameters that can be passed to the OpenAIChat LLM can also
be passed here. The PromptLayerOpenAIChat adds two optional
Parameters
pl_tags (Optional[List[str]]) – List of strings to tag the request with.
return_pl_id (Optional[bool]) – If True, the PromptLayer request ID will be
returned in the generation_info field of the
Generation object.
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
client (Any) –
model_name (str) –
model_kwargs (Dict[str, Any]) –
openai_api_key (Optional[str]) –


openai_api_base (Optional[str]) –
openai_proxy (Optional[str]) –
max_retries (int) –
prefix_messages (List) –
streaming (bool) –
allowed_special (Union[Literal['all'], typing.AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], typing.Collection[str]]) –
Return type
None
Example
from langchain.llms import PromptLayerOpenAIChat
openaichat = PromptLayerOpenAIChat(model_name="gpt-3.5-turbo")
attribute allowed_special: Union[Literal['all'], AbstractSet[str]] = {}
Set of special tokens that are allowed。
attribute disallowed_special: Union[Literal['all'], Collection[str]] = 'all'
Set of special tokens that are not allowed。
attribute max_retries: int = 6


Maximum number of retries to make when generating.
attribute model_kwargs: Dict[str, Any] [Optional]
Holds any model parameters valid for create call not explicitly specified.
attribute model_name: str = 'gpt-3.5-turbo'
Model name to use.
attribute prefix_messages: List [Optional]
Series of messages for Chat input.
attribute streaming: bool = False
Whether to stream the results or not.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str


Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –


kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values


Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)


Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token IDs using the tiktoken package.
Parameters
text (str) –
Return type
List[int]


Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –


models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)


classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.


class langchain.llms.RWKV(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, model, tokens_path, strategy='cpu fp32', rwkv_verbose=True, temperature=1.0, top_p=0.5, penalty_alpha_frequency=0.4, penalty_alpha_presence=0.4, CHUNK_LEN=256, max_tokens_per_generation=256, client=None, tokenizer=None, pipeline=None, model_tokens=None, model_state=None)[source]
Bases: langchain.llms.base.LLM, pydantic.main.BaseModel
Wrapper around RWKV language models.
To use, you should have the rwkv python package installed, the
pre-trained model file, and the model’s config information.
Example
from langchain.llms import RWKV
model = RWKV(model="./models/rwkv-3b-fp16.bin", strategy="cpu fp32")
# Simplest invocation


# Simplest invocation
response = model("Once upon a time, ")
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
model (str) –
tokens_path (str) –
strategy (str) –
rwkv_verbose (bool) –
temperature (float) –
top_p (float) –
penalty_alpha_frequency (float) –
penalty_alpha_presence (float) –
CHUNK_LEN (int) –
max_tokens_per_generation (int) –
client (Any) –
tokenizer (Any) –
pipeline (Any) –
model_tokens (Any) –


model_tokens (Any) –
model_state (Any) –
Return type
None
attribute CHUNK_LEN: int = 256
Batch size for prompt processing.
attribute max_tokens_per_generation: int = 256
Maximum number of tokens to generate.
attribute model: str [Required]
Path to the pre-trained RWKV model file.
attribute penalty_alpha_frequency: float = 0.4
Positive values penalize new tokens based on their existing frequency
in the text so far, decreasing the model’s likelihood to repeat the same
line verbatim..
attribute penalty_alpha_presence: float = 0.4
Positive values penalize new tokens based on whether they appear
in the text so far, increasing the model’s likelihood to talk about
new topics..
attribute rwkv_verbose: bool = True
Print debug information.


Print debug information.
attribute strategy: str = 'cpu fp32'
Token context window.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute temperature: float = 1.0
The temperature to use for sampling.
attribute tokens_path: str [Required]
Path to the RWKV tokens file.
attribute top_p: float = 0.5
The top-p value to use for sampling.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters


Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)


Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating


the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)


Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]


class langchain.llms.Replicate(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, model, input=None, model_kwargs=None, replicate_api_token=None)[source]
Bases: langchain.llms.base.LLM
Wrapper around Replicate models.
To use, you should have the replicate python package installed,
and the environment variable REPLICATE_API_TOKEN set with your API token.
You can find your token here: https://replicate.com/account
The model param is required, but any other model parameters can also
be passed in with the format input={model_param: value, …}
Example
from langchain.llms import Replicate


from langchain.llms import Replicate
replicate = Replicate(model="stability-ai/stable-diffusion:                                          27b93a2413e7f36cd83da926f365628                                         0b2931564ff050bf9575f1fdf9bcd7478",
                      input={"image_dimensions": "512x512"})
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
model (str) –
input (Dict[str, Any]) –
model_kwargs (Dict[str, Any]) –
replicate_api_token (Optional[str]) –
Return type
None


Return type
None
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –


Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model


Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type


kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –


kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().


encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str


Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.
class langchain.llms.SagemakerEndpoint(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, endpoint_name='', region_name='', credentials_profile_name=None, content_handler, model_kwargs=None, endpoint_kwargs=None)[source]
Bases: langchain.llms.base.LLM
Wrapper around custom Sagemaker Inference Endpoints.


To use, you must supply the endpoint name from your deployed
Sagemaker model & the region where it is deployed.
To authenticate, the AWS client uses the following methods to
automatically load credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
If a specific credential profile should be used, you must pass
the name of the profile from the ~/.aws/credentials file that is to be used.
Make sure the credentials / roles used have the required policies to
access the Sagemaker endpoint.
See: https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –


tags (Optional[List[str]]) –
client (Any) –
endpoint_name (str) –
region_name (str) –
credentials_profile_name (Optional[str]) –
content_handler (langchain.llms.sagemaker_endpoint.LLMContentHandler) –
model_kwargs (Optional[Dict]) –
endpoint_kwargs (Optional[Dict]) –
Return type
None
attribute content_handler: langchain.llms.sagemaker_endpoint.LLMContentHandler [Required]
The content handler class that provides an input and
output transform functions to handle formats between LLM
and the endpoint.
attribute credentials_profile_name: Optional[str] = None
The name of the profile in the ~/.aws/credentials or ~/.aws/config files, which
has either access keys or role information specified.
If not specified, the default credential profile or, if on an EC2 instance,


credentials from IMDS will be used.
See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
attribute endpoint_kwargs: Optional[Dict] = None
Optional attributes passed to the invoke_endpoint
function. See `boto3`_. docs for more info.
.. _boto3: <https://boto3.amazonaws.com/v1/documentation/api/latest/index.html>
attribute endpoint_name: str = ''
The name of the endpoint from the deployed Sagemaker model.
Must be unique within an AWS Region.
attribute model_kwargs: Optional[Dict] = None
Key word arguments to pass to the model.
attribute region_name: str = ''
The aws region where the Sagemaker model is deployed, eg. us-west-2.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.


Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –


tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)


Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model


exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int


Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –


exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –


kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.
class langchain.llms.SelfHostedHuggingFaceLLM(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_ref=None, client=None, inference_fn=<function _generate_text>, hardware=None, model_load_fn=<function _load_transformer>, load_fn_kwargs=None, model_reqs=['./', 'transformers', 'torch'], model_id='gpt2', task='text-generation', device=0, model_kwargs=None)[source]
Bases: langchain.llms.self_hosted.SelfHostedPipeline
Wrapper around HuggingFace Pipeline API to run on self-hosted remote hardware.


Supported hardware includes auto-launched instances on AWS, GCP, Azure,
and Lambda, as well as servers specified
by IP address and SSH credentials (such as on-prem, or another cloud
like Paperspace, Coreweave, etc.).
To use, you should have the runhouse python package installed.
Only supports text-generation, text2text-generation and summarization for now.
Example using from_model_id:from langchain.llms import SelfHostedHuggingFaceLLM
import runhouse as rh
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
hf = SelfHostedHuggingFaceLLM(
    model_id="google/flan-t5-large", task="text2text-generation",
    hardware=gpu
)
Example passing fn that generates a pipeline (bc the pipeline is not serializable):from langchain.llms import SelfHostedHuggingFaceLLM


from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import runhouse as rh
def get_pipeline():
    model_id = "gpt2"
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(model_id)
    pipe = pipeline(
        "text-generation", model=model, tokenizer=tokenizer
    )
    return pipe
hf = SelfHostedHuggingFaceLLM(
    model_load_fn=get_pipeline, model_id="gpt2", hardware=gpu)
Parameters
cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
pipeline_ref (Any) –


pipeline_ref (Any) –
client (Any) –
inference_fn (Callable) –
hardware (Any) –
model_load_fn (Callable) –
load_fn_kwargs (Optional[dict]) –
model_reqs (List[str]) –
model_id (str) –
task (str) –
device (int) –
model_kwargs (Optional[dict]) –
Return type
None
attribute device: int = 0
Device to use for inference. -1 for CPU, 0 for GPU, 1 for second GPU, etc.
attribute hardware: Any = None
Remote hardware to send the inference function to.
attribute inference_fn: Callable = <function _generate_text>
Inference function to send to the remote hardware.
attribute load_fn_kwargs: Optional[dict] = None


Key word arguments to pass to the model load function.
attribute model_id: str = 'gpt2'
Hugging Face model_id to load the model.
attribute model_kwargs: Optional[dict] = None
Key word arguments to pass to the model.
attribute model_load_fn: Callable = <function _load_transformer>
Function to load the model remotely on the server.
attribute model_reqs: List[str] = ['./', 'transformers', 'torch']
Requirements to install on hardware to inference the model.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute task: str = 'text-generation'
Hugging Face task (“text-generation”, “text2text-generation” or
“summarization”).
attribute verbose: bool [Optional]
Whether to print out response text.


Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type


kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters


exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
classmethod from_pipeline(pipeline, hardware, model_reqs=None, device=0, **kwargs)
Init the SelfHostedPipeline from a pipeline object or string.
Parameters
pipeline (Any) –
hardware (Any) –
model_reqs (Optional[List[str]]) –


device (int) –
kwargs (Any) –
Return type
langchain.llms.base.LLM
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]


Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –


models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)


classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.


class langchain.llms.SelfHostedPipeline(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_ref=None, client=None, inference_fn=<function _generate_text>, hardware=None, model_load_fn, load_fn_kwargs=None, model_reqs=['./', 'torch'])[source]
Bases: langchain.llms.base.LLM
Run model inference on self-hosted remote hardware.
Supported hardware includes auto-launched instances on AWS, GCP, Azure,
and Lambda, as well as servers specified
by IP address and SSH credentials (such as on-prem, or another
cloud like Paperspace, Coreweave, etc.).
To use, you should have the runhouse python package installed.
Example for custom pipeline and inference functions:from langchain.llms import SelfHostedPipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import runhouse as rh


import runhouse as rh
def load_pipeline():
    tokenizer = AutoTokenizer.from_pretrained("gpt2")
    model = AutoModelForCausalLM.from_pretrained("gpt2")
    return pipeline(
        "text-generation", model=model, tokenizer=tokenizer,
        max_new_tokens=10
    )
def inference_fn(pipeline, prompt, stop = None):
    return pipeline(prompt)[0]["generated_text"]
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
llm = SelfHostedPipeline(
    model_load_fn=load_pipeline,
    hardware=gpu,
    model_reqs=model_reqs, inference_fn=inference_fn
)
Example for <2GB model (can be serialized and sent directly to the server):from langchain.llms import SelfHostedPipeline
import runhouse as rh


import runhouse as rh
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
my_model = ...
llm = SelfHostedPipeline.from_pipeline(
    pipeline=my_model,
    hardware=gpu,
    model_reqs=["./", "torch", "transformers"],
)
Example passing model path for larger models:from langchain.llms import SelfHostedPipeline
import runhouse as rh
import pickle
from transformers import pipeline
generator = pipeline(model="gpt2")
rh.blob(pickle.dumps(generator), path="models/pipeline.pkl"
    ).save().to(gpu, path="models")
llm = SelfHostedPipeline.from_pipeline(
    pipeline="models/pipeline.pkl",
    hardware=gpu,
    model_reqs=["./", "torch", "transformers"],
)
Parameters
cache (Optional[bool]) –


cache (Optional[bool]) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
pipeline_ref (Any) –
client (Any) –
inference_fn (Callable) –
hardware (Any) –
model_load_fn (Callable) –
load_fn_kwargs (Optional[dict]) –
model_reqs (List[str]) –
Return type
None
attribute hardware: Any = None
Remote hardware to send the inference function to.
attribute inference_fn: Callable = <function _generate_text>
Inference function to send to the remote hardware.
attribute load_fn_kwargs: Optional[dict] = None


Key word arguments to pass to the model load function.
attribute model_load_fn: Callable [Required]
Function to load the model remotely on the server.
attribute model_reqs: List[str] = ['./', 'torch']
Requirements to install on hardware to inference the model.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)
Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str


Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –


kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values


Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)


Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
classmethod from_pipeline(pipeline, hardware, model_reqs=None, device=0, **kwargs)[source]
Init the SelfHostedPipeline from a pipeline object or string.
Parameters
pipeline (Any) –
hardware (Any) –
model_reqs (Optional[List[str]]) –
device (int) –
kwargs (Any) –
Return type
langchain.llms.base.LLM
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –


stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int


Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –


exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –


kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example:
.. code-block:: python
llm.save(file_path=”path/llm.yaml”)
classmethod update_forward_refs(**localns)
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Parameters
localns (Any) –
Return type
None
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.
class langchain.llms.StochasticAI(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, api_url='', model_kwargs=None, stochasticai_api_key=None)[source]
Bases: langchain.llms.base.LLM
Wrapper around StochasticAI large language models.
To use, you should have the environment variable STOCHASTICAI_API_KEY
set with your API key.
Example
from langchain.llms import StochasticAI
stochasticai = StochasticAI(api_url="")
Parameters
cache (Optional[bool]) –
verbose (bool) –


verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
tags (Optional[List[str]]) –
api_url (str) –
model_kwargs (Dict[str, Any]) –
stochasticai_api_key (Optional[str]) –
Return type
None
attribute api_url: str = ''
Model name to use.
attribute model_kwargs: Dict[str, Any] [Optional]
Holds any model parameters valid for create call not
explicitly specified.
attribute tags: Optional[List[str]] = None
Tags to add to the run trace.
attribute verbose: bool [Optional]
Whether to print out response text.
__call__(prompt, stop=None, callbacks=None, **kwargs)


Check Cache and run the LLM on the given prompt and input.
Parameters
prompt (str) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
str
async agenerate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async agenerate_prompt(prompts, stop=None, callbacks=None, **kwargs)


Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
async apredict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
async apredict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage


Return type
langchain.schema.BaseMessage
classmethod construct(_fields_set=None, **values)
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
Parameters
_fields_set (Optional[SetStr]) –
values (Any) –
Return type
Model
copy(*, include=None, exclude=None, update=None, deep=False)
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include


update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) –
Returns
new model instance
Return type
Model
dict(**kwargs)
Return a dictionary of the LLM.
Parameters
kwargs (Any) –
Return type
Dict
generate(prompts, stop=None, callbacks=None, *, tags=None, **kwargs)
Run the LLM on the given prompt and input.
Parameters
prompts (List[str]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
tags (Optional[List[str]]) –
kwargs (Any) –
Return type


kwargs (Any) –
Return type
langchain.schema.LLMResult
generate_prompt(prompts, stop=None, callbacks=None, **kwargs)
Take in a list of prompt values and return an LLMResult.
Parameters
prompts (List[langchain.schema.PromptValue]) –
stop (Optional[List[str]]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Return type
langchain.schema.LLMResult
get_num_tokens(text)
Get the number of tokens present in the text.
Parameters
text (str) –
Return type
int
get_num_tokens_from_messages(messages)
Get the number of tokens in the message.
Parameters
messages (List[langchain.schema.BaseMessage]) –
Return type
int
get_token_ids(text)


int
get_token_ids(text)
Get the token present in the text.
Parameters
text (str) –
Return type
List[int]
json(*, include=None, exclude=None, by_alias=False, skip_defaults=None, exclude_unset=False, exclude_defaults=False, exclude_none=False, encoder=None, models_as_dict=True, **dumps_kwargs)
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) –
by_alias (bool) –
skip_defaults (Optional[bool]) –
exclude_unset (bool) –
exclude_defaults (bool) –
exclude_none (bool) –


exclude_none (bool) –
encoder (Optional[Callable[[Any], Any]]) –
models_as_dict (bool) –
dumps_kwargs (Any) –
Return type
unicode
predict(text, *, stop=None, **kwargs)
Predict text from text.
Parameters
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
str
predict_messages(messages, *, stop=None, **kwargs)
Predict message from messages.
Parameters
messages (List[langchain.schema.BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
Return type
langchain.schema.BaseMessage
save(file_path)
Save the LLM.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the LLM to.
Return type
None
Example: