diff --git a/README.md b/README.md index f797909..ce51844 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ export OPENAI_API_KEY="YOUR-KEY" # default to 0 export OPENAI_TEMPERATURE="0" -t2c k8s "get all pod in namespace kube-system and sort by create time" +t2c -e k8s "get all pod name ant create time in namespace kube-system and sort by create time" -t2c docker "run image nginx:latest and mount ~/nginx.conf to /etc/nginx.conf, and also expose 80 to local 8080, remove it after stop" +t2c -e docker "run image nginx:latest and mount ~/nginx.conf to /etc/nginx.conf, and also expose 80 to local 8080, remove it after stop" -t2c cli "list all flies" +t2c "list all flies" ``` @@ -35,22 +35,24 @@ export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_pr ``` # Usage ```bash -usage: t2c [-h] [-m {cmd,config}] [-c CONFIG] [-d] {docker,k8s,kubernetes,kubectl,nginx} goal +usage: t2c [-h] [-m {cmd,config}] [--command COMMAND] [-c CONFIG] [-d] goal Convert natural language text to configuration files(yaml/ini/conf/json) or command of various projects(docker/kubernetes/vim/nginx/postgres/terraform). -Example: t2c k8s "get all pod in namespace kube-system and sort by create time" +Example: t2c -e k8s "get all pod in namespace kube-system and sort by create time" + +t2c "list all files sort by create time" positional arguments: - {docker,k8s,kubernetes,kubectl,nginx,cli} - command name goal goal of the command or config that you want to generate options: -h, --help show this help message and exit -m {cmd,config}, --mode {cmd,config} generate command or config + --command COMMAND, -e COMMAND + command name: docker, k8s, kubernetes, kubectl, nginx, any, ... -c CONFIG, --config CONFIG -d, --debug show debug log @@ -61,7 +63,7 @@ options: - text to k8s yaml - text to nginx conf - chain command to other commands -- text to any command/config with predefined prompt template. +- ✅ text to any command/config with predefined prompt template. - text to custom command/config with user provide prompt template ## text to docker command ```bash @@ -69,18 +71,18 @@ t2c docker "run image nginx and export port 80 to local port 8080" ## text to k8s cmd ```bash -t2c k8s "scale deploy nginx to 3" -t2c k8s -m cmd "get svc in ns which expose domain t2c.io" +t2c -e k8s "scale deploy nginx to 3" +t2c -e kubectl -m cmd "get svc in ns which expose domain t2c.io" ``` ## text to k8s yaml ```bash -t2c k8s --mode config "run nginx deploy and export a ingress with domain t2c.github.io" -t2c k8s -m config "mount a nas pvc to deploy mysql in namespace test" +t2c -e k8s --mode config "run nginx deploy and export a ingress with domain t2c.github.io" +t2c -e k8s -m config "mount a nas pvc to deploy mysql in namespace test" ``` ## text to nginx conf ``` -t2c nginx -c /path/to/nginx.conf add location /docs/ to vhost t2c.io redirect to t2c.github.io +t2c -e nginx -c /path/to/nginx.conf add location /docs/ to vhost t2c.io redirect to t2c.github.io ``` ## Thanks diff --git a/text2config/cli/__init__.py b/text2config/cli/__init__.py index 0902a0e..497b5ce 100644 --- a/text2config/cli/__init__.py +++ b/text2config/cli/__init__.py @@ -7,12 +7,13 @@ parser = argparse.ArgumentParser(description="""Convert natural language text to configuration files(yaml/ini/conf/json) or command of various projects(docker/kubernetes/vim/nginx/postgres/terraform). -Example: t2c k8s "get all pod in namespace kube-system and sort by create time" +Example: t2c -e k8s "get all pod in namespace kube-system and sort by create time" """, formatter_class=RawTextHelpFormatter) parser.add_argument("-m", "--mode", type=str, choices=["cmd", "config"], default="cmd", help="generate command or config") -parser.add_argument("command", type=str, choices=["docker", "k8s", "kubernetes", "kubectl", "nginx","cli"], default="kubectl", help="command name") +parser.add_argument("--command", "-e", type=str, + default="any", help="command name: docker, k8s, kubernetes, kubectl, nginx, any, ...") parser.add_argument("goal", type=str, help="goal of the command or config that you want to generate") parser.add_argument("-c", "--config") parser.add_argument("-d", "--debug", action="store_true", default=False, help="show debug log") @@ -26,12 +27,19 @@ def parse_args(): config = args.config if args.debug: print(f"command goal mode config: {command} {goal} {mode} {config}") - if command in ('k8s', 'kubernetes', 'kubectl'): - command = 'k8s' - prompt_str = f"{command}_{mode}" + if command not in ('docker', 'k8s', 'kubernetes', 'kubectl', 'nginx', 'any'): + cmd_type = 'other' + elif command in ('k8s', 'kubernetes', 'kubectl'): + cmd_type = 'k8s' + else: + cmd_type = command + prompt_str = f"{cmd_type}_{mode}" if hasattr(prompts, prompt_str): promptTemplate: PromptTemplate = getattr(prompts, prompt_str) - prompt = promptTemplate.format(goal=goal) + if cmd_type == 'other': + prompt = promptTemplate.format(goal=goal, cmd=command) + else: + prompt = promptTemplate.format(goal=goal) if args.debug: print(prompt) print(f'{quest(prompt)}\n') diff --git a/text2config/pkg/openai/prompts.py b/text2config/pkg/openai/prompts.py index 61659e1..7ad3059 100644 --- a/text2config/pkg/openai/prompts.py +++ b/text2config/pkg/openai/prompts.py @@ -2,7 +2,7 @@ from langchain.prompts import PromptTemplate # TODO use chain and tools -__all__ = ['docker_cmd', 'k8s_cmd', 'cli_cmd', 'k8s_config', 'nginx_config'] +__all__ = ['docker_cmd', 'k8s_cmd', 'any_cmd', 'other_cmd', 'k8s_config', 'nginx_config'] class PromptTemplateFromFile(PromptTemplate): @@ -22,7 +22,13 @@ def __init__(self, input_variables, template_file): input_variables=["goal"], template_file=pkg_resources.resource_filename("text2config", "pkg/openai/templates/k8s-cmd.tpl") ) -cli_cmd = PromptTemplateFromFile( + +other_cmd = PromptTemplateFromFile( + input_variables=["cmd", "goal"], + template_file=pkg_resources.resource_filename("text2config", "pkg/openai/templates/other-cmd.tpl") +) + +any_cmd = PromptTemplateFromFile( input_variables=["goal"], template_file=pkg_resources.resource_filename("text2config", "pkg/openai/templates/cli.tpl") ) diff --git a/text2config/pkg/openai/templates/cli.tpl b/text2config/pkg/openai/templates/cli.tpl index 3f03538..d8e3af0 100644 --- a/text2config/pkg/openai/templates/cli.tpl +++ b/text2config/pkg/openai/templates/cli.tpl @@ -1,9 +1,6 @@ You are a command line translation program . You can translate natural language instructions from human language into corresponding command line statements. - -Simply output the translated instruction without any explanation. Add the "> " symbol at the beginning of the output. - -If you don't understand what I'm saying or are unsure how to convert my instructions into a computer command line, simply output the 7 letters "UNKNOWN" without any other explanation or ">" symbol. - +Simply output the translated instruction without any explanation. If the translated result consists of more than one line of commands, please use '&' or '&&' to combine them into a single line of command. +You can also use "|" to pipe one command to another command to accomplish your goal. The goal is "{goal}" \ No newline at end of file diff --git a/text2config/pkg/openai/templates/k8s-cmd.tpl b/text2config/pkg/openai/templates/k8s-cmd.tpl index dcc8384..f5ce39b 100644 --- a/text2config/pkg/openai/templates/k8s-cmd.tpl +++ b/text2config/pkg/openai/templates/k8s-cmd.tpl @@ -1,5 +1,7 @@ You are a kubectl(Kubernetes Command line tool) expert, you have strong knowledge of linux, cloud computing and SRE. Start by crafting a kubectl command that is syntactically correct, and then analyze the query results. -Only generate valid kubectl command. Do not provide any explanations, only generate the command. +Try your best to generate valid kubectl command(but if some command is really not supported by kubectl, +you can combine kubectl with other bash command, like grep,xargs,find,cut,sed,echo,base64,sort and so on). +Do not provide any explanations, only generate the command. The goal is "{goal}" diff --git a/text2config/pkg/openai/templates/other-cmd.tpl b/text2config/pkg/openai/templates/other-cmd.tpl new file mode 100644 index 0000000..6b15b40 --- /dev/null +++ b/text2config/pkg/openai/templates/other-cmd.tpl @@ -0,0 +1,8 @@ +You are a linux expert who is familiar with {cmd}, you have strong knowledge of linux, cloud computing and SRE. +Start by crafting a {cmd} command that is syntactically correct, and then analyze the query results. +Try your best to generate valid {cmd} command(but if some command is really not supported by {cmd}, +you can combine kubectl with other bash command, like grep,xargs,find,cut,sed,echo,base64,sort and so on). +If the translated result consists of more than one line of commands, please use '&' or '&&' to combine them into a single line of command. +You can also use "|" to pipe one command to another command to accomplish your goal. + +The goal is "{goal}" \ No newline at end of file