Source code for langchain.agents.conversational_chat.base
"""An agent designed to hold a conversation in addition to using tools."""from__future__importannotationsfromtypingimportAny,List,Optional,Sequence,Tuplefromlangchain_core._apiimportdeprecatedfromlangchain_core.agentsimportAgentActionfromlangchain_core.callbacksimportBaseCallbackManagerfromlangchain_core.language_modelsimportBaseLanguageModelfromlangchain_core.messagesimportAIMessage,BaseMessage,HumanMessagefromlangchain_core.output_parsersimportBaseOutputParserfromlangchain_core.promptsimportBasePromptTemplatefromlangchain_core.prompts.chatimport(ChatPromptTemplate,HumanMessagePromptTemplate,MessagesPlaceholder,SystemMessagePromptTemplate,)fromlangchain_core.toolsimportBaseToolfrompydanticimportFieldfromlangchain.agents.agentimportAgent,AgentOutputParserfromlangchain.agents.conversational_chat.output_parserimportConvoOutputParserfromlangchain.agents.conversational_chat.promptimport(PREFIX,SUFFIX,TEMPLATE_TOOL_RESPONSE,)fromlangchain.agents.utilsimportvalidate_tools_single_inputfromlangchain.chainsimportLLMChain
[docs]@deprecated("0.1.0",alternative="create_json_chat_agent",removal="1.0")classConversationalChatAgent(Agent):"""An agent designed to hold a conversation in addition to using tools."""output_parser:AgentOutputParser=Field(default_factory=ConvoOutputParser)"""Output parser for the agent."""template_tool_response:str=TEMPLATE_TOOL_RESPONSE"""Template for the tool response."""@classmethoddef_get_default_output_parser(cls,**kwargs:Any)->AgentOutputParser:returnConvoOutputParser()@propertydef_agent_type(self)->str:raiseNotImplementedError@propertydefobservation_prefix(self)->str:"""Prefix to append the observation with. Returns: "Observation: " """return"Observation: "@propertydefllm_prefix(self)->str:"""Prefix to append the llm call with. Returns: "Thought: " """return"Thought:"@classmethoddef_validate_tools(cls,tools:Sequence[BaseTool])->None:super()._validate_tools(tools)validate_tools_single_input(cls.__name__,tools)
[docs]@classmethoddefcreate_prompt(cls,tools:Sequence[BaseTool],system_message:str=PREFIX,human_message:str=SUFFIX,input_variables:Optional[List[str]]=None,output_parser:Optional[BaseOutputParser]=None,)->BasePromptTemplate:"""Create a prompt for the agent. Args: tools: The tools to use. system_message: The system message to use. Defaults to the PREFIX. human_message: The human message to use. Defaults to the SUFFIX. input_variables: The input variables to use. Defaults to None. output_parser: The output parser to use. Defaults to None. Returns: A PromptTemplate. """tool_strings="\n".join([f"> {tool.name}: {tool.description}"fortoolintools])tool_names=", ".join([tool.namefortoolintools])_output_parser=output_parserorcls._get_default_output_parser()format_instructions=human_message.format(format_instructions=_output_parser.get_format_instructions())final_prompt=format_instructions.format(tool_names=tool_names,tools=tool_strings)ifinput_variablesisNone:input_variables=["input","chat_history","agent_scratchpad"]messages=[SystemMessagePromptTemplate.from_template(system_message),MessagesPlaceholder(variable_name="chat_history"),HumanMessagePromptTemplate.from_template(final_prompt),MessagesPlaceholder(variable_name="agent_scratchpad"),]returnChatPromptTemplate(input_variables=input_variables,messages=messages)# type: ignore[arg-type]
def_construct_scratchpad(self,intermediate_steps:List[Tuple[AgentAction,str]])->List[BaseMessage]:"""Construct the scratchpad that lets the agent continue its thought process."""thoughts:List[BaseMessage]=[]foraction,observationinintermediate_steps:thoughts.append(AIMessage(content=action.log))human_message=HumanMessage(content=self.template_tool_response.format(observation=observation))thoughts.append(human_message)returnthoughts
[docs]@classmethoddeffrom_llm_and_tools(cls,llm:BaseLanguageModel,tools:Sequence[BaseTool],callback_manager:Optional[BaseCallbackManager]=None,output_parser:Optional[AgentOutputParser]=None,system_message:str=PREFIX,human_message:str=SUFFIX,input_variables:Optional[List[str]]=None,**kwargs:Any,)->Agent:"""Construct an agent from an LLM and tools. Args: llm: The language model to use. tools: A list of tools to use. callback_manager: The callback manager to use. Default is None. output_parser: The output parser to use. Default is None. system_message: The system message to use. Default is PREFIX. human_message: The human message to use. Default is SUFFIX. input_variables: The input variables to use. Default is None. **kwargs: Any additional arguments. Returns: An agent. """cls._validate_tools(tools)_output_parser=output_parserorcls._get_default_output_parser()prompt=cls.create_prompt(tools,system_message=system_message,human_message=human_message,input_variables=input_variables,output_parser=_output_parser,)llm_chain=LLMChain(# type: ignore[misc]llm=llm,prompt=prompt,callback_manager=callback_manager,)tool_names=[tool.namefortoolintools]returncls(llm_chain=llm_chain,allowed_tools=tool_names,output_parser=_output_parser,**kwargs,)