mirror of
https://github.com/hwchase17/langchain.git
synced 2025-10-08 23:59:17 +00:00
.devcontainer
.github
cookbook
autogpt
data
video_captioning
Gemma_LangChain.ipynb
LLaMA2_sql_chat.ipynb
Multi_modal_RAG.ipynb
Multi_modal_RAG_google.ipynb
RAPTOR.ipynb
README.md
Semi_Structured_RAG.ipynb
Semi_structured_and_multi_modal_RAG.ipynb
Semi_structured_multi_modal_RAG_LLaMA2.ipynb
advanced_rag_eval.ipynb
agent_vectorstore.ipynb
airbyte_github.ipynb
amazon_personalize_how_to.ipynb
analyze_document.ipynb
anthropic_structured_outputs.ipynb
apache_kafka_message_handling.ipynb
baby_agi.ipynb
baby_agi_with_agent.ipynb
camel_role_playing.ipynb
causal_program_aided_language_model.ipynb
code-analysis-deeplake.ipynb
cql_agent.ipynb
custom_agent_with_plugin_retrieval.ipynb
custom_agent_with_plugin_retrieval_using_plugnplai.ipynb
custom_agent_with_tool_retrieval.ipynb
custom_multi_action_agent.ipynb
databricks_sql_db.ipynb
deeplake_semantic_search_over_chat.ipynb
docugami_xml_kg_rag.ipynb
elasticsearch_db_qa.ipynb
extraction_openai_tools.ipynb
fake_llm.ipynb
fireworks_rag.ipynb
forward_looking_retrieval_augmented_generation.ipynb
generative_agents_interactive_simulacra_of_human_behavior.ipynb
gymnasium_agent_simulation.ipynb
hugginggpt.ipynb
human_approval.ipynb
human_input_chat_model.ipynb
human_input_llm.ipynb
hypothetical_document_embeddings.ipynb
langgraph_agentic_rag.ipynb
langgraph_crag.ipynb
langgraph_self_rag.ipynb
learned_prompt_optimization.ipynb
llm_bash.ipynb
llm_checker.ipynb
llm_math.ipynb
llm_summarization_checker.ipynb
llm_symbolic_math.ipynb
meta_prompt.ipynb
mongodb-langchain-cache-memory.ipynb
multi_modal_QA.ipynb
multi_modal_RAG_chroma.ipynb
multi_modal_RAG_vdms.ipynb
multi_modal_output_agent.ipynb
multi_player_dnd.ipynb
multiagent_authoritarian.ipynb
multiagent_bidding.ipynb
myscale_vector_sql.ipynb
nomic_embedding_rag.ipynb
openai_functions_retrieval_qa.ipynb
openai_v1_cookbook.ipynb
optimization.ipynb
oracleai_demo.ipynb
petting_zoo.ipynb
plan_and_execute_agent.ipynb
press_releases.ipynb
program_aided_language_model.ipynb
qa_citations.ipynb
qianfan_baidu_elasticesearch_RAG.ipynb
rag_fusion.ipynb
rag_semantic_chunking_azureaidocintelligence.ipynb
rag_upstage_layout_analysis_groundedness_check.ipynb
rag_with_quantized_embeddings.ipynb
retrieval_in_sql.ipynb
rewrite.ipynb
sales_agent_with_context.ipynb
selecting_llms_based_on_context_length.ipynb
self-discover.ipynb
self_query_hotel_search.ipynb
sharedmemory_for_tools.ipynb
smart_llm.ipynb
sql_db_qa.mdx
stepback-qa.ipynb
together_ai.ipynb
tool_call_messages.ipynb
tree_of_thought.ipynb
twitter-the-algorithm-analysis-deeplake.ipynb
two_agent_debate_tools.ipynb
two_player_dnd.ipynb
wikibase_agent.ipynb
docker
docs
libs
templates
.gitattributes
.gitignore
.readthedocs.yaml
CITATION.cff
LICENSE
MIGRATE.md
Makefile
README.md
SECURITY.md
poetry.lock
poetry.toml
pyproject.toml
This path updates function "run" to "invoke" in llm_bash.ipynb. Without this path, you see following warning. LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead. Signed-off-by: Masanari Iida <standby24x7@gmail.com>
260 lines
7.1 KiB
Plaintext
260 lines
7.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Bash chain\n",
|
|
"This notebook showcases using LLMs and a bash process to perform simple filesystem commands."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
|
|
"Please write a bash script that prints 'Hello World' to the console.\u001b[32;1m\u001b[1;3m\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"echo \"Hello World\"\n",
|
|
"```\u001b[0m\n",
|
|
"Code: \u001b[33;1m\u001b[1;3m['echo \"Hello World\"']\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
|
|
"\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Hello World\\n'"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from langchain_experimental.llm_bash.base import LLMBashChain\n",
|
|
"from langchain_openai import OpenAI\n",
|
|
"\n",
|
|
"llm = OpenAI(temperature=0)\n",
|
|
"\n",
|
|
"text = \"Please write a bash script that prints 'Hello World' to the console.\"\n",
|
|
"\n",
|
|
"bash_chain = LLMBashChain.from_llm(llm, verbose=True)\n",
|
|
"\n",
|
|
"bash_chain.invoke(text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Customize Prompt\n",
|
|
"You can also customize the prompt that is used. Here is an example prompting to avoid using the 'echo' utility"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.prompts.prompt import PromptTemplate\n",
|
|
"from langchain_experimental.llm_bash.prompt import BashOutputParser\n",
|
|
"\n",
|
|
"_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:\n",
|
|
"Question: \"copy the files in the directory named 'target' into a new directory at the same level as target called 'myNewDirectory'\"\n",
|
|
"I need to take the following actions:\n",
|
|
"- List all files in the directory\n",
|
|
"- Create a new directory\n",
|
|
"- Copy the files from the first directory into the second directory\n",
|
|
"```bash\n",
|
|
"ls\n",
|
|
"mkdir myNewDirectory\n",
|
|
"cp -r target/* myNewDirectory\n",
|
|
"```\n",
|
|
"\n",
|
|
"Do not use 'echo' when writing the script.\n",
|
|
"\n",
|
|
"That is the format. Begin!\n",
|
|
"Question: {question}\"\"\"\n",
|
|
"\n",
|
|
"PROMPT = PromptTemplate(\n",
|
|
" input_variables=[\"question\"],\n",
|
|
" template=_PROMPT_TEMPLATE,\n",
|
|
" output_parser=BashOutputParser(),\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
|
|
"Please write a bash script that prints 'Hello World' to the console.\u001b[32;1m\u001b[1;3m\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"printf \"Hello World\\n\"\n",
|
|
"```\u001b[0m\n",
|
|
"Code: \u001b[33;1m\u001b[1;3m['printf \"Hello World\\\\n\"']\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
|
|
"\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Hello World\\n'"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"bash_chain = LLMBashChain.from_llm(llm, prompt=PROMPT, verbose=True)\n",
|
|
"\n",
|
|
"text = \"Please write a bash script that prints 'Hello World' to the console.\"\n",
|
|
"\n",
|
|
"bash_chain.invoke(text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Persistent Terminal\n",
|
|
"\n",
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
|
|
"List the current directory then move up a level.\u001b[32;1m\u001b[1;3m\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"ls\n",
|
|
"cd ..\n",
|
|
"```\u001b[0m\n",
|
|
"Code: \u001b[33;1m\u001b[1;3m['ls', 'cd ..']\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3mcpal.ipynb llm_bash.ipynb llm_symbolic_math.ipynb\n",
|
|
"index.mdx llm_math.ipynb pal.ipynb\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'cpal.ipynb llm_bash.ipynb llm_symbolic_math.ipynb\\r\\nindex.mdx llm_math.ipynb pal.ipynb'"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from langchain_experimental.llm_bash.bash import BashProcess\n",
|
|
"\n",
|
|
"persistent_process = BashProcess(persistent=True)\n",
|
|
"bash_chain = LLMBashChain.from_llm(llm, bash_process=persistent_process, verbose=True)\n",
|
|
"\n",
|
|
"text = \"List the current directory then move up a level.\"\n",
|
|
"\n",
|
|
"bash_chain.invoke(text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
|
|
"List the current directory then move up a level.\u001b[32;1m\u001b[1;3m\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"ls\n",
|
|
"cd ..\n",
|
|
"```\u001b[0m\n",
|
|
"Code: \u001b[33;1m\u001b[1;3m['ls', 'cd ..']\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3m_category_.yml\tdata_generation.ipynb\t\t self_check\n",
|
|
"agents\t\tgraph\n",
|
|
"code_writing\tlearned_prompt_optimization.ipynb\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'_category_.yml\\tdata_generation.ipynb\\t\\t self_check\\r\\nagents\\t\\tgraph\\r\\ncode_writing\\tlearned_prompt_optimization.ipynb'"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# Run the same command again and see that the state is maintained between calls\n",
|
|
"bash_chain.invoke(text)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|