Skip to content

Commit ee670c4

Browse files
Persistent Bash Shell (langchain-ai#3580)
Clean up linting and make more idiomatic by using an output parser --------- Co-authored-by: FergusFettes <fergusfettes@gmail.com>
1 parent c5451f4 commit ee670c4

File tree

6 files changed

+467
-40
lines changed

6 files changed

+467
-40
lines changed

docs/modules/agents/tools/examples/bash.ipynb

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@
3939
"name": "stdout",
4040
"output_type": "stream",
4141
"text": [
42+
"apify.ipynb\n",
43+
"arxiv.ipynb\n",
4244
"bash.ipynb\n",
45+
"bing_search.ipynb\n",
46+
"chatgpt_plugins.ipynb\n",
47+
"ddg.ipynb\n",
48+
"google_places.ipynb\n",
4349
"google_search.ipynb\n",
50+
"google_serper.ipynb\n",
51+
"gradio_tools.ipynb\n",
52+
"human_tools.ipynb\n",
53+
"ifttt.ipynb\n",
54+
"openweathermap.ipynb\n",
4455
"python.ipynb\n",
4556
"requests.ipynb\n",
57+
"search_tools.ipynb\n",
58+
"searx_search.ipynb\n",
4659
"serpapi.ipynb\n",
60+
"wikipedia.ipynb\n",
61+
"wolfram_alpha.ipynb\n",
62+
"zapier.ipynb\n",
4763
"\n"
4864
]
4965
}
@@ -54,9 +70,94 @@
5470
},
5571
{
5672
"cell_type": "code",
57-
"execution_count": null,
73+
"execution_count": 4,
74+
"id": "e7896f8e",
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"apify.ipynb\n",
82+
"arxiv.ipynb\n",
83+
"bash.ipynb\n",
84+
"bing_search.ipynb\n",
85+
"chatgpt_plugins.ipynb\n",
86+
"ddg.ipynb\n",
87+
"google_places.ipynb\n",
88+
"google_search.ipynb\n",
89+
"google_serper.ipynb\n",
90+
"gradio_tools.ipynb\n",
91+
"human_tools.ipynb\n",
92+
"ifttt.ipynb\n",
93+
"openweathermap.ipynb\n",
94+
"python.ipynb\n",
95+
"requests.ipynb\n",
96+
"search_tools.ipynb\n",
97+
"searx_search.ipynb\n",
98+
"serpapi.ipynb\n",
99+
"wikipedia.ipynb\n",
100+
"wolfram_alpha.ipynb\n",
101+
"zapier.ipynb\n",
102+
"\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"bash.run(\"cd ..\")\n",
108+
"# The commands are executed in a new subprocess each time, meaning that\n",
109+
"# this call will return the same results as the last.\n",
110+
"print(bash.run(\"ls\"))"
111+
]
112+
},
113+
{
114+
"attachments": {},
115+
"cell_type": "markdown",
58116
"id": "851fee9f",
59117
"metadata": {},
118+
"source": [
119+
"## Terminal Persistance\n",
120+
"\n",
121+
"By default, the bash command will be executed in a new subprocess each time. To retain a persistent bash session, we can use the `persistent=True` arg."
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 5,
127+
"id": "4a93ea2c",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"bash = BashProcess(persistent=True)"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 6,
137+
"id": "a1e98b78",
138+
"metadata": {},
139+
"outputs": [
140+
{
141+
"name": "stdout",
142+
"output_type": "stream",
143+
"text": [
144+
"custom_tools.ipynb\t\tmulti_input_tool.ipynb\n",
145+
"examples\t\t\ttool_input_validation.ipynb\n",
146+
"getting_started.md\n"
147+
]
148+
}
149+
],
150+
"source": [
151+
"bash.run(\"cd ..\")\n",
152+
"# Note the list of files is different\n",
153+
"print(bash.run(\"ls\"))"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"id": "e13c1c9c",
160+
"metadata": {},
60161
"outputs": [],
61162
"source": []
62163
}
@@ -77,7 +178,7 @@
77178
"name": "python",
78179
"nbconvert_exporter": "python",
79180
"pygments_lexer": "ipython3",
80-
"version": "3.10.9"
181+
"version": "3.8.16"
81182
}
82183
},
83184
"nbformat": 4,

docs/modules/chains/examples/llm_bash.ipynb

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"\n",
2525
"```bash\n",
2626
"echo \"Hello World\"\n",
27-
"```\u001b[0m['```bash', 'echo \"Hello World\"', '```']\n",
28-
"\n",
27+
"```\u001b[0m\n",
28+
"Code: \u001b[33;1m\u001b[1;3m['echo \"Hello World\"']\u001b[0m\n",
2929
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
3030
"\u001b[0m\n",
3131
"\u001b[1m> Finished chain.\u001b[0m\n"
@@ -65,7 +65,7 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 28,
68+
"execution_count": 2,
6969
"metadata": {},
7070
"outputs": [],
7171
"source": [
@@ -93,7 +93,7 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 29,
96+
"execution_count": 3,
9797
"metadata": {},
9898
"outputs": [
9999
{
@@ -107,8 +107,8 @@
107107
"\n",
108108
"```bash\n",
109109
"printf \"Hello World\\n\"\n",
110-
"```\u001b[0m['```bash', 'printf \"Hello World\\\\n\"', '```']\n",
111-
"\n",
110+
"```\u001b[0m\n",
111+
"Code: \u001b[33;1m\u001b[1;3m['printf \"Hello World\\\\n\"']\u001b[0m\n",
112112
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
113113
"\u001b[0m\n",
114114
"\u001b[1m> Finished chain.\u001b[0m\n"
@@ -120,7 +120,7 @@
120120
"'Hello World\\n'"
121121
]
122122
},
123-
"execution_count": 29,
123+
"execution_count": 3,
124124
"metadata": {},
125125
"output_type": "execute_result"
126126
}
@@ -132,6 +132,114 @@
132132
"\n",
133133
"bash_chain.run(text)"
134134
]
135+
},
136+
{
137+
"attachments": {},
138+
"cell_type": "markdown",
139+
"metadata": {},
140+
"source": [
141+
"## Persistent Terminal\n",
142+
"\n",
143+
"By default, the chain will run in a separate subprocess each time it is called. This behavior can be changed by instantiating with a persistent bash process."
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 4,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"\n",
156+
"\n",
157+
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
158+
"List the current directory then move up a level.\u001b[32;1m\u001b[1;3m\n",
159+
"\n",
160+
"```bash\n",
161+
"ls\n",
162+
"cd ..\n",
163+
"```\u001b[0m\n",
164+
"Code: \u001b[33;1m\u001b[1;3m['ls', 'cd ..']\u001b[0m\n",
165+
"Answer: \u001b[33;1m\u001b[1;3mapi.ipynb\t\t\tllm_summarization_checker.ipynb\n",
166+
"constitutional_chain.ipynb\tmoderation.ipynb\n",
167+
"llm_bash.ipynb\t\t\topenai_openapi.yaml\n",
168+
"llm_checker.ipynb\t\topenapi.ipynb\n",
169+
"llm_math.ipynb\t\t\tpal.ipynb\n",
170+
"llm_requests.ipynb\t\tsqlite.ipynb\u001b[0m\n",
171+
"\u001b[1m> Finished chain.\u001b[0m\n"
172+
]
173+
},
174+
{
175+
"data": {
176+
"text/plain": [
177+
"'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'"
178+
]
179+
},
180+
"execution_count": 4,
181+
"metadata": {},
182+
"output_type": "execute_result"
183+
}
184+
],
185+
"source": [
186+
"from langchain.utilities.bash import BashProcess\n",
187+
"\n",
188+
"\n",
189+
"persistent_process = BashProcess(persistent=True)\n",
190+
"bash_chain = LLMBashChain.from_bash_process(llm=llm, bash_process=persistent_process, verbose=True)\n",
191+
"\n",
192+
"text = \"List the current directory then move up a level.\"\n",
193+
"\n",
194+
"bash_chain.run(text)"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 5,
200+
"metadata": {},
201+
"outputs": [
202+
{
203+
"name": "stdout",
204+
"output_type": "stream",
205+
"text": [
206+
"\n",
207+
"\n",
208+
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
209+
"List the current directory then move up a level.\u001b[32;1m\u001b[1;3m\n",
210+
"\n",
211+
"```bash\n",
212+
"ls\n",
213+
"cd ..\n",
214+
"```\u001b[0m\n",
215+
"Code: \u001b[33;1m\u001b[1;3m['ls', 'cd ..']\u001b[0m\n",
216+
"Answer: \u001b[33;1m\u001b[1;3mexamples\t\tgetting_started.ipynb\tindex_examples\n",
217+
"generic\t\t\thow_to_guides.rst\u001b[0m\n",
218+
"\u001b[1m> Finished chain.\u001b[0m\n"
219+
]
220+
},
221+
{
222+
"data": {
223+
"text/plain": [
224+
"'examples\\t\\tgetting_started.ipynb\\tindex_examples\\r\\ngeneric\\t\\t\\thow_to_guides.rst'"
225+
]
226+
},
227+
"execution_count": 5,
228+
"metadata": {},
229+
"output_type": "execute_result"
230+
}
231+
],
232+
"source": [
233+
"# Run the same command again and see that the state is maintained between calls\n",
234+
"bash_chain.run(text)"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": []
135243
}
136244
],
137245
"metadata": {
@@ -150,7 +258,7 @@
150258
"name": "python",
151259
"nbconvert_exporter": "python",
152260
"pygments_lexer": "ipython3",
153-
"version": "3.10.6"
261+
"version": "3.8.16"
154262
}
155263
},
156264
"nbformat": 4,

0 commit comments

Comments
 (0)