ape-linux 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ape/__init__.py +1 -0
- ape/cli.py +61 -0
- ape/llm.py +76 -0
- ape_linux-0.1.0.dist-info/LICENSE +19 -0
- ape_linux-0.1.0.dist-info/METADATA +124 -0
- ape_linux-0.1.0.dist-info/RECORD +8 -0
- ape_linux-0.1.0.dist-info/WHEEL +4 -0
- ape_linux-0.1.0.dist-info/entry_points.txt +3 -0
ape/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""AI for Linux commands."""
|
ape/cli.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
import openai
|
|
5
|
+
import rich.console
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from . import llm
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(add_completion=False, pretty_exceptions_enable=False)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@app.command()
|
|
14
|
+
def run(
|
|
15
|
+
query: Annotated[str, typer.Argument(help="Query describing a Linux task.")],
|
|
16
|
+
model: Annotated[
|
|
17
|
+
str,
|
|
18
|
+
typer.Option(
|
|
19
|
+
help="OpenAI model. See https://platform.openai.com/docs/models.",
|
|
20
|
+
),
|
|
21
|
+
] = "gpt-4o",
|
|
22
|
+
execute: Annotated[
|
|
23
|
+
bool,
|
|
24
|
+
typer.Option(
|
|
25
|
+
help="Run the command if suggested. Dangerous!",
|
|
26
|
+
),
|
|
27
|
+
] = False,
|
|
28
|
+
):
|
|
29
|
+
"""Suggest a command for a Linux task described in QUERY.
|
|
30
|
+
|
|
31
|
+
Example: ape "Create a symbolic link named 'win' pointing to /mnt/c/Users/jdoe"
|
|
32
|
+
|
|
33
|
+
Output : ln -s /mnt/c/Users/jdoe win
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
console = rich.console.Console()
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
with console.status("[bold][blue]Processing ...", spinner="monkey"):
|
|
40
|
+
answer = llm.find_answer(query, model)
|
|
41
|
+
except llm.EmptyQueryError:
|
|
42
|
+
typer.echo("Query cannot be empty.", err=True)
|
|
43
|
+
raise typer.Exit(1)
|
|
44
|
+
except llm.OpenAIAPIStatusError as e:
|
|
45
|
+
typer.echo(
|
|
46
|
+
f"OpenAI {e.status_code} error: {e.message}",
|
|
47
|
+
err=True,
|
|
48
|
+
)
|
|
49
|
+
raise typer.Exit(1)
|
|
50
|
+
except openai.OpenAIError as e:
|
|
51
|
+
typer.echo(f"Generic OpenAI error: {e}", err=True)
|
|
52
|
+
raise typer.Exit(1)
|
|
53
|
+
|
|
54
|
+
if llm.no_answer(answer):
|
|
55
|
+
typer.echo(answer, err=True)
|
|
56
|
+
raise typer.Exit(1)
|
|
57
|
+
|
|
58
|
+
typer.echo(answer)
|
|
59
|
+
|
|
60
|
+
if execute:
|
|
61
|
+
subprocess.check_call(answer, shell=True)
|
ape/llm.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import openai
|
|
2
|
+
|
|
3
|
+
SYSTEM_PROMPT = """\
|
|
4
|
+
You are a Linux command assistant. You will be asked a question about how to perform a task in Linux or Unix-like operating systems. You should only include in your answer the command or commands to perform the task. If you do not know how to perform the task, output "Please rephrase.".
|
|
5
|
+
|
|
6
|
+
Here are a few examples.
|
|
7
|
+
|
|
8
|
+
Question: List all the files and directories in projects in my home directory
|
|
9
|
+
Answer: ls ~/projects
|
|
10
|
+
|
|
11
|
+
Question: What is my username?
|
|
12
|
+
Answer: whoami
|
|
13
|
+
|
|
14
|
+
Question: Find all files with the extension .txt under the current working directory
|
|
15
|
+
Answer: find . -name "*.txt"
|
|
16
|
+
|
|
17
|
+
Question: What is the captial of France?
|
|
18
|
+
Answer: Please rephrase.
|
|
19
|
+
|
|
20
|
+
Question: Tell me a story
|
|
21
|
+
Answer: Please rephrase.""" # noqa: E501
|
|
22
|
+
|
|
23
|
+
USER_PROMPT_TEMPLATE = """\
|
|
24
|
+
Question: {query}
|
|
25
|
+
Answer:"""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class EmptyQueryError(ValueError):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class OpenAIAPIStatusError(Exception):
|
|
33
|
+
def __init__(self, message: str, status_code: int) -> None:
|
|
34
|
+
self.message = message
|
|
35
|
+
self.status_code = status_code
|
|
36
|
+
super().__init__(self.message, self.status_code)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def make_user_prompt(query: str) -> str:
|
|
40
|
+
query = query.strip()
|
|
41
|
+
if query == "":
|
|
42
|
+
raise EmptyQueryError()
|
|
43
|
+
return USER_PROMPT_TEMPLATE.format(query=query)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def call_llm(user_prompt: str, model: str, system_prompt: str) -> str | None:
|
|
47
|
+
client = openai.OpenAI()
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
response = client.chat.completions.create(
|
|
51
|
+
model=model,
|
|
52
|
+
messages=[
|
|
53
|
+
{"role": "system", "content": system_prompt},
|
|
54
|
+
{"role": "user", "content": user_prompt},
|
|
55
|
+
],
|
|
56
|
+
)
|
|
57
|
+
except openai.APIStatusError as e:
|
|
58
|
+
raise OpenAIAPIStatusError(e.response.json()["error"]["message"], e.status_code)
|
|
59
|
+
|
|
60
|
+
answer = response.choices[0].message.content
|
|
61
|
+
return answer
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def find_answer(query: str, model: str) -> str:
|
|
65
|
+
user_prompt = make_user_prompt(query)
|
|
66
|
+
|
|
67
|
+
answer = call_llm(user_prompt, model, SYSTEM_PROMPT)
|
|
68
|
+
|
|
69
|
+
if answer is None:
|
|
70
|
+
return "Please rephrase."
|
|
71
|
+
else:
|
|
72
|
+
return answer # this can also be "Please rephrase."
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def no_answer(answer: str) -> bool:
|
|
76
|
+
return answer.strip().lower().rstrip(".") == "please rephrase"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2024 Seto Balian
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
|
5
|
+
deal in the Software without restriction, including without limitation the
|
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
14
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
15
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
16
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
17
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
18
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
19
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: ape-linux
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI for Linux commands
|
|
5
|
+
Home-page: https://github.com/sbalian/ape
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: ai,llm,linux,openai
|
|
8
|
+
Author: Seto Balian
|
|
9
|
+
Author-email: seto.balian@gmail.com
|
|
10
|
+
Maintainer: Seto Balian
|
|
11
|
+
Maintainer-email: seto.balian@gmail.com
|
|
12
|
+
Requires-Python: >=3.10,<4.0
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Software Development
|
|
21
|
+
Requires-Dist: openai (>=1.30.3,<2.0.0)
|
|
22
|
+
Requires-Dist: rich (>=13.7.1,<14.0.0)
|
|
23
|
+
Requires-Dist: typer (>=0.12.3,<0.13.0)
|
|
24
|
+
Project-URL: Repository, https://github.com/sbalian/ape
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Ape
|
|
28
|
+
|
|
29
|
+
Ape is an AI for Linux commands.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
ape "Find all the important PDF files in user/projects. An important PDF file has 'attention' in its name. Write the results to important_files.txt and then move it to Documents."
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Output:
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
find ~/user/projects -type f -name "*attention*.pdf" > important_files.txt && mv important_files.txt ~/Documents/
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Currently, only [OpenAI](https://openai.com/api/) is supported.
|
|
42
|
+
|
|
43
|
+
To install:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pipx install ape-linux
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Next, set your API key:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
export OPENAI_API_KEY=key
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
To run:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
ape "Create a symbolic link called win pointing to /mnt/c/Users/jdoe"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Output:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
ln -s /mnt/c/Users/jdoe win
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Another example:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
ape "Delete all the .venv directories under projects/"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Output:
|
|
74
|
+
|
|
75
|
+
```text
|
|
76
|
+
find projects/ -type d -name ".venv" -exec rm -rf {} +
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
If you try to ask something unrelated to Linux commands, you should get "Please rephrase." printed:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
ape "Tell me about monkeys"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Output:
|
|
86
|
+
|
|
87
|
+
```text
|
|
88
|
+
Please rephrase.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
You can change the model using `--model`. The default is `gpt-4o`.
|
|
92
|
+
See [here](https://platform.openai.com/docs/models) for a list of models. For example:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
ape "List the contents of the working directory with as much detail as possible" --model gpt-3.5-turbo
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Output:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
ls -lha
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
If you pass `--execute`, the tool will run the command for you after printing it! Be careful with this as LLMs often make mistakes:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
ape "Who am I logged in as?"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Output:
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
whoami
|
|
114
|
+
jdoe
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
For more help:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
ape --help
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
See also: [Gorilla](https://github.com/gorilla-llm)
|
|
124
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
ape/__init__.py,sha256=KJJ5wxvn0U2TzD9o0-eSkT-YNWzBbkED29WB5wWvln0,29
|
|
2
|
+
ape/cli.py,sha256=Gs1cJ9qNGbb1OTmQPCIcLhOvX_zAx3Dp3glokXB5nxM,1588
|
|
3
|
+
ape/llm.py,sha256=TykrpQKNbl9ajmzyGdO8XvK3Gi2yB4eCTkMMxQg4xe4,2201
|
|
4
|
+
ape_linux-0.1.0.dist-info/LICENSE,sha256=qEiuw01rkSrWJjnjUb--8CmLaxsBhWHEsS8mThnZ254,1051
|
|
5
|
+
ape_linux-0.1.0.dist-info/METADATA,sha256=2mSzfzHK3vu8LYja5FzTQ6bMgw24EoobKhm8aD-Dpb0,2550
|
|
6
|
+
ape_linux-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
7
|
+
ape_linux-0.1.0.dist-info/entry_points.txt,sha256=nrgK6S77ZFF--hcHOCuz90ewm1DEXW5uwiWmxcv6pwA,35
|
|
8
|
+
ape_linux-0.1.0.dist-info/RECORD,,
|