Source code for langchain_experimental.llm_bash.prompt
# flake8: noqafrom__future__importannotationsimportrefromtypingimportListfromlangchain_core.prompts.promptimportPromptTemplatefromlangchain_core.output_parsersimportBaseOutputParserfromlangchain_core.exceptionsimportOutputParserException_PROMPT_TEMPLATE="""If someone asks you to perform a task, your job is to come up with a series of bash commands that will perform the task. There is no need to put "#!/bin/bash" in your answer. Make sure to reason step by step, using this format:Question: "copy the files in the directory named 'target' into a new directory at the same level as target called 'myNewDirectory'"I need to take the following actions:- List all files in the directory- Create a new directory- Copy the files from the first directory into the second directory```bashlsmkdir myNewDirectorycp -r target/* myNewDirectory```That is the format. Begin!Question: {question}"""
[docs]classBashOutputParser(BaseOutputParser):"""Parser for bash output."""
[docs]defparse(self,text:str)->List[str]:"""Parse the output of a bash command."""if"```bash"intext:returnself.get_code_blocks(text)else:raiseOutputParserException(f"Failed to parse bash output. Got: {text}",)
[docs]@staticmethoddefget_code_blocks(t:str)->List[str]:"""Get multiple code blocks from the LLM result."""code_blocks:List[str]=[]# Bash markdown code blockspattern=re.compile(r"```bash(.*?)(?:\n\s*)```",re.DOTALL)formatchinpattern.finditer(t):matched=match.group(1).strip()ifmatched:code_blocks.extend([lineforlineinmatched.split("\n")ifline.strip()])returncode_blocks