Skip to content

Commit

Permalink
Merge pull request #4 from FingerLiu/feat/any-cmd
Browse files Browse the repository at this point in the history
Feat/any cmd
  • Loading branch information
FingerLiu authored Jun 4, 2023
2 parents d510494 + 22213a5 commit edf0a32
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

```

Expand All @@ -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

Expand All @@ -61,26 +63,26 @@ 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
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
Expand Down
20 changes: 14 additions & 6 deletions text2config/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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')
Expand Down
10 changes: 8 additions & 2 deletions text2config/pkg/openai/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
)
Expand Down
7 changes: 2 additions & 5 deletions text2config/pkg/openai/templates/cli.tpl
Original file line number Diff line number Diff line change
@@ -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}"
4 changes: 3 additions & 1 deletion text2config/pkg/openai/templates/k8s-cmd.tpl
Original file line number Diff line number Diff line change
@@ -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}"
8 changes: 8 additions & 0 deletions text2config/pkg/openai/templates/other-cmd.tpl
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit edf0a32

Please sign in to comment.