class langchain.chat_models.ChatAnthropic(*, client=None, model='claude-v1', max_tokens_to_sample=256, temperature=None, top_k=None, top_p=None, streaming=False, default_request_timeout=None, anthropic_api_url=None, anthropic_api_key=None, HUMAN_PROMPT=None, AI_PROMPT=None, count_tokens=None, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None)[source]
Bases: langchain.chat_models.base.BaseChatModel, langchain.llms.anthropic._AnthropicCommon
Wrapper around Anthropic’s large language model.
To use, you should have the anthropic python package installed, and the
environment variable ANTHROPIC_API_KEY set with your API key, or pass
it as a named parameter to the constructor.
Example
import anthropic
from langchain.llms import Anthropic


from langchain.llms import Anthropic
model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key")
Parameters
client (Any) –
model (str) –
max_tokens_to_sample (int) –
temperature (Optional[float]) –
top_k (Optional[int]) –
top_p (Optional[float]) –
streaming (bool) –
default_request_timeout (Optional[Union[float, Tuple[float, float]]]) –
anthropic_api_url (Optional[str]) –
anthropic_api_key (Optional[str]) –
HUMAN_PROMPT (Optional[str]) –
AI_PROMPT (Optional[str]) –
count_tokens (Optional[Callable[[str], int]]) –
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]]) –
Return type
None
get_num_tokens(text)[source]
Calculate number of tokens.
Parameters
text (str) –
Return type
int
property lc_serializable: bool
Return whether or not the class is serializable.
class langchain.chat_models.ChatGooglePalm(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model_name='models/chat-bison-001', google_api_key=None, temperature=None, top_p=None, top_k=None, n=1)[source]
Bases: langchain.chat_models.base.BaseChatModel, pydantic.main.BaseModel


Wrapper around Google’s PaLM Chat API.
To use you must have the google.generativeai Python package installed and
either:
The GOOGLE_API_KEY` environment varaible set with your API key, or
Pass your API key using the google_api_key kwarg to the ChatGoogle
constructor.
Example
from langchain.chat_models import ChatGooglePalm
chat = ChatGooglePalm()
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_name (str) –
google_api_key (Optional[str]) –
temperature (Optional[float]) –
top_p (Optional[float]) –


top_p (Optional[float]) –
top_k (Optional[int]) –
n (int) –
Return type
None
attribute google_api_key: Optional[str] = None
attribute model_name: str = 'models/chat-bison-001'
Model name to use.
attribute n: int = 1
Number of chat completions to generate for each prompt. Note that the API may
not return the full n completions if duplicates are generated.
attribute temperature: Optional[float] = None
Run inference with this temperature. Must by in the closed
interval [0.0, 1.0].
attribute top_k: Optional[int] = None
Decode using top-k sampling: consider the set of top_k most probable tokens.
Must be positive.
attribute top_p: Optional[float] = None
Decode using nucleus sampling: consider the smallest set of tokens whose


probability sum is at least top_p. Must be in the closed interval [0.0, 1.0].
class langchain.chat_models.ChatVertexAI(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, client=None, model_name='chat-bison', temperature=0.0, max_output_tokens=128, top_p=0.95, top_k=40, stop=None, project=None, location='us-central1', credentials=None)[source]
Bases: langchain.llms.vertexai._VertexAICommon, langchain.chat_models.base.BaseChatModel
Wrapper around Vertex AI large language models.
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 (_LanguageModel) –
model_name (str) –
temperature (float) –
max_output_tokens (int) –
top_p (float) –
top_k (int) –
stop (Optional[List[str]]) –
project (Optional[str]) –
location (str) –
credentials (Any) –
Return type
None
attribute model_name: str = 'chat-bison'
Model name to use.


Prompt Templates
Prompt template classes.
class langchain.prompts.AIMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) –
additional_kwargs (dict) –
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) –
Return type
langchain.schema.BaseMessage
class langchain.prompts.BaseChatPromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.prompts.base.BasePromptTemplate, abc.ABC
Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
Return type
None


Return type
None
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
abstract format_messages(**kwargs)[source]
Format kwargs into a list of messages.
Parameters
kwargs (Any) –
Return type
List[langchain.schema.BaseMessage]
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) –
Return type
langchain.schema.PromptValue
class langchain.prompts.BasePromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.load.serializable.Serializable, abc.ABC
Base class for all prompt templates, returning a prompt.
Parameters


Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute output_parser: Optional[langchain.schema.BaseOutputParser] = None
How to parse the output of calling an LLM on this formatted prompt.
attribute partial_variables: Mapping[str, Union[str, Callable[[], str]]] [Optional]
dict(**kwargs)[source]
Return dictionary representation of prompt.
Parameters
kwargs (Any) –
Return type
Dict
abstract format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns


Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
abstract format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) –
Return type
langchain.schema.PromptValue
partial(**kwargs)[source]
Return a partial of the prompt template.
Parameters
kwargs (Union[str, Callable[[], str]]) –
Return type
langchain.prompts.base.BasePromptTemplate
save(file_path)[source]
Save the prompt.
Parameters
file_path (Union[pathlib.Path, str]) – Path to directory to save prompt to.
Return type
None
Example:
.. code-block:: python
prompt.save(file_path=”path/prompt.yaml”)
property lc_serializable: bool
Return whether or not the class is serializable.


class langchain.prompts.ChatMessagePromptTemplate(*, prompt, additional_kwargs=None, role)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) –
additional_kwargs (dict) –
role (str) –
Return type
None
attribute role: str [Required]
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) –
Return type
langchain.schema.BaseMessage
class langchain.prompts.ChatPromptTemplate(*, input_variables, output_parser=None, partial_variables=None, messages)[source]
Bases: langchain.prompts.chat.BaseChatPromptTemplate, abc.ABC
Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –


partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
messages (List[Union[langchain.prompts.chat.BaseMessagePromptTemplate, langchain.schema.BaseMessage]]) –
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute messages: List[Union[BaseMessagePromptTemplate, BaseMessage]] [Required]
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
format_messages(**kwargs)[source]
Format kwargs into a list of messages.
Parameters
kwargs (Any) –
Return type
List[langchain.schema.BaseMessage]


List[langchain.schema.BaseMessage]
classmethod from_messages(messages)[source]
Parameters
messages (Sequence[Union[langchain.prompts.chat.BaseMessagePromptTemplate, langchain.schema.BaseMessage]]) –
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_role_strings(string_messages)[source]
Parameters
string_messages (List[Tuple[str, str]]) –
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_strings(string_messages)[source]
Parameters
string_messages (List[Tuple[Type[langchain.prompts.chat.BaseMessagePromptTemplate], str]]) –
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_template(template, **kwargs)[source]
Parameters
template (str) –
kwargs (Any) –
Return type
langchain.prompts.chat.ChatPromptTemplate
partial(**kwargs)[source]


partial(**kwargs)[source]
Return a partial of the prompt template.
Parameters
kwargs (Union[str, Callable[[], str]]) –
Return type
langchain.prompts.base.BasePromptTemplate
save(file_path)[source]
Save the prompt.
Parameters
file_path (Union[pathlib.Path, str]) – Path to directory to save prompt to.
Return type
None
Example:
.. code-block:: python
prompt.save(file_path=”path/prompt.yaml”)
class langchain.prompts.FewShotPromptTemplate(*, input_variables, output_parser=None, partial_variables=None, examples=None, example_selector=None, example_prompt, suffix, example_separator='\n\n', prefix='', template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Prompt template that contains few shot examples.
Parameters
input_variables (List[str]) –


input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
examples (Optional[List[dict]]) –
example_selector (Optional[langchain.prompts.example_selector.base.BaseExampleSelector]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
suffix (str) –
example_separator (str) –
prefix (str) –
template_format (str) –
validate_template (bool) –
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
PromptTemplate used to format an individual example.
attribute example_selector: Optional[langchain.prompts.example_selector.base.BaseExampleSelector] = None
ExampleSelector to choose the examples to format into the prompt.


Either this or examples should be provided.
attribute example_separator: str = '\n\n'
String separator used to join the prefix, the examples, and suffix.
attribute examples: Optional[List[dict]] = None
Examples to format into the prompt.
Either this or example_selector should be provided.
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute prefix: str = ''
A prompt template string to put before the examples.
attribute suffix: str [Required]
A prompt template string to put after the examples.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.
attribute validate_template: bool = True
Whether or not to try validating the template.
dict(**kwargs)[source]


dict(**kwargs)[source]
Return a dictionary of the prompt.
Parameters
kwargs (Any) –
Return type
Dict
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
property lc_serializable: bool
Return whether or not the class is serializable.
class langchain.prompts.FewShotPromptWithTemplates(*, input_variables, output_parser=None, partial_variables=None, examples=None, example_selector=None, example_prompt, suffix, example_separator='\n\n', prefix=None, template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Prompt template that contains few shot examples.
Parameters


Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
examples (Optional[List[dict]]) –
example_selector (Optional[langchain.prompts.example_selector.base.BaseExampleSelector]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
suffix (langchain.prompts.base.StringPromptTemplate) –
example_separator (str) –
prefix (Optional[langchain.prompts.base.StringPromptTemplate]) –
template_format (str) –
validate_template (bool) –
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
PromptTemplate used to format an individual example.
attribute example_selector: Optional[langchain.prompts.example_selector.base.BaseExampleSelector] = None


ExampleSelector to choose the examples to format into the prompt.
Either this or examples should be provided.
attribute example_separator: str = '\n\n'
String separator used to join the prefix, the examples, and suffix.
attribute examples: Optional[List[dict]] = None
Examples to format into the prompt.
Either this or example_selector should be provided.
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute prefix: Optional[langchain.prompts.base.StringPromptTemplate] = None
A PromptTemplate to put before the examples.
attribute suffix: langchain.prompts.base.StringPromptTemplate [Required]
A PromptTemplate to put after the examples.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.


attribute validate_template: bool = True
Whether or not to try validating the template.
dict(**kwargs)[source]
Return a dictionary of the prompt.
Parameters
kwargs (Any) –
Return type
Dict
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
class langchain.prompts.HumanMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) –
additional_kwargs (dict) –
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) –


Parameters
kwargs (Any) –
Return type
langchain.schema.BaseMessage
class langchain.prompts.LengthBasedExampleSelector(*, examples, example_prompt, get_text_length=<function _get_length_based>, max_length=2048, example_text_lengths=[])[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select examples based on length.
Parameters
examples (List[dict]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
get_text_length (Callable[[str], int]) –
max_length (int) –
example_text_lengths (List[int]) –
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]


A list of the examples that the prompt template expects.
attribute get_text_length: Callable[[str], int] = <function _get_length_based>
Function to measure prompt length. Defaults to word count.
attribute max_length: int = 2048
Max length for the prompt, beyond which examples are cut.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) –
Return type
None
select_examples(input_variables)[source]
Select which examples to use based on the input lengths.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]
class langchain.prompts.MaxMarginalRelevanceExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None, fetch_k=20)[source]


Bases: langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
ExampleSelector that selects examples based on Max Marginal Relevance.
This was shown to improve performance in this paper:
https://arxiv.org/pdf/2211.13892.pdf
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) –
k (int) –
example_keys (Optional[List[str]]) –
input_keys (Optional[List[str]]) –
fetch_k (int) –
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute fetch_k: int = 20
Number of examples to fetch to rerank.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.


the input variables instead of all variables.
attribute k: int = 4
Number of examples to select.
attribute vectorstore: langchain.vectorstores.base.VectorStore [Required]
VectorStore than contains information about examples.
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, fetch_k=20, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An iniialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select


input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
fetch_k (int) –
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]
class langchain.prompts.MessagesPlaceholder(*, variable_name)[source]
Bases: langchain.prompts.chat.BaseMessagePromptTemplate
Prompt template that assumes variable is already list of messages.
Parameters
variable_name (str) –
Return type
None
attribute variable_name: str [Required]


format_messages(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) –
Return type
List[langchain.schema.BaseMessage]
property input_variables: List[str]
Input variables for this prompt template.
class langchain.prompts.NGramOverlapExampleSelector(*, examples, example_prompt, threshold=- 1.0)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select and order examples based on ngram overlap score (sentence_bleu score).
https://www.nltk.org/_modules/nltk/translate/bleu_score.html
https://aclanthology.org/P02-1040.pdf
Parameters
examples (List[dict]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
threshold (float) –
Return type
None


Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.
attribute threshold: float = -1.0
Threshold at which algorithm stops. Set to -1.0 by default.
For negative threshold:
select_examples sorts examples by ngram_overlap_score, but excludes none.
For threshold greater than 1.0:
select_examples excludes all examples, and returns an empty list.
For threshold equal to 0.0:
select_examples sorts examples by ngram_overlap_score,
and excludes examples with no ngram overlap with input.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) –
Return type
None


Return type
None
select_examples(input_variables)[source]
Return list of examples sorted by ngram_overlap_score with input.
Descending order.
Excludes any examples with ngram_overlap_score less than or equal to threshold.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]
class langchain.prompts.PipelinePromptTemplate(*, input_variables, output_parser=None, partial_variables=None, final_prompt, pipeline_prompts)[source]
Bases: langchain.prompts.base.BasePromptTemplate
A prompt template for composing multiple prompts together.
This can be useful when you want to reuse parts of prompts.
A PipelinePrompt consists of two main parts:
final_prompt: This is the final prompt that is returned
pipeline_prompts: This is a list of tuples, consistingof a string (name) and a Prompt Template.
Each PromptTemplate will be formatted and then passed


Each PromptTemplate will be formatted and then passed
to future prompt templates as a variable with
the same name as name
Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
final_prompt (langchain.prompts.base.BasePromptTemplate) –
pipeline_prompts (List[Tuple[str, langchain.prompts.base.BasePromptTemplate]]) –
Return type
None
attribute final_prompt: langchain.prompts.base.BasePromptTemplate [Required]
attribute pipeline_prompts: List[Tuple[str, langchain.prompts.base.BasePromptTemplate]] [Required]
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str


A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) –
Return type
langchain.schema.PromptValue
langchain.prompts.Prompt
alias of langchain.prompts.prompt.PromptTemplate
class langchain.prompts.PromptTemplate(*, input_variables, output_parser=None, partial_variables=None, template, template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Schema to represent a prompt for an LLM.
Example
from langchain import PromptTemplate
prompt = PromptTemplate(input_variables=["foo"], template="Say {foo}")
Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –


partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
template (str) –
template_format (str) –
validate_template (bool) –
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute template: str [Required]
The prompt template.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.
attribute validate_template: bool = True
Whether or not to try validating the template.
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")


prompt.format(variable1="foo")
classmethod from_examples(examples, suffix, input_variables, example_separator='\n\n', prefix='', **kwargs)[source]
Take examples in list format with prefix and suffix to create a prompt.
Intended to be used as a way to dynamically create a prompt from examples.
Parameters
examples (List[str]) – List of examples to use in the prompt.
suffix (str) – String to go after the list of examples. Should generally
set up the user’s input.
input_variables (List[str]) – A list of variable names the final prompt template
will expect.
example_separator (str) – The separator to use in between examples. Defaults
to two new line characters.
prefix (str) – String that should go before any examples. Generally includes
examples. Default to an empty string.
kwargs (Any) –
Returns
The final prompt generated.
Return type


The final prompt generated.
Return type
langchain.prompts.prompt.PromptTemplate
classmethod from_file(template_file, input_variables, **kwargs)[source]
Load a prompt from a file.
Parameters
template_file (Union[str, pathlib.Path]) – The path to the file containing the prompt template.
input_variables (List[str]) – A list of variable names the final prompt template
will expect.
kwargs (Any) –
Returns
The prompt loaded from the file.
Return type
langchain.prompts.prompt.PromptTemplate
classmethod from_template(template, **kwargs)[source]
Load a prompt template from a template.
Parameters
template (str) –
kwargs (Any) –
Return type
langchain.prompts.prompt.PromptTemplate
property lc_attributes: Dict[str, Any]
Return a list of attribute names that should be included in the


serialized kwargs. These attributes must be accepted by the
constructor.
class langchain.prompts.SemanticSimilarityExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Example selector that selects examples based on SemanticSimilarity.
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) –
k (int) –
example_keys (Optional[List[str]]) –
input_keys (Optional[List[str]]) –
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.
attribute k: int = 4


attribute k: int = 4
Number of examples to select.
attribute vectorstore: langchain.vectorstores.base.VectorStore [Required]
VectorStore than contains information about examples.
add_example(example)[source]
Add new example to vectorstore.
Parameters
example (Dict[str, str]) –
Return type
str
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An initialized embedding API interface, e.g. OpenAIEmbeddings().


vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]
class langchain.prompts.StringPromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.prompts.base.BasePromptTemplate, abc.ABC


String prompt should expose the format method, returning a prompt.
Parameters
input_variables (List[str]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) –
Return type
None
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) –
Return type
langchain.schema.PromptValue
class langchain.prompts.SystemMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) –
additional_kwargs (dict) –
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) –
Return type
langchain.schema.BaseMessage


Return type
langchain.schema.BaseMessage
langchain.prompts.load_prompt(path)[source]
Unified method for loading a prompt from LangChainHub or local fs.
Parameters
path (Union[str, pathlib.Path]) –
Return type
langchain.prompts.base.BasePromptTemplate


Example Selector
Logic for selecting examples to include in prompts.
class langchain.prompts.example_selector.LengthBasedExampleSelector(*, examples, example_prompt, get_text_length=<function _get_length_based>, max_length=2048, example_text_lengths=[])[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select examples based on length.
Parameters
examples (List[dict]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
get_text_length (Callable[[str], int]) –
max_length (int) –
example_text_lengths (List[int]) –
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.


attribute get_text_length: Callable[[str], int] = <function _get_length_based>
Function to measure prompt length. Defaults to word count.
attribute max_length: int = 2048
Max length for the prompt, beyond which examples are cut.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) –
Return type
None
select_examples(input_variables)[source]
Select which examples to use based on the input lengths.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]
class langchain.prompts.example_selector.MaxMarginalRelevanceExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None, fetch_k=20)[source]
Bases: langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector


ExampleSelector that selects examples based on Max Marginal Relevance.
This was shown to improve performance in this paper:
https://arxiv.org/pdf/2211.13892.pdf
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) –
k (int) –
example_keys (Optional[List[str]]) –
input_keys (Optional[List[str]]) –
fetch_k (int) –
Return type
None
attribute fetch_k: int = 20
Number of examples to fetch to rerank.
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, fetch_k=20, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.


embeddings (langchain.embeddings.base.Embeddings) – An iniialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
fetch_k (int) –
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]


Return type
List[dict]
class langchain.prompts.example_selector.NGramOverlapExampleSelector(*, examples, example_prompt, threshold=- 1.0)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select and order examples based on ngram overlap score (sentence_bleu score).
https://www.nltk.org/_modules/nltk/translate/bleu_score.html
https://aclanthology.org/P02-1040.pdf
Parameters
examples (List[dict]) –
example_prompt (langchain.prompts.prompt.PromptTemplate) –
threshold (float) –
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.


attribute threshold: float = -1.0
Threshold at which algorithm stops. Set to -1.0 by default.
For negative threshold:
select_examples sorts examples by ngram_overlap_score, but excludes none.
For threshold greater than 1.0:
select_examples excludes all examples, and returns an empty list.
For threshold equal to 0.0:
select_examples sorts examples by ngram_overlap_score,
and excludes examples with no ngram overlap with input.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) –
Return type
None
select_examples(input_variables)[source]
Return list of examples sorted by ngram_overlap_score with input.
Descending order.
Excludes any examples with ngram_overlap_score less than or equal to threshold.
Parameters
input_variables (Dict[str, str]) –
Return type


Return type
List[dict]
class langchain.prompts.example_selector.SemanticSimilarityExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Example selector that selects examples based on SemanticSimilarity.
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) –
k (int) –
example_keys (Optional[List[str]]) –
input_keys (Optional[List[str]]) –
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.
attribute k: int = 4


vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) –
Return type
List[dict]


Document Transformers
Transform documents
langchain.document_transformers.get_stateful_documents(documents)[source]
Convert a list of documents to a list of documents with state.
Parameters
documents (Sequence[langchain.schema.Document]) – The documents to convert.
Returns
A list of documents with state.
Return type
Sequence[langchain.document_transformers._DocumentWithState]
class langchain.document_transformers.EmbeddingsRedundantFilter(*, embeddings, similarity_fn=<function cosine_similarity>, similarity_threshold=0.95)[source]
Bases: langchain.schema.BaseDocumentTransformer, pydantic.main.BaseModel
Filter that drops redundant documents by comparing their embeddings.
Parameters
embeddings (langchain.embeddings.base.Embeddings) –
similarity_fn (Callable) –
similarity_threshold (float) –
Return type
None
attribute embeddings: langchain.embeddings.base.Embeddings [Required]


Embeddings to use for embedding document contents.
attribute similarity_fn: Callable = <function cosine_similarity>
Similarity function for comparing documents. Function expected to take as input
two matrices (List[List[float]]) and return a matrix of scores where higher values
indicate greater similarity.
attribute similarity_threshold: float = 0.95
Threshold for determining when two documents are similar enough
to be considered redundant.
async atransform_documents(documents, **kwargs)[source]
Asynchronously transform a list of documents.
Parameters
documents (Sequence[langchain.schema.Document]) –
kwargs (Any) –
Return type
Sequence[langchain.schema.Document]
transform_documents(documents, **kwargs)[source]
Filter down documents.
Parameters
documents (Sequence[langchain.schema.Document]) –
kwargs (Any) –
Return type
Sequence[langchain.schema.Document]


Sequence[langchain.schema.Document]
Text Splitters
Functionality for splitting text.
class langchain.text_splitter.TextSplitter(chunk_size=4000, chunk_overlap=200, length_function=<built-in function len>, keep_separator=False, add_start_index=False)[source]
Bases: langchain.schema.BaseDocumentTransformer, abc.ABC
Interface for splitting text into chunks.
Parameters
chunk_size (int) –
chunk_overlap (int) –
length_function (Callable[[str], int]) –
keep_separator (bool) –
add_start_index (bool) –
Return type
None
abstract split_text(text)[source]
Split text into multiple components.
Parameters
text (str) –
Return type
List[str]
create_documents(texts, metadatas=None)[source]
Create documents from a list of texts.
Parameters


Parameters
texts (List[str]) –
metadatas (Optional[List[dict]]) –
Return type
List[langchain.schema.Document]
split_documents(documents)[source]
Split documents.
Parameters
documents (Iterable[langchain.schema.Document]) –
Return type
List[langchain.schema.Document]
classmethod from_huggingface_tokenizer(tokenizer, **kwargs)[source]
Text splitter that uses HuggingFace tokenizer to count length.
Parameters
tokenizer (Any) –
kwargs (Any) –
Return type
langchain.text_splitter.TextSplitter
classmethod from_tiktoken_encoder(encoding_name='gpt2', model_name=None, allowed_special={}, disallowed_special='all', **kwargs)[source]
Text splitter that uses tiktoken encoder to count length.
Parameters
encoding_name (str) –
model_name (Optional[str]) –


model_name (Optional[str]) –
allowed_special (Union[Literal['all'], typing.AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], typing.Collection[str]]) –
kwargs (Any) –
Return type
langchain.text_splitter.TS
transform_documents(documents, **kwargs)[source]
Transform sequence of documents by splitting them.
Parameters
documents (Sequence[langchain.schema.Document]) –
kwargs (Any) –
Return type
Sequence[langchain.schema.Document]
async atransform_documents(documents, **kwargs)[source]
Asynchronously transform a sequence of documents by splitting them.
Parameters
documents (Sequence[langchain.schema.Document]) –
kwargs (Any) –
Return type
Sequence[langchain.schema.Document]
class langchain.text_splitter.CharacterTextSplitter(separator='\n\n', **kwargs)[source]


Bases: langchain.text_splitter.TextSplitter
Implementation of splitting text that looks at characters.
Parameters
separator (str) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split incoming text and return chunks.
Parameters
text (str) –
Return type
List[str]
class langchain.text_splitter.LineType[source]
Bases: TypedDict
Line type as typed dict.
metadata: Dict[str, str]
content: str
class langchain.text_splitter.HeaderType[source]
Bases: TypedDict
Header type as typed dict.
level: int
name: str
data: str
class langchain.text_splitter.MarkdownHeaderTextSplitter(headers_to_split_on, return_each_line=False)[source]
Bases: object


Bases: object
Implementation of splitting markdown files based on specified headers.
Parameters
headers_to_split_on (List[Tuple[str, str]]) –
return_each_line (bool) –
aggregate_lines_to_chunks(lines)[source]
Combine lines with common metadata into chunks
:param lines: Line of text / associated header metadata
Parameters
lines (List[langchain.text_splitter.LineType]) –
Return type
List[langchain.schema.Document]
split_text(text)[source]
Split markdown file
:param text: Markdown file
Parameters
text (str) –
Return type
List[langchain.schema.Document]
class langchain.text_splitter.Tokenizer(chunk_overlap: 'int', tokens_per_chunk: 'int', decode: 'Callable[[list[int]], str]', encode: 'Callable[[str], List[int]]')[source]
Bases: object
Parameters


Bases: object
Parameters
chunk_overlap (int) –
tokens_per_chunk (int) –
decode (Callable[[list[int]], str]) –
encode (Callable[[str], List[int]]) –
Return type
None
chunk_overlap: int
tokens_per_chunk: int
decode: Callable[[list[int]], str]
encode: Callable[[str], List[int]]
langchain.text_splitter.split_text_on_tokens(*, text, tokenizer)[source]
Split incoming text and return chunks.
Parameters
text (str) –
tokenizer (langchain.text_splitter.Tokenizer) –
Return type
List[str]
class langchain.text_splitter.TokenTextSplitter(encoding_name='gpt2', model_name=None, allowed_special={}, disallowed_special='all', **kwargs)[source]
Bases: langchain.text_splitter.TextSplitter


Implementation of splitting text that looks at tokens.
Parameters
encoding_name (str) –
model_name (Optional[str]) –
allowed_special (Union[Literal['all'], AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], Collection[str]]) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split text into multiple components.
Parameters
text (str) –
Return type
List[str]
class langchain.text_splitter.SentenceTransformersTokenTextSplitter(chunk_overlap=50, model_name='sentence-transformers/all-mpnet-base-v2', tokens_per_chunk=None, **kwargs)[source]
Bases: langchain.text_splitter.TextSplitter
Implementation of splitting text that looks at tokens.
Parameters
chunk_overlap (int) –
model_name (str) –


model_name (str) –
tokens_per_chunk (Optional[int]) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split text into multiple components.
Parameters
text (str) –
Return type
List[str]
count_tokens(*, text)[source]
Parameters
text (str) –
Return type
int
class langchain.text_splitter.Language(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases: str, enum.Enum
CPP = 'cpp'
GO = 'go'
JAVA = 'java'
JS = 'js'
PHP = 'php'
PROTO = 'proto'
PYTHON = 'python'
RST = 'rst'


RST = 'rst'
RUBY = 'ruby'
RUST = 'rust'
SCALA = 'scala'
SWIFT = 'swift'
MARKDOWN = 'markdown'
LATEX = 'latex'
HTML = 'html'
SOL = 'sol'
class langchain.text_splitter.RecursiveCharacterTextSplitter(separators=None, keep_separator=True, **kwargs)[source]
Bases: langchain.text_splitter.TextSplitter
Implementation of splitting text that looks at characters.
Recursively tries to split by different characters to find one
that works.
Parameters
separators (Optional[List[str]]) –
keep_separator (bool) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split text into multiple components.
Parameters


Split text into multiple components.
Parameters
text (str) –
Return type
List[str]
classmethod from_language(language, **kwargs)[source]
Parameters
language (langchain.text_splitter.Language) –
kwargs (Any) –
Return type
langchain.text_splitter.RecursiveCharacterTextSplitter
static get_separators_for_language(language)[source]
Parameters
language (langchain.text_splitter.Language) –
Return type
List[str]
class langchain.text_splitter.NLTKTextSplitter(separator='\n\n', **kwargs)[source]
Bases: langchain.text_splitter.TextSplitter
Implementation of splitting text that looks at sentences using NLTK.
Parameters
separator (str) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split incoming text and return chunks.
Parameters


Split incoming text and return chunks.
Parameters
text (str) –
Return type
List[str]
class langchain.text_splitter.SpacyTextSplitter(separator='\n\n', pipeline='en_core_web_sm', **kwargs)[source]
Bases: langchain.text_splitter.TextSplitter
Implementation of splitting text that looks at sentences using Spacy.
Parameters
separator (str) –
pipeline (str) –
kwargs (Any) –
Return type
None
split_text(text)[source]
Split incoming text and return chunks.
Parameters
text (str) –
Return type
List[str]
class langchain.text_splitter.PythonCodeTextSplitter(**kwargs)[source]
Bases: langchain.text_splitter.RecursiveCharacterTextSplitter
Attempts to split the text along Python syntax.
Parameters
kwargs (Any) –
Return type
None


Return type
None
class langchain.text_splitter.MarkdownTextSplitter(**kwargs)[source]
Bases: langchain.text_splitter.RecursiveCharacterTextSplitter
Attempts to split the text along Markdown-formatted headings.
Parameters
kwargs (Any) –
Return type
None
class langchain.text_splitter.LatexTextSplitter(**kwargs)[source]
Bases: langchain.text_splitter.RecursiveCharacterTextSplitter
Attempts to split the text along Latex-formatted layout elements.
Parameters
kwargs (Any) –
Return type
None


Agents
Interface for agents.
class langchain.agents.Agent(*, llm_chain, output_parser, allowed_tools=None)[source]
Bases: langchain.agents.agent.BaseSingleActionAgent
Class responsible for calling the language model and deciding the action.
This is driven by an LLMChain. The prompt in the LLMChain MUST include
a variable called “agent_scratchpad” where the agent can put its
intermediary work.
Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –
Return type
None
attribute allowed_tools: Optional[List[str]] = None
attribute llm_chain: langchain.chains.llm.LLMChain [Required]
attribute output_parser: langchain.agents.agent.AgentOutputParser [Required]


async aplan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
abstract classmethod create_prompt(tools)[source]
Create a prompt for this class.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) –
Return type
langchain.prompts.base.BasePromptTemplate
dict(**kwargs)[source]
Return dictionary representation of agent.
Parameters


Return dictionary representation of agent.
Parameters
kwargs (Any) –
Return type
Dict
classmethod from_llm_and_tools(llm, tools, callback_manager=None, output_parser=None, **kwargs)[source]
Construct an agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
kwargs (Any) –
Return type
langchain.agents.agent.Agent
get_allowed_tools()[source]
Return type
Optional[List[str]]
get_full_inputs(intermediate_steps, **kwargs)[source]
Create the full inputs for the LLMChain from intermediate steps.
Parameters


Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) –
kwargs (Any) –
Return type
Dict[str, Any]
plan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
return_stopped_response(early_stopping_method, intermediate_steps, **kwargs)[source]


Return response when agent has been stopped due to max iterations.
Parameters
early_stopping_method (str) –
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) –
kwargs (Any) –
Return type
langchain.schema.AgentFinish
tool_run_logging_kwargs()[source]
Return type
Dict
abstract property llm_prefix: str
Prefix to append the LLM call with.
abstract property observation_prefix: str
Prefix to append the observation with.
property return_values: List[str]
Return values of the agent.
class langchain.agents.AgentExecutor(*, memory=None, callbacks=None, callback_manager=None, verbose=None, tags=None, agent, tools, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', handle_parsing_errors=False)[source]


Bases: langchain.chains.base.Chain
Consists of an agent using tools.
Parameters
memory (Optional[langchain.schema.BaseMemory]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
verbose (bool) –
tags (Optional[List[str]]) –
agent (Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]) –
tools (Sequence[langchain.tools.base.BaseTool]) –
return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
handle_parsing_errors (Union[bool, str, Callable[[langchain.schema.OutputParserException], str]]) –
Return type


Return type
None
attribute agent: Union[BaseSingleActionAgent, BaseMultiActionAgent] [Required]
The agent to run for creating a plan and determining actions
to take at each step of the execution loop.
attribute early_stopping_method: str = 'force'
The method to use for early stopping if the agent never
returns AgentFinish. Either ‘force’ or ‘generate’.
“force” returns a string saying that it stopped because it met atime or iteration limit.
“generate” calls the agent’s LLM Chain one final time to generatea final answer based on the previous steps.
attribute handle_parsing_errors: Union[bool, str, Callable[[OutputParserException], str]] = False
How to handle errors raised by the agent’s output parser.Defaults to False, which raises the error.
sIf true, the error will be sent back to the LLM as an observation.


If a string, the string itself will be sent to the LLM as an observation.
If a callable function, the function will be called with the exception
as an argument, and the result of that function will be passed to the agentas an observation.
attribute max_execution_time: Optional[float] = None
The maximum amount of wall clock time to spend in the execution
loop.
attribute max_iterations: Optional[int] = 15
The maximum number of steps to take before ending the execution
loop.
Setting to ‘None’ could lead to an infinite loop.
attribute return_intermediate_steps: bool = False
Whether to return the agent’s trajectory of intermediate steps
at the end in addition to the final output.
attribute tools: Sequence[BaseTool] [Required]
The valid tools the agent can call.


The valid tools the agent can call.
classmethod from_agent_and_tools(agent, tools, callback_manager=None, **kwargs)[source]
Create from agent and tools.
Parameters
agent (Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
kwargs (Any) –
Return type
langchain.agents.agent.AgentExecutor
lookup_tool(name)[source]
Lookup tool by name.
Parameters
name (str) –
Return type
langchain.tools.base.BaseTool
save(file_path)[source]
Raise error - saving not supported for Agent Executors.
Parameters
file_path (Union[pathlib.Path, str]) –
Return type
None
save_agent(file_path)[source]


save_agent(file_path)[source]
Save the underlying agent.
Parameters
file_path (Union[pathlib.Path, str]) –
Return type
None
class langchain.agents.AgentOutputParser[source]
Bases: langchain.schema.BaseOutputParser
Return type
None
abstract parse(text)[source]
Parse text into agent action/finish.
Parameters
text (str) –
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
class langchain.agents.AgentType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases: str, enum.Enum
Enumerator with the Agent types.
ZERO_SHOT_REACT_DESCRIPTION = 'zero-shot-react-description'
REACT_DOCSTORE = 'react-docstore'


SELF_ASK_WITH_SEARCH = 'self-ask-with-search'
CONVERSATIONAL_REACT_DESCRIPTION = 'conversational-react-description'
CHAT_ZERO_SHOT_REACT_DESCRIPTION = 'chat-zero-shot-react-description'
CHAT_CONVERSATIONAL_REACT_DESCRIPTION = 'chat-conversational-react-description'
STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION = 'structured-chat-zero-shot-react-description'
OPENAI_FUNCTIONS = 'openai-functions'
OPENAI_MULTI_FUNCTIONS = 'openai-multi-functions'
class langchain.agents.BaseMultiActionAgent[source]
Bases: pydantic.main.BaseModel
Base Agent class.
Return type
None
abstract async aplan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters


Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Actions specifying what tool to use.
Return type
Union[List[langchain.schema.AgentAction], langchain.schema.AgentFinish]
dict(**kwargs)[source]
Return dictionary representation of agent.
Parameters
kwargs (Any) –
Return type
Dict
get_allowed_tools()[source]
Return type
Optional[List[str]]
abstract plan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters


Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Actions specifying what tool to use.
Return type
Union[List[langchain.schema.AgentAction], langchain.schema.AgentFinish]
return_stopped_response(early_stopping_method, intermediate_steps, **kwargs)[source]
Return response when agent has been stopped due to max iterations.
Parameters
early_stopping_method (str) –
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) –
kwargs (Any) –
Return type
langchain.schema.AgentFinish
save(file_path)[source]


save(file_path)[source]
Save the agent.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the agent to.
Return type
None
Example:
.. code-block:: python
# If working with agent executor
agent.agent.save(file_path=”path/agent.yaml”)
tool_run_logging_kwargs()[source]
Return type
Dict
property return_values: List[str]
Return values of the agent.
class langchain.agents.BaseSingleActionAgent[source]
Bases: pydantic.main.BaseModel
Base Agent class.
Return type
None
abstract async aplan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations


along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
dict(**kwargs)[source]
Return dictionary representation of agent.
Parameters
kwargs (Any) –
Return type
Dict
classmethod from_llm_and_tools(llm, tools, callback_manager=None, **kwargs)[source]
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
kwargs (Any) –
Return type
langchain.agents.agent.BaseSingleActionAgent


langchain.agents.agent.BaseSingleActionAgent
get_allowed_tools()[source]
Return type
Optional[List[str]]
abstract plan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
return_stopped_response(early_stopping_method, intermediate_steps, **kwargs)[source]
Return response when agent has been stopped due to max iterations.
Parameters


Parameters
early_stopping_method (str) –
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) –
kwargs (Any) –
Return type
langchain.schema.AgentFinish
save(file_path)[source]
Save the agent.
Parameters
file_path (Union[pathlib.Path, str]) – Path to file to save the agent to.
Return type
None
Example:
.. code-block:: python
# If working with agent executor
agent.agent.save(file_path=”path/agent.yaml”)
tool_run_logging_kwargs()[source]
Return type
Dict
property return_values: List[str]
Return values of the agent.
class langchain.agents.ConversationalAgent(*, llm_chain, output_parser=None, allowed_tools=None, ai_prefix='AI')[source]
Bases: langchain.agents.agent.Agent


Bases: langchain.agents.agent.Agent
An agent designed to hold a conversation in addition to using tools.
Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –
ai_prefix (str) –
Return type
None
attribute ai_prefix: str = 'AI'
attribute output_parser: langchain.agents.agent.AgentOutputParser [Optional]


classmethod create_prompt(tools, prefix='Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable


help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\n\nTOOLS:\n------\n\nAssistant has access to the following tools:', suffix='Begin!\n\nPrevious conversation history:\n{chat_history}\n\nNew input: {input}\n{agent_scratchpad}', format_instructions='To use a tool, please use the following format:\n\n```\nThought: Do I need to use a tool? Yes\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n```\n\nWhen you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:\n\n```\nThought: Do I


Do I need to use a tool? No\n{ai_prefix}: [your response here]\n```', ai_prefix='AI', human_prefix='Human', input_variables=None)[source]


Create prompt in the style of the zero shot agent.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) – List of tools the agent will have access to, used to format the
prompt.
prefix (str) – String to put before the list of tools.
suffix (str) – String to put after the list of tools.
ai_prefix (str) – String to use before AI output.
human_prefix (str) – String to use before human output.
input_variables (Optional[List[str]]) – List of input variables the final prompt will expect.
format_instructions (str) –
Returns
A PromptTemplate with the template assembled from the pieces here.
Return type
langchain.prompts.prompt.PromptTemplate


classmethod from_llm_and_tools(llm, tools, callback_manager=None, output_parser=None, prefix='Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful


topics.\n\nOverall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\n\nTOOLS:\n------\n\nAssistant has access to the following tools:', suffix='Begin!\n\nPrevious conversation history:\n{chat_history}\n\nNew input: {input}\n{agent_scratchpad}', format_instructions='To use a tool, please use the following format:\n\n```\nThought: Do I need to use a tool? Yes\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n```\n\nWhen you have a response to say to the Human, or if you do not need to use a tool, you MUST


do not need to use a tool, you MUST use the format:\n\n```\nThought: Do I need to use a tool? No\n{ai_prefix}: [your response here]\n```', ai_prefix='AI', human_prefix='Human', input_variables=None, **kwargs)[source]


Construct an agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
prefix (str) –
suffix (str) –
format_instructions (str) –
ai_prefix (str) –
human_prefix (str) –
input_variables (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.agents.agent.Agent
property llm_prefix: str
Prefix to append the llm call with.
property observation_prefix: str
Prefix to append the observation with.


Prefix to append the observation with.
class langchain.agents.ConversationalChatAgent(*, llm_chain, output_parser=None, allowed_tools=None, template_tool_response="TOOL RESPONSE: \n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.")[source]
Bases: langchain.agents.agent.Agent
An agent designed to hold a conversation in addition to using tools.
Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –


allowed_tools (Optional[List[str]]) –
template_tool_response (str) –
Return type
None
attribute output_parser: langchain.agents.agent.AgentOutputParser [Optional]
attribute template_tool_response: str = "TOOL RESPONSE: \n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else."


classmethod create_prompt(tools, system_message='Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide


can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.', human_message="TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{{tools}}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{{{input}}}}", input_variables=None, output_parser=None)[source]


Create a prompt for this class.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) –
system_message (str) –
human_message (str) –
input_variables (Optional[List[str]]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
Return type
langchain.prompts.base.BasePromptTemplate


classmethod from_llm_and_tools(llm, tools, callback_manager=None, output_parser=None, system_message='Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a


of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.', human_message="TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{{tools}}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{{{input}}}}", input_variables=None, **kwargs)[source]


Construct an agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
system_message (str) –
human_message (str) –
input_variables (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.agents.agent.Agent
property llm_prefix: str
Prefix to append the llm call with.
property observation_prefix: str
Prefix to append the observation with.
class langchain.agents.LLMSingleActionAgent(*, llm_chain, output_parser, stop)[source]
Bases: langchain.agents.agent.BaseSingleActionAgent
Parameters


Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
stop (List[str]) –
Return type
None
attribute llm_chain: langchain.chains.llm.LLMChain [Required]
attribute output_parser: langchain.agents.agent.AgentOutputParser [Required]
attribute stop: List[str] [Required]
async aplan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.


**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
dict(**kwargs)[source]
Return dictionary representation of agent.
Parameters
kwargs (Any) –
Return type
Dict
plan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type


Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
tool_run_logging_kwargs()[source]
Return type
Dict
class langchain.agents.MRKLChain(*, memory=None, callbacks=None, callback_manager=None, verbose=None, tags=None, agent, tools, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', handle_parsing_errors=False)[source]
Bases: langchain.agents.agent.AgentExecutor
Chain that implements the MRKL system.
Example
from langchain import OpenAI, MRKLChain
from langchain.chains.mrkl.base import ChainConfig
llm = OpenAI(temperature=0)
prompt = PromptTemplate(...)
chains = [...]
mrkl = MRKLChain.from_chains(llm=llm, prompt=prompt)
Parameters
memory (Optional[langchain.schema.BaseMemory]) –


callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
verbose (bool) –
tags (Optional[List[str]]) –
agent (Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]) –
tools (Sequence[langchain.tools.base.BaseTool]) –
return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
handle_parsing_errors (Union[bool, str, Callable[[langchain.schema.OutputParserException], str]]) –
Return type
None
classmethod from_chains(llm, chains, **kwargs)[source]
User friendly way to initialize the MRKL chain.


This is intended to be an easy way to get up and running with the
MRKL chain.
Parameters
llm (langchain.base_language.BaseLanguageModel) – The LLM to use as the agent LLM.
chains (List[langchain.agents.mrkl.base.ChainConfig]) – The chains the MRKL system has access to.
**kwargs – parameters to be passed to initialization.
kwargs (Any) –
Returns
An initialized MRKL chain.
Return type
langchain.agents.agent.AgentExecutor
Example
from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, MRKLChain
from langchain.chains.mrkl.base import ChainConfig
llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain(llm=llm)
chains = [
    ChainConfig(
        action_name = "Search",


action_name = "Search",
        action=search.search,
        action_description="useful for searching"
    ),
    ChainConfig(
        action_name="Calculator",
        action=llm_math_chain.run,
        action_description="useful for doing math"
    )
]
mrkl = MRKLChain.from_chains(llm, chains)
class langchain.agents.OpenAIFunctionsAgent(*, llm, tools, prompt)[source]
Bases: langchain.agents.agent.BaseSingleActionAgent
An Agent driven by OpenAIs function powered API.
Parameters
llm (langchain.base_language.BaseLanguageModel) – This should be an instance of ChatOpenAI, specifically a model
that supports using functions.
tools (Sequence[langchain.tools.base.BaseTool]) – The tools this agent has access to.


prompt (langchain.prompts.base.BasePromptTemplate) – The prompt for this agent, should support agent_scratchpad as one
of the variables. For an easy way to construct this prompt, use
OpenAIFunctionsAgent.create_prompt(…)
Return type
None
attribute llm: langchain.base_language.BaseLanguageModel [Required]
attribute prompt: langchain.prompts.base.BasePromptTemplate [Required]
attribute tools: Sequence[langchain.tools.base.BaseTool] [Required]
async aplan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date,
along with observations
**kwargs – User inputs.


**kwargs – User inputs.
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
classmethod create_prompt(system_message=SystemMessage(content='You are a helpful AI assistant.', additional_kwargs={}), extra_prompt_messages=None)[source]
Create prompt for this agent.
Parameters
system_message (Optional[langchain.schema.SystemMessage]) – Message to use as the system message that will be the
first in the prompt.
extra_prompt_messages (Optional[List[langchain.prompts.chat.BaseMessagePromptTemplate]]) – Prompt messages that will be placed between the
system message and the new human input.
Returns
A prompt template to pass into this agent.
Return type
langchain.prompts.base.BasePromptTemplate


langchain.prompts.base.BasePromptTemplate
classmethod from_llm_and_tools(llm, tools, callback_manager=None, extra_prompt_messages=None, system_message=SystemMessage(content='You are a helpful AI assistant.', additional_kwargs={}), **kwargs)[source]
Construct an agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
extra_prompt_messages (Optional[List[langchain.prompts.chat.BaseMessagePromptTemplate]]) –
system_message (Optional[langchain.schema.SystemMessage]) –
kwargs (Any) –
Return type
langchain.agents.agent.BaseSingleActionAgent
get_allowed_tools()[source]
Get allowed tools.
Return type
List[str]


Return type
List[str]
plan(intermediate_steps, callbacks=None, **kwargs)[source]
Given input, decided what to do.
Parameters
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) – Steps the LLM has taken to date, along with observations
**kwargs – User inputs.
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
kwargs (Any) –
Returns
Action specifying what tool to use.
Return type
Union[langchain.schema.AgentAction, langchain.schema.AgentFinish]
property functions: List[dict]
property input_keys: List[str]
Get input keys. Input refers to user input here.


class langchain.agents.ReActChain(llm, docstore, *, memory=None, callbacks=None, callback_manager=None, verbose=None, tags=None, agent, tools, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', handle_parsing_errors=False)[source]
Bases: langchain.agents.agent.AgentExecutor
Chain that implements the ReAct paper.
Example
from langchain import ReActChain, OpenAI
react = ReAct(llm=OpenAI())
Parameters
llm (langchain.base_language.BaseLanguageModel) –
docstore (langchain.docstore.base.Docstore) –
memory (Optional[langchain.schema.BaseMemory]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –


verbose (bool) –
tags (Optional[List[str]]) –
agent (Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]) –
tools (Sequence[langchain.tools.base.BaseTool]) –
return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
handle_parsing_errors (Union[bool, str, Callable[[langchain.schema.OutputParserException], str]]) –
Return type
None
class langchain.agents.ReActTextWorldAgent(*, llm_chain, output_parser=None, allowed_tools=None)[source]
Bases: langchain.agents.react.base.ReActDocstoreAgent
Agent for the ReAct TextWorld chain.
Parameters
llm_chain (langchain.chains.llm.LLMChain) –


output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –
Return type
None
classmethod create_prompt(tools)[source]
Return default prompt.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) –
Return type
langchain.prompts.base.BasePromptTemplate
class langchain.agents.SelfAskWithSearchChain(llm, search_chain, *, memory=None, callbacks=None, callback_manager=None, verbose=None, tags=None, agent, tools, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', handle_parsing_errors=False)[source]
Bases: langchain.agents.agent.AgentExecutor
Chain that does self ask with search.
Example
from langchain import SelfAskWithSearchChain, OpenAI, GoogleSerperAPIWrapper
search_chain = GoogleSerperAPIWrapper()


search_chain = GoogleSerperAPIWrapper()
self_ask = SelfAskWithSearchChain(llm=OpenAI(), search_chain=search_chain)
Parameters
llm (langchain.base_language.BaseLanguageModel) –
search_chain (Union[langchain.utilities.google_serper.GoogleSerperAPIWrapper, langchain.utilities.serpapi.SerpAPIWrapper]) –
memory (Optional[langchain.schema.BaseMemory]) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
verbose (bool) –
tags (Optional[List[str]]) –
agent (Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]) –
tools (Sequence[langchain.tools.base.BaseTool]) –
return_intermediate_steps (bool) –


return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
handle_parsing_errors (Union[bool, str, Callable[[langchain.schema.OutputParserException], str]]) –
Return type
None
class langchain.agents.StructuredChatAgent(*, llm_chain, output_parser=None, allowed_tools=None)[source]
Bases: langchain.agents.agent.Agent
Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –
Return type
None
attribute output_parser: langchain.agents.agent.AgentOutputParser [Optional]


classmethod create_prompt(tools, prefix='Respond to the human as helpfully and accurately as possible. You have access to the following tools:', suffix='Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.\nThought:', human_message_template='{input}\n\n{agent_scratchpad}', format_instructions='Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).\n\nValid "action" values: "Final Answer" or {tool_names}\n\nProvide only ONE action per $JSON_BLOB, as shown:\n\n```\n{{{{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}}}}\n```\n\nFollow this format:\n\nQuestion: input question to answer\nThought: consider


input question to answer\nThought: consider previous and subsequent steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action result\n... (repeat Thought/Action/Observation N times)\nThought: I know what to respond\nAction:\n```\n{{{{\n  "action": "Final Answer",\n  "action_input": "Final response to human"\n}}}}\n```', input_variables=None, memory_prompts=None)[source]


Create a prompt for this class.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) –
prefix (str) –
suffix (str) –
human_message_template (str) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
memory_prompts (Optional[List[langchain.prompts.base.BasePromptTemplate]]) –
Return type
langchain.prompts.base.BasePromptTemplate


classmethod from_llm_and_tools(llm, tools, callback_manager=None, output_parser=None, prefix='Respond to the human as helpfully and accurately as possible. You have access to the following tools:', suffix='Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.\nThought:', human_message_template='{input}\n\n{agent_scratchpad}', format_instructions='Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).\n\nValid "action" values: "Final Answer" or {tool_names}\n\nProvide only ONE action per $JSON_BLOB, as shown:\n\n```\n{{{{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}}}}\n```\n\nFollow this


this format:\n\nQuestion: input question to answer\nThought: consider previous and subsequent steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action result\n... (repeat Thought/Action/Observation N times)\nThought: I know what to respond\nAction:\n```\n{{{{\n  "action": "Final Answer",\n  "action_input": "Final response to human"\n}}}}\n```', input_variables=None, memory_prompts=None, **kwargs)[source]


Construct an agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
prefix (str) –
suffix (str) –
human_message_template (str) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
memory_prompts (Optional[List[langchain.prompts.base.BasePromptTemplate]]) –
kwargs (Any) –
Return type
langchain.agents.agent.Agent
property llm_prefix: str
Prefix to append the llm call with.
property observation_prefix: str
Prefix to append the observation with.


Prefix to append the observation with.
class langchain.agents.Tool(name, func, description, *, args_schema=None, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, coroutine=None)[source]
Bases: langchain.tools.base.BaseTool
Tool that takes in function or coroutine directly.
Parameters
name (str) –
func (Callable[[...], str]) –
description (str) –
args_schema (Optional[Type[pydantic.main.BaseModel]]) –
return_direct (bool) –
verbose (bool) –
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
handle_tool_error (Optional[Union[bool, str, Callable[[langchain.tools.base.ToolException], str]]]) –


coroutine (Optional[Callable[[...], Awaitable[str]]]) –
Return type
None
attribute coroutine: Optional[Callable[[...], Awaitable[str]]] = None
The asynchronous version of the function.
attribute description: str = ''
Used to tell the model how/when/why to use the tool.
You can provide few-shot examples as a part of the description.
attribute func: Callable[[...], str] [Required]
The function to run when the tool is called.
classmethod from_function(func, name, description, return_direct=False, args_schema=None, **kwargs)[source]
Initialize tool from a function.
Parameters
func (Callable) –
name (str) –
description (str) –
return_direct (bool) –
args_schema (Optional[Type[pydantic.main.BaseModel]]) –


kwargs (Any) –
Return type
langchain.tools.base.Tool
property args: dict
The tool’s input arguments.
class langchain.agents.ZeroShotAgent(*, llm_chain, output_parser=None, allowed_tools=None)[source]
Bases: langchain.agents.agent.Agent
Agent for the MRKL chain.
Parameters
llm_chain (langchain.chains.llm.LLMChain) –
output_parser (langchain.agents.agent.AgentOutputParser) –
allowed_tools (Optional[List[str]]) –
Return type
None
attribute output_parser: langchain.agents.agent.AgentOutputParser [Optional]


classmethod create_prompt(tools, prefix='Answer the following questions as best you can. You have access to the following tools:', suffix='Begin!\n\nQuestion: {input}\nThought:{agent_scratchpad}', format_instructions='Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question', input_variables=None)[source]
Create prompt in the style of the zero shot agent.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) – List of tools the agent will have access to, used to format the


prompt.
prefix (str) – String to put before the list of tools.
suffix (str) – String to put after the list of tools.
input_variables (Optional[List[str]]) – List of input variables the final prompt will expect.
format_instructions (str) –
Returns
A PromptTemplate with the template assembled from the pieces here.
Return type
langchain.prompts.prompt.PromptTemplate


langchain.prompts.prompt.PromptTemplate
classmethod from_llm_and_tools(llm, tools, callback_manager=None, output_parser=None, prefix='Answer the following questions as best you can. You have access to the following tools:', suffix='Begin!\n\nQuestion: {input}\nThought:{agent_scratchpad}', format_instructions='Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question', input_variables=None, **kwargs)[source]
Construct an agent from an LLM and tools.
Parameters


Parameters
llm (langchain.base_language.BaseLanguageModel) –
tools (Sequence[langchain.tools.base.BaseTool]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
prefix (str) –
suffix (str) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
kwargs (Any) –
Return type
langchain.agents.agent.Agent
property llm_prefix: str
Prefix to append the llm call with.
property observation_prefix: str
Prefix to append the observation with.
langchain.agents.create_csv_agent(llm, path, pandas_kwargs=None, **kwargs)[source]
Create csv agent by loading to a dataframe and using pandas agent.
Parameters


Parameters
llm (langchain.base_language.BaseLanguageModel) –
path (Union[str, List[str]]) –
pandas_kwargs (Optional[dict]) –
kwargs (Any) –
Return type
langchain.agents.agent.AgentExecutor


langchain.agents.create_json_agent(llm, toolkit, callback_manager=None, prefix='You are an agent designed to interact with JSON.\nYour goal is to return a final answer by interacting with the JSON.\nYou have access to the following tools which help you learn more about the JSON you are interacting with.\nOnly use the below tools. Only use the information returned by the below tools to construct your final answer.\nDo not make up any information that is not contained in the JSON.\nYour input to the tools should be in the form of `data["key"][0]` where `data` is the JSON blob you are interacting with, and the syntax used is Python. \nYou should only use keys that you know for a fact exist. You must validate that a key exists by seeing it previously when calling `json_spec_list_keys`. \nIf you have not seen a key in one of those responses, you cannot use it.\nYou should only


you cannot use it.\nYou should only add one key at a time to the path. You cannot add multiple keys at once.\nIf you encounter a "KeyError", go back to the previous key, look at the available keys, and try again.\n\nIf the question does not seem to be related to the JSON, just return "I don\'t know" as the answer.\nAlways begin your interaction with the `json_spec_list_keys` tool with input "data" to see what keys exist in the JSON.\n\nNote that sometimes the value at a given path is large. In this case, you will get an error "Value is a large dictionary, should explore its keys directly".\nIn this case, you should ALWAYS follow up by using the `json_spec_list_keys` tool to see what keys exist at that path.\nDo not simply refer the user to the JSON or a section of the JSON, as this is not a valid answer.


JSON, as this is not a valid answer. Keep digging until you find the answer and explicitly return it.\n', suffix='Begin!"\n\nQuestion: {input}\nThought: I should look at the keys that exist in data to see what I have access to\n{agent_scratchpad}', format_instructions='Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question', input_variables=None, verbose=False, agent_executor_kwargs=None, **kwargs)[source]


Construct a json agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (langchain.agents.agent_toolkits.json.toolkit.JsonToolkit) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
suffix (str) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor


langchain.agents.create_openapi_agent(llm, toolkit, callback_manager=None, prefix="You are an agent designed to answer questions by making web requests to an API given the openapi spec.\n\nIf the question does not seem related to the API, return I don't know. Do not make up an answer.\nOnly use information provided by the tools to construct your response.\n\nFirst, find the base URL needed to make the request.\n\nSecond, find the relevant paths needed to answer the question. Take note that, sometimes, you might need to make more than one request to more than one path to answer the question.\n\nThird, find the required parameters needed to make the request. For GET requests, these are usually URL parameters and for POST requests, these are request body parameters.\n\nFourth, make the requests needed to answer the question. Ensure that you are sending the correct parameters to the request by checking which parameters are required. For parameters


request by checking which parameters are required. For parameters with a fixed set of values, please use the spec to look at which values are allowed.\n\nUse the exact parameter names as listed in the spec, do not make up any names or abbreviate the names of parameters.\nIf you get a not found error, ensure that you are using a path that actually exists in the spec.\n", suffix='Begin!\n\nQuestion: {input}\nThought: I should explore the spec to find the base url for the API.\n{agent_scratchpad}', format_instructions='Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N


Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question', input_variables=None, max_iterations=15, max_execution_time=None, early_stopping_method='force', verbose=False, return_intermediate_steps=False, agent_executor_kwargs=None, **kwargs)[source]


Construct a json agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (langchain.agents.agent_toolkits.openapi.toolkit.OpenAPIToolkit) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
suffix (str) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
verbose (bool) –
return_intermediate_steps (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor


langchain.agents.agent.AgentExecutor
langchain.agents.create_pandas_dataframe_agent(llm, df, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, callback_manager=None, prefix=None, suffix=None, input_variables=None, verbose=False, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', agent_executor_kwargs=None, include_df_in_prompt=True, **kwargs)[source]
Construct a pandas agent from an LLM and dataframe.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
df (Any) –
agent_type (langchain.agents.agent_types.AgentType) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (Optional[str]) –
suffix (Optional[str]) –
input_variables (Optional[List[str]]) –
verbose (bool) –


verbose (bool) –
return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
include_df_in_prompt (Optional[bool]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor


langchain.agents.create_pbi_agent(llm, toolkit, powerbi=None, callback_manager=None, prefix='You are an agent designed to help users interact with a PowerBI Dataset.\n\nAgent has access to a tool that can write a query based on the question and then run those against PowerBI, Microsofts business intelligence tool. The questions from the users should be interpreted as related to the dataset that is available and not general questions about the world. If the question does not seem related to the dataset, just return "This does not appear to be part of this dataset." as the answer.\n\nGiven an input question, ask to run the questions against the dataset, then look at the results and return the answer, the answer should be a complete sentence that answers the question, if multiple rows are asked find a way to write that in a easily readable format for a human, also make sure to represent numbers in readable ways, like 1M instead of


in readable ways, like 1M instead of 1000000. Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results.\n', suffix='Begin!\n\nQuestion: {input}\nThought: I can first ask which tables I have, then how each table is defined and then ask the query tool the question I need, and finally create a nice sentence that answers the question.\n{agent_scratchpad}', format_instructions='Use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer


the final answer\nFinal Answer: the final answer to the original input question', examples=None, input_variables=None, top_k=10, verbose=False, agent_executor_kwargs=None, **kwargs)[source]


Construct a pbi agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (Optional[langchain.agents.agent_toolkits.powerbi.toolkit.PowerBIToolkit]) –
powerbi (Optional[langchain.utilities.powerbi.PowerBIDataset]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
suffix (str) –
format_instructions (str) –
examples (Optional[str]) –
input_variables (Optional[List[str]]) –
top_k (int) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor