langchain_contrib.chains.mrkl package#

Submodules#

langchain_contrib.chains.mrkl.choice module#

Module for defining a single iteration of the MRKL agent loop.

class langchain_contrib.chains.mrkl.choice.MrklLoopChain(*, memory: ~langchain.schema.BaseMemory | None = None, callback_manager: ~langchain.callbacks.base.BaseCallbackManager = None, verbose: bool = None, choice_picker: ~langchain.chains.base.Chain, prep_picker_output: ~typing.Callable[[~typing.Dict[str, str]], ~typing.Dict[str, str]] = <function ChoiceChain.<lambda>>, choices: ~typing.Mapping[str, ~langchain.chains.base.Chain], choice_key: str = 'choice', ignore_keys: ~typing.List[str] = [])#

Bases: ChoiceChain

Chain executing one single iteration of the MRKL agent.

classmethod from_tools(llm: ~langchain.schema.BaseLanguageModel, tools: ~typing.List[~langchain.tools.base.BaseTool], prompt_selector: ~typing.Type[~langchain_contrib.chains.mrkl.prompt.MrklPromptSelector] = <class 'langchain_contrib.chains.mrkl.prompt.MrklPromptSelector'>, **kwargs: ~typing.Any) MrklLoopChain#

Create a new instance of the chain from tools.

langchain_contrib.chains.mrkl.pick_action module#

Chain for selecting a MRKL tool for use.

class langchain_contrib.chains.mrkl.pick_action.MrklPickActionChain(*, memory: BaseMemory | None = None, callback_manager: BaseCallbackManager = None, verbose: bool = None, llm: BaseLanguageModel, prompt: BasePromptTemplate, observation_prefix: str = 'Observation: ', choice_key: str = 'choice', action_input_key: str = 'action_input')#

Bases: Chain

MRKL chain that selects a tool for use.

action_input_key: str#

Output key for the action’s input.

choice_key: str#

Output key for which action the LLM chose.

classmethod from_tools(llm: ~langchain.schema.BaseLanguageModel, tools: ~typing.List[~langchain.tools.base.BaseTool], prompt_selector: ~typing.Type[~langchain_contrib.chains.mrkl.prompt.MrklPromptSelector] = <class 'langchain_contrib.chains.mrkl.prompt.MrklPromptSelector'>) MrklPickActionChain#

Instantiate the MRKL action chain from a list of tools.

get_action_and_input(llm_output: str) Tuple[str, str, str]#

Parse out the action and input from the LLM output.

Copied and edited from langchain/agents/mrkl/base.py

get_chat_action_and_input(llm_output: str) Tuple[str, str, str]#

Parse out the action and input from the LLM output.

Copied and edited from langchain/agents/chat/base.py

property input_keys: List[str]#

Input keys this chain expects.

llm: BaseLanguageModel#

LLM to run this iteration of the MRKL agent with.

observation_prefix: str#

Observation prefix to stop the MRKL at.

property output_keys: List[str]#

Output keys this chain expects.

prompt: BasePromptTemplate#

Prompt for a single response of the LLM.

langchain_contrib.chains.mrkl.prompt module#

Prompting configuration for MRKL agents.

class langchain_contrib.chains.mrkl.prompt.BaseMrklPrompt(*, tools: List[BaseTool])#

Bases: BaseModel, ABC

A base class for MRKL prompting.

class Config#

Bases: object

Configuration for this pydantic object.

extra = 'forbid'#
abstract property base_prompt: BasePromptTemplate#

The basis for the MRKL prompt.

template() ChoicePromptTemplate#

Return a ChoicePromptTemplate for these tools.

property tool_descriptions: F#

Descriptions of the tools and their usage.

property tool_names: List[str]#

Just the names of the tools, without their descriptions.

tools: List[BaseTool]#

Tools that the MRKL agent has access to.

class langchain_contrib.chains.mrkl.prompt.ChatMrklPrompt(*, tools: List[BaseTool], system_template: str = 'Answer the following questions as best you can. You have access to the following tools:\n\n{tool_descriptions}\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the "action" field are: {tool_names}\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{{{{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}}}}\n```\n\nHere is an example of an invalid $JSON_BLOB:\n\n```\n{{{{\n  "action": $FIRST_TOOL_NAME,\n  "action_input": $FIRST_INPUT\n}}}}\n\n{{{{\n  "action": $SECOND_TOOL_NAME,\n  "action_input": $SECOND_INPUT\n}}}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.', human_template: str = '{input}\n\n{agent_scratchpad}')#

Bases: BaseMrklPrompt

The chat version of the prompt to the MRKL agent.

property base_prompt: BasePromptTemplate#

The basis for the chat MRKL prompt.

human_template: str#
system_template: str#
class langchain_contrib.chains.mrkl.prompt.MrklPromptSelector(*, default_prompt: BasePromptTemplate, conditionals: List[Tuple[Callable[[BaseLanguageModel], bool], BasePromptTemplate]] = None)#

Bases: ConditionalPromptSelector

Prompt definitions for the MRKL agent.

class Config#

Bases: object

Configuration for this pydantic object.

extra = 'forbid'#
classmethod from_tools(tools: List[BaseTool]) MrklPromptSelector#

Construct MRKL prompt selector from a list of tools.

class langchain_contrib.chains.mrkl.prompt.StringMrklPrompt(*, tools: List[BaseTool], template_text: str = 'Answer the following questions as best you can. You have access to the following tools:\n\n{tool_descriptions}\n\nUse 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\n\nBegin!\n\nQuestion: {input}\nThought:{agent_scratchpad}')#

Bases: BaseMrklPrompt

The string version of the prompt to the MRKL agent.

property base_prompt: BasePromptTemplate#

The basis for the string MRKL prompt.

template_text: str#

Module contents#

Module to reimplement langchain’s MRKL in a more customizable manner.

class langchain_contrib.chains.mrkl.MrklLoopChain(*, memory: ~langchain.schema.BaseMemory | None = None, callback_manager: ~langchain.callbacks.base.BaseCallbackManager = None, verbose: bool = None, choice_picker: ~langchain.chains.base.Chain, prep_picker_output: ~typing.Callable[[~typing.Dict[str, str]], ~typing.Dict[str, str]] = <function ChoiceChain.<lambda>>, choices: ~typing.Mapping[str, ~langchain.chains.base.Chain], choice_key: str = 'choice', ignore_keys: ~typing.List[str] = [])#

Bases: ChoiceChain

Chain executing one single iteration of the MRKL agent.

classmethod from_tools(llm: ~langchain.schema.BaseLanguageModel, tools: ~typing.List[~langchain.tools.base.BaseTool], prompt_selector: ~typing.Type[~langchain_contrib.chains.mrkl.prompt.MrklPromptSelector] = <class 'langchain_contrib.chains.mrkl.prompt.MrklPromptSelector'>, **kwargs: ~typing.Any) MrklLoopChain#

Create a new instance of the chain from tools.

class langchain_contrib.chains.mrkl.MrklPickActionChain(*, memory: BaseMemory | None = None, callback_manager: BaseCallbackManager = None, verbose: bool = None, llm: BaseLanguageModel, prompt: BasePromptTemplate, observation_prefix: str = 'Observation: ', choice_key: str = 'choice', action_input_key: str = 'action_input')#

Bases: Chain

MRKL chain that selects a tool for use.

action_input_key: str#

Output key for the action’s input.

callback_manager: BaseCallbackManager#
choice_key: str#

Output key for which action the LLM chose.

classmethod from_tools(llm: ~langchain.schema.BaseLanguageModel, tools: ~typing.List[~langchain.tools.base.BaseTool], prompt_selector: ~typing.Type[~langchain_contrib.chains.mrkl.prompt.MrklPromptSelector] = <class 'langchain_contrib.chains.mrkl.prompt.MrklPromptSelector'>) MrklPickActionChain#

Instantiate the MRKL action chain from a list of tools.

get_action_and_input(llm_output: str) Tuple[str, str, str]#

Parse out the action and input from the LLM output.

Copied and edited from langchain/agents/mrkl/base.py

get_chat_action_and_input(llm_output: str) Tuple[str, str, str]#

Parse out the action and input from the LLM output.

Copied and edited from langchain/agents/chat/base.py

property input_keys: List[str]#

Input keys this chain expects.

llm: BaseLanguageModel#

LLM to run this iteration of the MRKL agent with.

memory: BaseMemory | None#
observation_prefix: str#

Observation prefix to stop the MRKL at.

property output_keys: List[str]#

Output keys this chain expects.

prompt: BasePromptTemplate#

Prompt for a single response of the LLM.

verbose: bool#