# [LangChain] 範例-用自然語言下達Bash指令 日期:2023/09/20 ## 敘述 - 主題:LLMBashChain()、Bash - 使用情境:有時開發程式會需要使用到linux作業系統,如果你對於terminal指令並不熟悉,這時便可透過此程式直接將你的要求轉換成指令,並幫你執行。 - 輸入:希望執行的動作。 - 輸出:將自然語言轉換成Bash指令,並執行此指令。 ※ 這個範例需在linux系統上執行。 ## 目錄 - [1. 主程式](#1-主程式) - [2. 輸入輸出](#2-輸入輸出) - [參考資料](#參考資料) ____________________ ## 1. 主程式 以下為主程式。需將以下程式碼以.py檔儲存,並於terminal中執行。 實現此範例功能主要是透過LLMBashChain()物件。首先會根據輸入生成對應的Bash指令,隨後使用LangChian套件中的BashProcess的run 方法直接執行此Bash 指令([參考資料2](#參考資料))。 ```python= import click from langchain.chains import LLMBashChain from langchain.llms import OpenAI from dotenv import load_dotenv load_dotenv() @click.command() def interact(): llm = OpenAI(temperature=0) bash_chain = LLMBashChain.from_llm(llm, verbose=True) #透過while持續執行定義好的agent。 while True: try: #click.prompt()用於讓使用者可以提供輸入, text = click.prompt('Question: ') bash_chain.run(text) except KeyboardInterrupt: break if __name__ == '__main__': interact() ``` ## 2. 輸入輸出 \<path>替換成你希望進行操作的路徑 ```bash cd <path> ``` 透過以下指令執行程式,之後便可開始輸入你希望執行的動作。 ```bash python /你的文件路徑/command conversion.py ``` **執行結果** 輸入『創建123.txt』。 按下enter後便可發現當前執行路徑中多了123.txt檔案。 :::success Question: : 創建123.txt [1m> Entering new LLMBashChain chain... [0m 創建123.txt [32;1m [1;3m檔案,內容為"Hello World" \```bash echo "Hello World" > 123.txt \``` [0m Code: [33;1m [1;3m['echo "Hello World" > 123.txt'] [0m Answer: [33;1m [1;3m [0m [1m> Finished chain. [0m ::: 其他更多可以嘗試的輸入可參考以下: - 回到上一個目錄 - 安裝git套件 - 解除安裝git套件 - 克隆 https://github.com/username/repository.git ______ ## 參考資料 1. https://python.langchain.com/docs/use_cases/more/code_writing/llm_bash 2. https://api.python.langchain.com/en/latest/chains/langchain.chains.llm_bash.base.LLMBashChain.html
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up