mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 09:55:21 +03:00
Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Davis Chase <130488702+dev2049@users.noreply.github.com> Co-authored-by: Zander Chase <130414180+vowelparrot@users.noreply.github.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
7.4 KiB
7.4 KiB
In [9]:
from langchain.chains import LLMBashChain
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
text = "Please write a bash script that prints 'Hello World' to the console."
bash_chain = LLMBashChain.from_llm(llm, verbose=True)
bash_chain.run(text)Out [9]:
[1m> Entering new LLMBashChain chain...[0m Please write a bash script that prints 'Hello World' to the console.[32;1m[1;3m ```bash echo "Hello World" ```[0m Code: [33;1m[1;3m['echo "Hello World"'][0m Answer: [33;1m[1;3mHello World [0m [1m> Finished chain.[0m
'Hello World\n'
In [10]:
from langchain.prompts.prompt import PromptTemplate
from langchain.chains.llm_bash.prompt import BashOutputParser
_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
```bash
ls
mkdir myNewDirectory
cp -r target/* myNewDirectory
```
Do not use 'echo' when writing the script.
That is the format. Begin!
Question: {question}"""
PROMPT = PromptTemplate(input_variables=["question"], template=_PROMPT_TEMPLATE, output_parser=BashOutputParser())In [11]:
bash_chain = LLMBashChain.from_llm(llm, prompt=PROMPT, verbose=True)
text = "Please write a bash script that prints 'Hello World' to the console."
bash_chain.run(text)Out [11]:
[1m> Entering new LLMBashChain chain...[0m Please write a bash script that prints 'Hello World' to the console.[32;1m[1;3m ```bash printf "Hello World\n" ```[0m Code: [33;1m[1;3m['printf "Hello World\\n"'][0m Answer: [33;1m[1;3mHello World [0m [1m> Finished chain.[0m
'Hello World\n'
In [12]:
from langchain.utilities.bash import BashProcess
persistent_process = BashProcess(persistent=True)
bash_chain = LLMBashChain.from_llm(llm, bash_process=persistent_process, verbose=True)
text = "List the current directory then move up a level."
bash_chain.run(text)Out [12]:
[1m> Entering new LLMBashChain chain...[0m List the current directory then move up a level.[32;1m[1;3m ```bash ls cd .. ```[0m Code: [33;1m[1;3m['ls', 'cd ..'][0m Answer: [33;1m[1;3mapi.ipynb llm_summarization_checker.ipynb constitutional_chain.ipynb moderation.ipynb llm_bash.ipynb openai_openapi.yaml llm_checker.ipynb openapi.ipynb llm_math.ipynb pal.ipynb llm_requests.ipynb sqlite.ipynb[0m [1m> Finished chain.[0m
'api.ipynb\t\t\tllm_summarization_checker.ipynb\r\nconstitutional_chain.ipynb\tmoderation.ipynb\r\nllm_bash.ipynb\t\t\topenai_openapi.yaml\r\nllm_checker.ipynb\t\topenapi.ipynb\r\nllm_math.ipynb\t\t\tpal.ipynb\r\nllm_requests.ipynb\t\tsqlite.ipynb'
In [13]:
# Run the same command again and see that the state is maintained between calls
bash_chain.run(text)Out [13]:
[1m> Entering new LLMBashChain chain...[0m List the current directory then move up a level.[32;1m[1;3m ```bash ls cd .. ```[0m Code: [33;1m[1;3m['ls', 'cd ..'][0m Answer: [33;1m[1;3mexamples getting_started.ipynb index_examples generic how_to_guides.rst[0m [1m> Finished chain.[0m
'examples\t\tgetting_started.ipynb\tindex_examples\r\ngeneric\t\t\thow_to_guides.rst'
In [ ]: