dict(**kwargs)[source]
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
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


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
None


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 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'


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
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
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
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 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
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Callbacks to run.
**kwargs – User inputs.
kwargs (Any) –


**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
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
early_stopping_method (str) –
intermediate_steps (List[Tuple[langchain.schema.AgentAction, str]]) –
kwargs (Any) –
Return type
langchain.schema.AgentFinish
save(file_path)[source]


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
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 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 need to use a tool?


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)[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 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 use the


say to the Human, or if you 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.
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]]) –
template_tool_response (str) –
Return type
None
attribute output_parser: langchain.agents.agent.AgentOutputParser [Optional]


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 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]]) –


human_message (str) –
input_variables (Optional[List[str]]) –
output_parser (Optional[langchain.schema.BaseOutputParser]) –
Return type
langchain.prompts.base.BasePromptTemplate


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 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) –


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
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 (Any) –
Returns
Action specifying what tool to use.
Return type


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
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]) –


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 = [


llm_math_chain = LLMMathChain(llm=llm)
chains = [
    ChainConfig(
        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.


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]
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
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]
plan(intermediate_steps, callbacks=None, **kwargs)[source]


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]]) –


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) –
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]


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 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]]) –


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 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) –


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.
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


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]


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


Return type
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
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
llm (langchain.base_language.BaseLanguageModel) –


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 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. Keep digging until you find the answer and explicitly


the 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 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 times)\nThought: I now know the final answer\nFinal


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.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) –
return_intermediate_steps (bool) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –


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 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 to the original input question', examples=None, input_variables=None,


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


Return type
langchain.agents.agent.AgentExecutor
langchain.agents.create_pbi_chat_agent(llm, toolkit, powerbi=None, callback_manager=None, output_parser=None, prefix='Assistant is a large language model built to help users interact with a PowerBI Dataset.\n\nAssistant 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 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="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}}}}\n", examples=None, input_variables=None, memory=None, top_k=10, verbose=False, agent_executor_kwargs=None, **kwargs)[source]
Construct a pbi agent from an Chat LLM and tools.


Construct a pbi agent from an Chat LLM and tools.
If you supply only a toolkit and no powerbi dataset, the same LLM is used for both.
Parameters
llm (langchain.chat_models.base.BaseChatModel) –
toolkit (Optional[langchain.agents.agent_toolkits.powerbi.toolkit.PowerBIToolkit]) –
powerbi (Optional[langchain.utilities.powerbi.PowerBIDataset]) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
output_parser (Optional[langchain.agents.agent.AgentOutputParser]) –
prefix (str) –
suffix (str) –
examples (Optional[str]) –
input_variables (Optional[List[str]]) –
memory (Optional[langchain.memory.chat_memory.BaseChatMemory]) –
top_k (int) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor
langchain.agents.create_spark_dataframe_agent(llm, df, callback_manager=None, prefix='\nYou are working with a spark dataframe in Python. The name of the dataframe is `df`.\nYou should use the tools below to answer the question posed of you:', suffix='\nThis is the result of `print(df.first())`:\n{df}\n\nBegin!\nQuestion: {input}\n{agent_scratchpad}', input_variables=None, verbose=False, return_intermediate_steps=False, max_iterations=15, max_execution_time=None, early_stopping_method='force', agent_executor_kwargs=None, **kwargs)[source]
Construct a spark agent from an LLM and dataframe.
Parameters
llm (langchain.llms.base.BaseLLM) –
df (Any) –


df (Any) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
suffix (str) –
input_variables (Optional[List[str]]) –
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]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor


langchain.agents.create_spark_sql_agent(llm, toolkit, callback_manager=None, prefix='You are an agent designed to interact with Spark SQL.\nGiven an input question, create a syntactically correct Spark SQL query to run, then look at the results of the query and return the answer.\nUnless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results.\nYou can order the results by a relevant column to return the most interesting examples in the database.\nNever query for all the columns from a specific table, only ask for the relevant columns given the question.\nYou have access to tools for interacting with the database.\nOnly use the below tools. Only use the information returned by the below tools to construct your final answer.\nYou MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.\n\nDO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.\n\nIf the question does not seem related to the database, just return "I don\'t know" as the answer.\n', suffix='Begin!\n\nQuestion: {input}\nThought: I should look at the tables in the database to see what I can query.\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, top_k=10,


Answer: the final answer to the original input question', input_variables=None, top_k=10, max_iterations=15, max_execution_time=None, early_stopping_method='force', verbose=False, agent_executor_kwargs=None, **kwargs)[source]


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


langchain.agents.create_sql_agent(llm, toolkit, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, callback_manager=None, prefix='You are an agent designed to interact with a SQL database.\nGiven an input question, create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.\nUnless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results.\nYou can order the results by a relevant column to return the most interesting examples in the database.\nNever query for all the columns from a specific table, only ask for the relevant columns given the question.\nYou have access to tools for interacting with the database.\nOnly use the below tools. Only use the information returned by the below tools to construct your final answer.\nYou MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.\n\nDO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.\n\nIf the question does not seem related to the database, just return "I don\'t know" as the answer.\n', suffix=None, 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, top_k=10, max_iterations=15, max_execution_time=None, early_stopping_method='force', verbose=False, agent_executor_kwargs=None,


max_execution_time=None, early_stopping_method='force', verbose=False, agent_executor_kwargs=None, **kwargs)[source]


Construct a sql agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (langchain.agents.agent_toolkits.sql.toolkit.SQLDatabaseToolkit) –
agent_type (langchain.agents.agent_types.AgentType) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
suffix (Optional[str]) –
format_instructions (str) –
input_variables (Optional[List[str]]) –
top_k (int) –
max_iterations (Optional[int]) –
max_execution_time (Optional[float]) –
early_stopping_method (str) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor
langchain.agents.create_vectorstore_agent(llm, toolkit, callback_manager=None, prefix='You are an agent designed to answer questions about sets of documents.\nYou have access to tools for interacting with the documents, and the inputs to the tools are questions.\nSometimes, you will be asked to provide sources for your questions, in which case you should use the appropriate tool to do so.\nIf the question does not seem relevant to any of the tools provided, just return "I don\'t know" as the answer.\n', verbose=False, agent_executor_kwargs=None, **kwargs)[source]
Construct a vectorstore agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (langchain.agents.agent_toolkits.vectorstore.toolkit.VectorStoreToolkit) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
verbose (bool) –


prefix (str) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor
langchain.agents.create_vectorstore_router_agent(llm, toolkit, callback_manager=None, prefix='You are an agent designed to answer questions.\nYou have access to tools for interacting with different sources, and the inputs to the tools are questions.\nYour main task is to decide which of the tools is relevant for answering question at hand.\nFor complex questions, you can break the question down into sub questions and use tools to answers the sub questions.\n', verbose=False, agent_executor_kwargs=None, **kwargs)[source]
Construct a vectorstore router agent from an LLM and tools.
Parameters
llm (langchain.base_language.BaseLanguageModel) –
toolkit (langchain.agents.agent_toolkits.vectorstore.toolkit.VectorStoreRouterToolkit) –
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) –
prefix (str) –
verbose (bool) –
agent_executor_kwargs (Optional[Dict[str, Any]]) –
kwargs (Dict[str, Any]) –
Return type
langchain.agents.agent.AgentExecutor
langchain.agents.get_all_tool_names()[source]
Get a list of all possible tool names.
Return type
List[str]
langchain.agents.initialize_agent(tools, llm, agent=None, callback_manager=None, agent_path=None, agent_kwargs=None, *, tags=None, **kwargs)[source]
Load an agent executor given tools and LLM.
Parameters
tools (Sequence[langchain.tools.base.BaseTool]) – List of tools this agent has access to.


llm (langchain.base_language.BaseLanguageModel) – Language model to use as the agent.
agent (Optional[langchain.agents.agent_types.AgentType]) – Agent type to use. If None and agent_path is also None, will default to
AgentType.ZERO_SHOT_REACT_DESCRIPTION.
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) – CallbackManager to use. Global callback manager is used if
not provided. Defaults to None.
agent_path (Optional[str]) – Path to serialized agent to use.
agent_kwargs (Optional[dict]) – Additional key word arguments to pass to the underlying agent
tags (Optional[Sequence[str]]) – Tags to apply to the traced runs.
**kwargs – Additional key word arguments passed to the agent executor
kwargs (Any) –
Returns
An agent executor
Return type
langchain.agents.agent.AgentExecutor
langchain.agents.load_agent(path, **kwargs)[source]
Unified method for loading a agent from LangChainHub or local fs.
Parameters
path (Union[str, pathlib.Path]) –
kwargs (Any) –
Return type
Union[langchain.agents.agent.BaseSingleActionAgent, langchain.agents.agent.BaseMultiActionAgent]
langchain.agents.load_huggingface_tool(task_or_repo_id, model_repo_id=None, token=None, remote=False, **kwargs)[source]
Loads a tool from the HuggingFace Hub.
Parameters
task_or_repo_id (str) – Task or model repo id.
model_repo_id (Optional[str]) – Optional model repo id.
token (Optional[str]) – Optional token.
remote (bool) – Optional remote. Defaults to False.
**kwargs –
kwargs (Any) –
Returns
A tool.
Return type
langchain.tools.base.BaseTool


Returns
A tool.
Return type
langchain.tools.base.BaseTool
langchain.agents.load_tools(tool_names, llm=None, callbacks=None, **kwargs)[source]
Load tools based on their name.
Parameters
tool_names (List[str]) – name of tools to load.
llm (Optional[langchain.base_language.BaseLanguageModel]) – Optional language model, may be needed to initialize certain tools.
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – Optional callback manager or list of callback handlers.
If not provided, default global callback manager will be used.
kwargs (Any) –
Returns
List of tools.
Return type
List[langchain.tools.base.BaseTool]
langchain.agents.tool(*args, return_direct=False, args_schema=None, infer_schema=True)[source]
Make tools out of functions, can be used with or without arguments.
Parameters
*args – The arguments to the tool.
return_direct (bool) – Whether to return directly from the tool rather
than continuing the agent loop.
args_schema (Optional[Type[pydantic.main.BaseModel]]) – optional argument schema for user to specify
infer_schema (bool) – Whether to infer the schema of the arguments from
the function’s signature. This also makes the resultant tool
accept a dictionary input to its run() function.
args (Union[str, Callable]) –
Return type
Callable
Requires:
Function must be of type (str) -> str
Function must have a docstring
Examples
@tool
def search_api(query: str) -> str:
    # Searches the API for the query.
    return
@tool("search", return_direct=True)
def search_api(query: str) -> str:
    # Searches the API for the query.
    return


Source code for langchain.requests
"""Lightweight wrapper around requests library, with async support."""
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator, Dict, Optional
import aiohttp
import requests
from pydantic import BaseModel, Extra
class Requests(BaseModel):
    """Wrapper around requests to handle auth and async.
    The main purpose of this wrapper is to handle authentication (by saving
    headers) and enable easy async methods on the same base object.
    """
    headers: Optional[Dict[str, str]] = None
    aiosession: Optional[aiohttp.ClientSession] = None
    class Config:
        """Configuration for this pydantic object."""
        extra = Extra.forbid
        arbitrary_types_allowed = True
    def get(self, url: str, **kwargs: Any) -> requests.Response:
        """GET the URL and return the text."""
        return requests.get(url, headers=self.headers, **kwargs)
    def post(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
        """POST to the URL and return the text."""
        return requests.post(url, json=data, headers=self.headers, **kwargs)
    def patch(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
        """PATCH the URL and return the text."""
        return requests.patch(url, json=data, headers=self.headers, **kwargs)
    def put(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
        """PUT the URL and return the text."""
        return requests.put(url, json=data, headers=self.headers, **kwargs)
    def delete(self, url: str, **kwargs: Any) -> requests.Response:


def delete(self, url: str, **kwargs: Any) -> requests.Response:
        """DELETE the URL and return the text."""
        return requests.delete(url, headers=self.headers, **kwargs)
    @asynccontextmanager
    async def _arequest(
        self, method: str, url: str, **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """Make an async request."""
        if not self.aiosession:
            async with aiohttp.ClientSession() as session:
                async with session.request(
                    method, url, headers=self.headers, **kwargs
                ) as response:
                    yield response
        else:
            async with self.aiosession.request(
                method, url, headers=self.headers, **kwargs
            ) as response:
                yield response
    @asynccontextmanager
    async def aget(
        self, url: str, **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """GET the URL and return the text asynchronously."""
        async with self._arequest("GET", url, **kwargs) as response:
            yield response
    @asynccontextmanager
    async def apost(
        self, url: str, data: Dict[str, Any], **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """POST to the URL and return the text asynchronously."""
        async with self._arequest("POST", url, **kwargs) as response:
            yield response
    @asynccontextmanager
    async def apatch(
        self, url: str, data: Dict[str, Any], **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """PATCH the URL and return the text asynchronously."""


"""PATCH the URL and return the text asynchronously."""
        async with self._arequest("PATCH", url, **kwargs) as response:
            yield response
    @asynccontextmanager
    async def aput(
        self, url: str, data: Dict[str, Any], **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """PUT the URL and return the text asynchronously."""
        async with self._arequest("PUT", url, **kwargs) as response:
            yield response
    @asynccontextmanager
    async def adelete(
        self, url: str, **kwargs: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        """DELETE the URL and return the text asynchronously."""
        async with self._arequest("DELETE", url, **kwargs) as response:
            yield response
[docs]class TextRequestsWrapper(BaseModel):
    """Lightweight wrapper around requests library.
    The main purpose of this wrapper is to always return a text output.
    """
    headers: Optional[Dict[str, str]] = None
    aiosession: Optional[aiohttp.ClientSession] = None
    class Config:
        """Configuration for this pydantic object."""
        extra = Extra.forbid
        arbitrary_types_allowed = True
    @property
    def requests(self) -> Requests:
        return Requests(headers=self.headers, aiosession=self.aiosession)
[docs]    def get(self, url: str, **kwargs: Any) -> str:
        """GET the URL and return the text."""
        return self.requests.get(url, **kwargs).text
[docs]    def post(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:


"""POST to the URL and return the text."""
        return self.requests.post(url, data, **kwargs).text
[docs]    def patch(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:
        """PATCH the URL and return the text."""
        return self.requests.patch(url, data, **kwargs).text
[docs]    def put(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:
        """PUT the URL and return the text."""
        return self.requests.put(url, data, **kwargs).text
[docs]    def delete(self, url: str, **kwargs: Any) -> str:
        """DELETE the URL and return the text."""
        return self.requests.delete(url, **kwargs).text
[docs]    async def aget(self, url: str, **kwargs: Any) -> str:
        """GET the URL and return the text asynchronously."""
        async with self.requests.aget(url, **kwargs) as response:
            return await response.text()
[docs]    async def apost(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:
        """POST to the URL and return the text asynchronously."""
        async with self.requests.apost(url, **kwargs) as response:
            return await response.text()
[docs]    async def apatch(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:
        """PATCH the URL and return the text asynchronously."""
        async with self.requests.apatch(url, **kwargs) as response:
            return await response.text()
[docs]    async def aput(self, url: str, data: Dict[str, Any], **kwargs: Any) -> str:


"""PUT the URL and return the text asynchronously."""
        async with self.requests.aput(url, **kwargs) as response:
            return await response.text()
[docs]    async def adelete(self, url: str, **kwargs: Any) -> str:
        """DELETE the URL and return the text asynchronously."""
        async with self.requests.adelete(url, **kwargs) as response:
            return await response.text()
# For backwards compatibility
RequestsWrapper = TextRequestsWrapper


Source code for langchain.text_splitter
"""Functionality for splitting text."""
from __future__ import annotations
import copy
import logging
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import (
    AbstractSet,
    Any,
    Callable,
    Collection,
    Dict,
    Iterable,
    List,
    Literal,
    Optional,
    Sequence,
    Tuple,
    Type,
    TypedDict,
    TypeVar,
    Union,
    cast,
)
from langchain.docstore.document import Document
from langchain.schema import BaseDocumentTransformer
logger = logging.getLogger(__name__)
TS = TypeVar("TS", bound="TextSplitter")
def _split_text_with_regex(
    text: str, separator: str, keep_separator: bool
) -> List[str]:
    # Now that we have the separator, split the text
    if separator:
        if keep_separator:
            # The parentheses in the pattern keep the delimiters in the result.
            _splits = re.split(f"({separator})", text)
            splits = [_splits[i] + _splits[i + 1] for i in range(1, len(_splits), 2)]
            if len(_splits) % 2 == 0:
                splits += _splits[-1:]
            splits = [_splits[0]] + splits
        else:
            splits = text.split(separator)
    else:
        splits = list(text)
    return [s for s in splits if s != ""]
[docs]class TextSplitter(BaseDocumentTransformer, ABC):
    """Interface for splitting text into chunks."""
    def __init__(
        self,


"""Interface for splitting text into chunks."""
    def __init__(
        self,
        chunk_size: int = 4000,
        chunk_overlap: int = 200,
        length_function: Callable[[str], int] = len,
        keep_separator: bool = False,
        add_start_index: bool = False,
    ) -> None:
        """Create a new TextSplitter.
        Args:
            chunk_size: Maximum size of chunks to return
            chunk_overlap: Overlap in characters between chunks
            length_function: Function that measures the length of given chunks
            keep_separator: Whether or not to keep the separator in the chunks
            add_start_index: If `True`, includes chunk's start index in metadata
        """
        if chunk_overlap > chunk_size:
            raise ValueError(
                f"Got a larger chunk overlap ({chunk_overlap}) than chunk size "
                f"({chunk_size}), should be smaller."
            )
        self._chunk_size = chunk_size
        self._chunk_overlap = chunk_overlap
        self._length_function = length_function
        self._keep_separator = keep_separator
        self._add_start_index = add_start_index
[docs]    @abstractmethod
    def split_text(self, text: str) -> List[str]:
        """Split text into multiple components."""
[docs]    def create_documents(
        self, texts: List[str], metadatas: Optional[List[dict]] = None
    ) -> List[Document]:
        """Create documents from a list of texts."""
        _metadatas = metadatas or [{}] * len(texts)
        documents = []
        for i, text in enumerate(texts):
            index = -1
            for chunk in self.split_text(text):
                metadata = copy.deepcopy(_metadatas[i])


metadata = copy.deepcopy(_metadatas[i])
                if self._add_start_index:
                    index = text.find(chunk, index + 1)
                    metadata["start_index"] = index
                new_doc = Document(page_content=chunk, metadata=metadata)
                documents.append(new_doc)
        return documents
[docs]    def split_documents(self, documents: Iterable[Document]) -> List[Document]:
        """Split documents."""
        texts, metadatas = [], []
        for doc in documents:
            texts.append(doc.page_content)
            metadatas.append(doc.metadata)
        return self.create_documents(texts, metadatas=metadatas)
    def _join_docs(self, docs: List[str], separator: str) -> Optional[str]:
        text = separator.join(docs)
        text = text.strip()
        if text == "":
            return None
        else:
            return text
    def _merge_splits(self, splits: Iterable[str], separator: str) -> List[str]:
        # We now want to combine these smaller pieces into medium size
        # chunks to send to the LLM.
        separator_len = self._length_function(separator)
        docs = []
        current_doc: List[str] = []
        total = 0
        for d in splits:
            _len = self._length_function(d)
            if (
                total + _len + (separator_len if len(current_doc) > 0 else 0)
                > self._chunk_size
            ):
                if total > self._chunk_size:
                    logger.warning(
                        f"Created a chunk of size {total}, "
                        f"which is longer than the specified {self._chunk_size}"
                    )
                if len(current_doc) > 0:


)
                if len(current_doc) > 0:
                    doc = self._join_docs(current_doc, separator)
                    if doc is not None:
                        docs.append(doc)
                    # Keep on popping if:
                    # - we have a larger chunk than in the chunk overlap
                    # - or if we still have any chunks and the length is long
                    while total > self._chunk_overlap or (
                        total + _len + (separator_len if len(current_doc) > 0 else 0)
                        > self._chunk_size
                        and total > 0
                    ):
                        total -= self._length_function(current_doc[0]) + (
                            separator_len if len(current_doc) > 1 else 0
                        )
                        current_doc = current_doc[1:]
            current_doc.append(d)
            total += _len + (separator_len if len(current_doc) > 1 else 0)
        doc = self._join_docs(current_doc, separator)
        if doc is not None:
            docs.append(doc)
        return docs
[docs]    @classmethod
    def from_huggingface_tokenizer(cls, tokenizer: Any, **kwargs: Any) -> TextSplitter:
        """Text splitter that uses HuggingFace tokenizer to count length."""
        try:
            from transformers import PreTrainedTokenizerBase
            if not isinstance(tokenizer, PreTrainedTokenizerBase):
                raise ValueError(
                    "Tokenizer received was not an instance of PreTrainedTokenizerBase"
                )
            def _huggingface_tokenizer_length(text: str) -> int:
                return len(tokenizer.encode(text))
        except ImportError:
            raise ValueError(
                "Could not import transformers python package. "
                "Please install it with `pip install transformers`."
            )


"Please install it with `pip install transformers`."
            )
        return cls(length_function=_huggingface_tokenizer_length, **kwargs)
[docs]    @classmethod
    def from_tiktoken_encoder(
        cls: Type[TS],
        encoding_name: str = "gpt2",
        model_name: Optional[str] = None,
        allowed_special: Union[Literal["all"], AbstractSet[str]] = set(),
        disallowed_special: Union[Literal["all"], Collection[str]] = "all",
        **kwargs: Any,
    ) -> TS:
        """Text splitter that uses tiktoken encoder to count length."""
        try:
            import tiktoken
        except ImportError:
            raise ImportError(
                "Could not import tiktoken python package. "
                "This is needed in order to calculate max_tokens_for_prompt. "
                "Please install it with `pip install tiktoken`."
            )
        if model_name is not None:
            enc = tiktoken.encoding_for_model(model_name)
        else:
            enc = tiktoken.get_encoding(encoding_name)
        def _tiktoken_encoder(text: str) -> int:
            return len(
                enc.encode(
                    text,
                    allowed_special=allowed_special,
                    disallowed_special=disallowed_special,
                )
            )
        if issubclass(cls, TokenTextSplitter):
            extra_kwargs = {
                "encoding_name": encoding_name,
                "model_name": model_name,
                "allowed_special": allowed_special,
                "disallowed_special": disallowed_special,
            }
            kwargs = {**kwargs, **extra_kwargs}
        return cls(length_function=_tiktoken_encoder, **kwargs)
[docs]    def transform_documents(


[docs]    def transform_documents(
        self, documents: Sequence[Document], **kwargs: Any
    ) -> Sequence[Document]:
        """Transform sequence of documents by splitting them."""
        return self.split_documents(list(documents))
[docs]    async def atransform_documents(
        self, documents: Sequence[Document], **kwargs: Any
    ) -> Sequence[Document]:
        """Asynchronously transform a sequence of documents by splitting them."""
        raise NotImplementedError
[docs]class CharacterTextSplitter(TextSplitter):
    """Implementation of splitting text that looks at characters."""
    def __init__(self, separator: str = "\n\n", **kwargs: Any) -> None:
        """Create a new TextSplitter."""
        super().__init__(**kwargs)
        self._separator = separator
[docs]    def split_text(self, text: str) -> List[str]:
        """Split incoming text and return chunks."""
        # First we naively split the large input into a bunch of smaller ones.
        splits = _split_text_with_regex(text, self._separator, self._keep_separator)
        _separator = "" if self._keep_separator else self._separator
        return self._merge_splits(splits, _separator)
[docs]class LineType(TypedDict):
    """Line type as typed dict."""
    metadata: Dict[str, str]
    content: str
[docs]class HeaderType(TypedDict):
    """Header type as typed dict."""
    level: int
    name: str
    data: str
[docs]class MarkdownHeaderTextSplitter:
    """Implementation of splitting markdown files based on specified headers."""
    def __init__(
        self, headers_to_split_on: List[Tuple[str, str]], return_each_line: bool = False
    ):


):
        """Create a new MarkdownHeaderTextSplitter.
        Args:
            headers_to_split_on: Headers we want to track
            return_each_line: Return each line w/ associated headers
        """
        # Output line-by-line or aggregated into chunks w/ common headers
        self.return_each_line = return_each_line
        # Given the headers we want to split on,
        # (e.g., "#, ##, etc") order by length
        self.headers_to_split_on = sorted(
            headers_to_split_on, key=lambda split: len(split[0]), reverse=True
        )
[docs]    def aggregate_lines_to_chunks(self, lines: List[LineType]) -> List[Document]:
        """Combine lines with common metadata into chunks
        Args:
            lines: Line of text / associated header metadata
        """
        aggregated_chunks: List[LineType] = []
        for line in lines:
            if (
                aggregated_chunks
                and aggregated_chunks[-1]["metadata"] == line["metadata"]
            ):
                # If the last line in the aggregated list
                # has the same metadata as the current line,
                # append the current content to the last lines's content
                aggregated_chunks[-1]["content"] += "  \n" + line["content"]
            else:
                # Otherwise, append the current line to the aggregated list
                aggregated_chunks.append(line)
        return [
            Document(page_content=chunk["content"], metadata=chunk["metadata"])
            for chunk in aggregated_chunks
        ]
[docs]    def split_text(self, text: str) -> List[Document]:
        """Split markdown file
        Args:
            text: Markdown file"""
        # Split the input text by newline character ("\n").
        lines = text.split("\n")
        # Final output


lines = text.split("\n")
        # Final output
        lines_with_metadata: List[LineType] = []
        # Content and metadata of the chunk currently being processed
        current_content: List[str] = []
        current_metadata: Dict[str, str] = {}
        # Keep track of the nested header structure
        # header_stack: List[Dict[str, Union[int, str]]] = []
        header_stack: List[HeaderType] = []
        initial_metadata: Dict[str, str] = {}
        for line in lines:
            stripped_line = line.strip()
            # Check each line against each of the header types (e.g., #, ##)
            for sep, name in self.headers_to_split_on:
                # Check if line starts with a header that we intend to split on
                if stripped_line.startswith(sep) and (
                    # Header with no text OR header is followed by space
                    # Both are valid conditions that sep is being used a header
                    len(stripped_line) == len(sep)
                    or stripped_line[len(sep)] == " "
                ):
                    # Ensure we are tracking the header as metadata
                    if name is not None:
                        # Get the current header level
                        current_header_level = sep.count("#")
                        # Pop out headers of lower or same level from the stack
                        while (
                            header_stack
                            and header_stack[-1]["level"] >= current_header_level
                        ):
                            # We have encountered a new header
                            # at the same or higher level
                            popped_header = header_stack.pop()
                            # Clear the metadata for the
                            # popped header in initial_metadata
                            if popped_header["name"] in initial_metadata:
                                initial_metadata.pop(popped_header["name"])
                        # Push the current header to the stack


# Push the current header to the stack
                        header: HeaderType = {
                            "level": current_header_level,
                            "name": name,
                            "data": stripped_line[len(sep) :].strip(),
                        }
                        header_stack.append(header)
                        # Update initial_metadata with the current header
                        initial_metadata[name] = header["data"]
                    # Add the previous line to the lines_with_metadata
                    # only if current_content is not empty
                    if current_content:
                        lines_with_metadata.append(
                            {
                                "content": "\n".join(current_content),
                                "metadata": current_metadata.copy(),
                            }
                        )
                        current_content.clear()
                    break
            else:
                if stripped_line:
                    current_content.append(stripped_line)
                elif current_content:
                    lines_with_metadata.append(
                        {
                            "content": "\n".join(current_content),
                            "metadata": current_metadata.copy(),
                        }
                    )
                    current_content.clear()
            current_metadata = initial_metadata.copy()
        if current_content:
            lines_with_metadata.append(
                {"content": "\n".join(current_content), "metadata": current_metadata}
            )
        # lines_with_metadata has each line with associated header metadata
        # aggregate these into chunks based on common metadata
        if not self.return_each_line:
            return self.aggregate_lines_to_chunks(lines_with_metadata)
        else:
            return [
                Document(page_content=chunk["content"], metadata=chunk["metadata"])
                for chunk in lines_with_metadata
            ]
# should be in newer Python versions (3.10+)
# @dataclass(frozen=True, kw_only=True, slots=True)
[docs]@dataclass(frozen=True)
class Tokenizer:
    chunk_overlap: int


class Tokenizer:
    chunk_overlap: int
    tokens_per_chunk: int
    decode: Callable[[list[int]], str]
    encode: Callable[[str], List[int]]
[docs]def split_text_on_tokens(*, text: str, tokenizer: Tokenizer) -> List[str]:
    """Split incoming text and return chunks."""
    splits: List[str] = []
    input_ids = tokenizer.encode(text)
    start_idx = 0
    cur_idx = min(start_idx + tokenizer.tokens_per_chunk, len(input_ids))
    chunk_ids = input_ids[start_idx:cur_idx]
    while start_idx < len(input_ids):
        splits.append(tokenizer.decode(chunk_ids))
        start_idx += tokenizer.tokens_per_chunk - tokenizer.chunk_overlap
        cur_idx = min(start_idx + tokenizer.tokens_per_chunk, len(input_ids))
        chunk_ids = input_ids[start_idx:cur_idx]
    return splits
[docs]class TokenTextSplitter(TextSplitter):
    """Implementation of splitting text that looks at tokens."""
    def __init__(
        self,
        encoding_name: str = "gpt2",
        model_name: Optional[str] = None,
        allowed_special: Union[Literal["all"], AbstractSet[str]] = set(),
        disallowed_special: Union[Literal["all"], Collection[str]] = "all",
        **kwargs: Any,
    ) -> None:
        """Create a new TextSplitter."""
        super().__init__(**kwargs)
        try:
            import tiktoken
        except ImportError:
            raise ImportError(
                "Could not import tiktoken python package. "
                "This is needed in order to for TokenTextSplitter. "
                "Please install it with `pip install tiktoken`."
            )
        if model_name is not None:


)
        if model_name is not None:
            enc = tiktoken.encoding_for_model(model_name)
        else:
            enc = tiktoken.get_encoding(encoding_name)
        self._tokenizer = enc
        self._allowed_special = allowed_special
        self._disallowed_special = disallowed_special
[docs]    def split_text(self, text: str) -> List[str]:
        def _encode(_text: str) -> List[int]:
            return self._tokenizer.encode(
                _text,
                allowed_special=self._allowed_special,
                disallowed_special=self._disallowed_special,
            )
        tokenizer = Tokenizer(
            chunk_overlap=self._chunk_overlap,
            tokens_per_chunk=self._chunk_size,
            decode=self._tokenizer.decode,
            encode=_encode,
        )
        return split_text_on_tokens(text=text, tokenizer=tokenizer)
[docs]class SentenceTransformersTokenTextSplitter(TextSplitter):
    """Implementation of splitting text that looks at tokens."""
    def __init__(
        self,
        chunk_overlap: int = 50,
        model_name: str = "sentence-transformers/all-mpnet-base-v2",
        tokens_per_chunk: Optional[int] = None,
        **kwargs: Any,
    ) -> None:
        """Create a new TextSplitter."""
        super().__init__(**kwargs, chunk_overlap=chunk_overlap)
        try:
            from sentence_transformers import SentenceTransformer
        except ImportError:
            raise ImportError(
                "Could not import sentence_transformer python package. "
                "This is needed in order to for SentenceTransformersTokenTextSplitter. "
                "Please install it with `pip install sentence-transformers`."
            )
        self.model_name = model_name


)
        self.model_name = model_name
        self._model = SentenceTransformer(self.model_name)
        self.tokenizer = self._model.tokenizer
        self._initialize_chunk_configuration(tokens_per_chunk=tokens_per_chunk)
    def _initialize_chunk_configuration(
        self, *, tokens_per_chunk: Optional[int]
    ) -> None:
        self.maximum_tokens_per_chunk = cast(int, self._model.max_seq_length)
        if tokens_per_chunk is None:
            self.tokens_per_chunk = self.maximum_tokens_per_chunk
        else:
            self.tokens_per_chunk = tokens_per_chunk
        if self.tokens_per_chunk > self.maximum_tokens_per_chunk:
            raise ValueError(
                f"The token limit of the models '{self.model_name}'"
                f" is: {self.maximum_tokens_per_chunk}."
                f" Argument tokens_per_chunk={self.tokens_per_chunk}"
                f" > maximum token limit."
            )
[docs]    def split_text(self, text: str) -> List[str]:
        def encode_strip_start_and_stop_token_ids(text: str) -> List[int]:
            return self._encode(text)[1:-1]
        tokenizer = Tokenizer(
            chunk_overlap=self._chunk_overlap,
            tokens_per_chunk=self.tokens_per_chunk,
            decode=self.tokenizer.decode,
            encode=encode_strip_start_and_stop_token_ids,
        )
        return split_text_on_tokens(text=text, tokenizer=tokenizer)
[docs]    def count_tokens(self, *, text: str) -> int:
        return len(self._encode(text))
    _max_length_equal_32_bit_integer = 2**32
    def _encode(self, text: str) -> List[int]:
        token_ids_with_start_and_end_token_ids = self.tokenizer.encode(
            text,


token_ids_with_start_and_end_token_ids = self.tokenizer.encode(
            text,
            max_length=self._max_length_equal_32_bit_integer,
            truncation="do_not_truncate",
        )
        return token_ids_with_start_and_end_token_ids
[docs]class Language(str, Enum):
    CPP = "cpp"
    GO = "go"
    JAVA = "java"
    JS = "js"
    PHP = "php"
    PROTO = "proto"
    PYTHON = "python"
    RST = "rst"
    RUBY = "ruby"
    RUST = "rust"
    SCALA = "scala"
    SWIFT = "swift"
    MARKDOWN = "markdown"
    LATEX = "latex"
    HTML = "html"
    SOL = "sol"
[docs]class RecursiveCharacterTextSplitter(TextSplitter):
    """Implementation of splitting text that looks at characters.
    Recursively tries to split by different characters to find one
    that works.
    """
    def __init__(
        self,
        separators: Optional[List[str]] = None,
        keep_separator: bool = True,
        **kwargs: Any,
    ) -> None:
        """Create a new TextSplitter."""
        super().__init__(keep_separator=keep_separator, **kwargs)
        self._separators = separators or ["\n\n", "\n", " ", ""]
    def _split_text(self, text: str, separators: List[str]) -> List[str]:
        """Split incoming text and return chunks."""
        final_chunks = []
        # Get appropriate separator to use
        separator = separators[-1]
        new_separators = []
        for i, _s in enumerate(separators):


for i, _s in enumerate(separators):
            if _s == "":
                separator = _s
                break
            if re.search(_s, text):
                separator = _s
                new_separators = separators[i + 1 :]
                break
        splits = _split_text_with_regex(text, separator, self._keep_separator)
        # Now go merging things, recursively splitting longer texts.
        _good_splits = []
        _separator = "" if self._keep_separator else separator
        for s in splits:
            if self._length_function(s) < self._chunk_size:
                _good_splits.append(s)
            else:
                if _good_splits:
                    merged_text = self._merge_splits(_good_splits, _separator)
                    final_chunks.extend(merged_text)
                    _good_splits = []
                if not new_separators:
                    final_chunks.append(s)
                else:
                    other_info = self._split_text(s, new_separators)
                    final_chunks.extend(other_info)
        if _good_splits:
            merged_text = self._merge_splits(_good_splits, _separator)
            final_chunks.extend(merged_text)
        return final_chunks
[docs]    def split_text(self, text: str) -> List[str]:
        return self._split_text(text, self._separators)
[docs]    @classmethod
    def from_language(
        cls, language: Language, **kwargs: Any
    ) -> RecursiveCharacterTextSplitter:
        separators = cls.get_separators_for_language(language)
        return cls(separators=separators, **kwargs)
[docs]    @staticmethod
    def get_separators_for_language(language: Language) -> List[str]:
        if language == Language.CPP:
            return [