Last active
June 5, 2023 07:48
-
-
Save sweemeng/de063d787265b87fc5834c40eb823d04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import subprocess | |
from langchain.prompts import PromptTemplate | |
from langchain.llms import OpenAI | |
from langchain.chains import LLMChain | |
from dotenv import load_dotenv | |
load_dotenv() | |
def main(): | |
parser = argparse.ArgumentParser(description="Translate a task to a shell command.") | |
parser.add_argument("tasks", help="Tasks to translate to shell commands.") | |
# add optional argument --run for parser | |
parser.add_argument("-r", "--run", default=False, action="store_true", help="Run the shell command.") | |
args = parser.parse_args() | |
llm = OpenAI(temperature=0) | |
template = "Show me the shell command to do {task}. The command is:" | |
prompt = PromptTemplate( | |
input_variables=["task"], | |
template=template, | |
) | |
chain = LLMChain(llm=llm, prompt=prompt) | |
output = chain.run(task=args.tasks) | |
if args.run: | |
subprocess.run(output, shell=True) | |
else: | |
print(output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment