[V04] Shortcuts for --shell --execute, colorful output, minor improvements

This commit is contained in:
Farkhod Sadykov
2023-02-07 19:16:44 +01:00
committed by Farkhod Sadykov
parent 243ba6238b
commit 8304227beb
5 changed files with 41 additions and 45 deletions
+2
View File
@@ -0,0 +1,2 @@
*.egg-info
dist
+17 -17
View File
@@ -1,12 +1,12 @@
# Shell GPT
A command-line interface (CLI) productivity tool for developers powered by OpenAI's GPT-3, will help you accomplish your tasks faster and more efficiently.
A command-line interface (CLI) productivity tool powered by OpenAI's Davinci model, will help you accomplish your tasks faster and more efficiently.
<div align="center">
<img src="https://i.ibb.co/zVm2YYc/output-video-2.gif" width="800"/>
<img src="https://i.ibb.co/VHVSSLL/v2-final-3.gif" width="800"/>
</div>
## Description
Chat GPT is a powerful language model developed by OpenAI that can generate human-like text. It can be used by us, coders, to generate code snippets, comments, documentation and more, helping us increase our productivity and efficiency while coding.
`text-davinci-003` is a powerful language model developed by OpenAI that can generate human-like text. It can be used by us, coders, to generate code snippets, comments, documentation and more, helping us increase our productivity and efficiency while coding.
Forget about cheat sheets and notes, with this tool you can get accurate answers right in your terminal, and you'll probably find yourself reducing your daily Google searches, saving you valuable time and effort.
@@ -34,23 +34,23 @@ Usually we are forgetting commands like `chmod 444` and we want quickly find the
sgpt --shell "make all files in current directory read only"
# -> chmod 444 *
```
Since we are receiving valid shell command, we can execute it using `eval $(sgpt --shell "make all files in current directory read only")` but this is not very convenient, instead we can use `--execute` parameter:
Since we are receiving valid shell command, we can execute it using `eval $(sgpt --shell "make all files in current directory read only")` but this is not very convenient, instead we can use `--execute` (or shortcut `-se` for `--shell` `--execute`) parameter:
```shell
sgpt --shell --execute "make all files in current directory read only"
# -> chmod 444 *
# -> Execute shell command? [y/N]: y
# ...
```
At this point it is already can solve half of my Google searches, but how far we can push the limits of Chat GPT? Let's try some docker containers:
At this point it is already can solve half of my Google searches, but how far we can push the limits of `text-davinci-003` model? Let's try some docker containers:
```shell
sgpt --shell --execute "start nginx using docker, forward 443 and 80 port, mount current folder with index.html"
sgpt -se "start nginx using docker, forward 443 and 80 port, mount current folder with index.html"
# -> docker run -d -p 443:443 -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
# -> Execute shell command? [y/N]: y
# ...
```
Also, we can provide some parameters name in our prompt, for example, I want to pass input and output file names to ffmpeg:
```shell
sgpt --shell --execute "slow down video twice using ffmpeg, input video name \"input.mp4\" output video name \"output.mp4\""
sgpt -se "slow down video twice using ffmpeg, input video name \"input.mp4\" output video name \"output.mp4\""
# -> ffmpeg -i input.mp4 -filter:v "setpts=2.0*PTS" output.mp4
# -> Execute shell command? [y/N]: y
# ...
@@ -59,12 +59,12 @@ And remember we are in shell, this means we can use outputs of any commands in o
```shell
ls
# -> 1.mp4 2.mp4 3.mp4
sgpt --shell --execute "using ffmpeg combine multiple videos into one without audio. Video file names $(ls)"
sgpt -se "using ffmpeg combine multiple videos into one without audio. Video file names $(ls)"
# -> ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v] [1:v] [2:v] concat=n=3:v=1 [v]" -map "[v]" out.mp4
# -> Execute shell command? [y/N]: y
# ...
```
Since GPT-3 models can also do summarization and analyzing of input text, we can ask Chat GPT to find error in logs and provide some details:
Since GPT-3 models can also do summarization and analyzing of input text, we can ask `text-davinci-003` to find error in logs and provide some details:
```shell
sgpt "check these logs, find errors, and explain what the error is about: ${docker logs -n 20 container_name}"
# ...
@@ -101,12 +101,12 @@ This is, just some examples of what we can do using GPT-3 models, I'm sure you w
### Full list of arguments
```shell
--model TEXT OpenAI model name. [default: text-davinci-003]
--max-tokens INTEGER Strict length of output (words). [default: 2048]
--shell --no-shell Get shell command as output. [default: no-shell]
--execute --no-execute Used with --shell, will execute command. [default: no-execute]
--code --no-code Provide only code as output. [default: no-code]
--spinner --no-spinner Show loading spinner during API request. [default: spinner]
--animation --no-animation Typewriter animation. [default: animation]
--help Show this message and exit.
--model TEXT OpenAI model name. [default: text-davinci-003]
--max-tokens INTEGER Strict length of output (words). [default: 2048]
--shell -s Provide shell command as output.
--execute -e Used with --shell, will execute command.
--code --no-code Provide code as output. [default: no-code]
--animation --no-animation Typewriter animation. [default: animation]
--spinner --no-spinner Show loading spinner during API request. [default: spinner]
--help Show this message and exit.
```
+1 -1
View File
@@ -1,3 +1,3 @@
typer~=0.7.0
requests~=2.28.2
rich==13.1.0
rich==13.3.1
+2 -2
View File
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="shell_gpt",
version="0.3",
version="0.4",
packages=find_packages(),
py_modules=[
"sgpt",
@@ -11,7 +11,7 @@ setup(
install_requires=[
"typer~=0.7.0",
"requests~=2.28.2",
"rich==13.1.0",
"rich==13.3.1",
],
entry_points={
'console_scripts': [
+19 -25
View File
@@ -9,9 +9,7 @@ import requests
from utils import loading_spinner
API_URL = "https://api.openai.com/v1/completions"
SECRET_KEY_URL = "https://beta.openai.com/account/api-keys"
# OS-specific app data folder
app_data_folder = os.path.expanduser('~/.config')
api_key_file = Path(app_data_folder) / "shell-gpt" / "api_key.txt"
@@ -38,47 +36,43 @@ def openai_request(prompt, model, max_tokens, api_key):
"max_tokens": max_tokens,
}
response = requests.post(API_URL, headers=headers, json=data)
if response.status_code != 200:
response_error = response.json().get("error", {}).get("message")
response_error = response_error or response.json()
message = f"Unexpected response from OpenAI API: {response_error}"
typer.echo(message, err=True)
raise typer.Exit(code=1)
response.raise_for_status()
return response.json()["choices"][0]["text"]
def typer_writer(text):
for char in text:
typer.echo(char, nl=False)
sleep(0.015)
# Add new line at the end, to prevent % from appearing.
typer.echo("")
def typer_writer(text, code, shell, animate):
shell_or_code = shell or code
color = "magenta" if shell_or_code else None
# typer.colors.
if animate and not shell_or_code:
for char in text:
typer.secho(char, nl=False, fg=color, bold=shell_or_code)
sleep(0.015)
# Add new line at the end, to prevent % from appearing.
typer.echo("")
return
typer.secho(text, fg=color, bold=shell_or_code)
def main(
prompt: str,
model: str = typer.Option("text-davinci-003", help="OpenAI model name."),
max_tokens: int = typer.Option(2048, help="Strict length of output (words)."),
shell: bool = typer.Option(False, help="Get shell command as output."),
execute: bool = typer.Option(False, help="Used with --shell, will execute command."),
code: bool = typer.Option(False, help="Provide only code as output."),
spinner: bool = typer.Option(True, help="Show loading spinner during API request."),
shell: bool = typer.Option(False, "--shell", "-s", help="Provide shell command as output."),
execute: bool = typer.Option(False, "--execute", "-e", help="Used with --shell, will execute command."),
code: bool = typer.Option(False, help="Provide code as output."),
animation: bool = typer.Option(True, help="Typewriter animation."),
spinner: bool = typer.Option(True, help="Show loading spinner during API request."),
):
api_key = get_api_key()
if shell:
prompt = f"{prompt}. Provide only shell command as output."
if code:
elif code:
prompt = f"{prompt}. Provide only code as output."
response_text = openai_request(prompt, model, max_tokens, api_key, spinner=spinner)
# For some reason OpenAI returns several leading/trailing white spaces.
response_text = response_text.strip()
if animation and not (code or shell):
typer_writer(response_text)
else:
typer.echo(response_text)
typer_writer(response_text, code, shell, animation)
if shell and execute and typer.confirm("Execute shell command?"):
os.system(response_text)