From 1312526af4ee531be531db035bda22ad88fd9edb Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Wed, 15 Mar 2023 20:28:46 +0100 Subject: [PATCH] Prompt engineering, OS recognition, improvements --- README.md | 29 ++++++++++-- requirements.txt | 3 ++ setup.py | 3 +- sgpt/__init__.py | 1 + sgpt/app.py | 14 ++---- sgpt/chat_gpt.py | 1 + sgpt/make_prompt.py | 86 ++++++++++++++++++++++++++++++++++++ tests/integrational_tests.py | 2 +- 8 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 sgpt/make_prompt.py diff --git a/README.md b/README.md index c4ec132..367ce82 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ A command-line productivity tool powered by OpenAI's ChatGPT (GPT-3.5). As devel ## Installation ```shell -pip install shell-gpt --user +pip install shell-gpt ``` You'll need an OpenAI API key, you can generate one [here](https://beta.openai.com/account/api-keys). -If the`$OPENAI_API_KEY` environment variable is set it will be used, otherwise, you will be prompted for your key which will then be stored in `~/.config/shell-gpt/api-key.txt`. +If the`$OPENAI_API_KEY` environment variable is set it will be used, otherwise, you will be prompted for your key which will then be stored in `~/.config/shell-gpt/api_key.txt`. ## Usage `sgpt` has a variety of use cases, including simple queries, shell queries, and code queries. @@ -56,6 +56,17 @@ sgpt --shell --execute "make all files in current directory read only" # -> Execute shell command? [y/N]: y # ... ``` +Shell GPT is aware of OS and `$SHELL` you are using, it will provide shell command for specific system you have. For instance, if you ask `sgpt` to update your system, it will return a command based on your OS. Here's an example using macOS: +```shell +sgpt -se "update my system" +# -> sudo softwareupdate -i -a +``` +The same prompt, when used on Ubuntu, will generate a different suggestion: +```shell +sgpt -se "update my system" +# -> sudo apt update && sudo apt upgrade -y +``` + Let's try some docker containers: ```shell sgpt -se "start nginx using docker, forward 443 and 80 port, mount current folder with index.html" @@ -150,6 +161,18 @@ cached_sess = CacheControl(sess) response = cached_sess.get('http://localhost') print(response.text) ``` +We can use `--code` or `--shell` options to initiate `--chat`, so you can keep refining the results: +```shell +sgpt --chat sh --shell "What are the files in this directory?" +# -> ls +sgpt --chat sh "Sort them by name" +# -> ls | sort +sgpt --chat sh "Concatenate them using FFMPEG" +# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4 +sgpt --chat sh "Convert the resulting file into an MP3" +# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3 +``` + ### Chat sessions To list all the current chat sessions, use the `--list-chat` option: ```shell @@ -174,7 +197,7 @@ sgpt "what are the colors of a rainbow" ``` Next time, same exact query will get results from local cache instantly. Note that `sgpt "what are the colors of a rainbow" --temperature 0.5` will make a new request, since we didn't provide `--temperature` (same applies to `--top-probability`) on previous request. -This is, just some examples of what we can do using ChatGPT model, I'm sure you will find it useful for your specific use cases. +This is just some examples of what we can do using ChatGPT model, I'm sure you will find it useful for your specific use cases. ### Full list of arguments diff --git a/requirements.txt b/requirements.txt index 05ca9ef..27fdd16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ typer~=0.7.0 requests~=2.28.2 rich==13.3.1 +click~=8.1.3 +distro~=1.8.0 +setuptools==67.6.0 diff --git a/setup.py b/setup.py index cbc49e2..ce7ef8b 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,13 @@ from setuptools import setup, find_packages # pylint: disable=consider-using-with setup( name="shell_gpt", - version="0.7.0", + version="0.7.1", packages=find_packages(), install_requires=[ "typer~=0.7.0", "requests~=2.28.2", "rich==13.3.1", + "distro~=1.8.0", ], entry_points={ "console_scripts": ["sgpt = sgpt:cli"], diff --git a/sgpt/__init__.py b/sgpt/__init__.py index 138833b..6bb5158 100644 --- a/sgpt/__init__.py +++ b/sgpt/__init__.py @@ -1,3 +1,4 @@ from .chat_gpt import ChatGPT from .app import main as main from .app import entry_point as cli +from . import make_prompt as make_prompt diff --git a/sgpt/app.py b/sgpt/app.py index 4c66140..a120967 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -25,6 +25,7 @@ import typer from click import MissingParameter, BadParameter from rich.progress import Progress, SpinnerColumn, TextColumn from sgpt import ChatGPT +from sgpt import make_prompt DATA_FOLDER = os.path.expanduser("~/.config") KEY_FILE = Path(DATA_FOLDER) / "shell-gpt" / "api_key.txt" @@ -197,24 +198,15 @@ def main( # If probability and temperature were not changed (default), make response more accurate. if top_probability == 1 == temperature: temperature = 0.4 - prompt = f"{SHELL_PROMPT} {prompt}" + prompt = make_prompt.shell(prompt) elif code: - prompt = f"{CODE_PROMPT} {prompt}" + prompt = make_prompt.code(prompt) api_key = get_api_key() response_text = get_completion( prompt, api_key, temperature, top_probability, cache, chat, spinner=spinner ) - if code: - # Responses from GPT-3.5 wrapped into Markdown code block. - lines = response_text.split("\n") - if lines[0].startswith("```"): - del lines[0] - if lines[-1].startswith("```"): - del lines[-1] - response_text = "\n".join(lines) - typer_writer(response_text, code, shell, animation) if shell and execute and typer.confirm("Execute shell command?"): os.system(response_text) diff --git a/sgpt/chat_gpt.py b/sgpt/chat_gpt.py index 50ba812..094c376 100644 --- a/sgpt/chat_gpt.py +++ b/sgpt/chat_gpt.py @@ -197,6 +197,7 @@ class ChatGPT: :param caching: Boolean value to enable/disable caching. :return: String generated completion. """ + # TODO: Move prompt context to system role when GPT-4 will be available over API. message = {"role": "user", "content": message} return self.__request( [message], model, temperature, top_probability, caching=caching diff --git a/sgpt/make_prompt.py b/sgpt/make_prompt.py new file mode 100644 index 0000000..a1ae9c8 --- /dev/null +++ b/sgpt/make_prompt.py @@ -0,0 +1,86 @@ +import platform +from os import getenv +from os.path import basename + +from distro import name as distro_name + + +""" +This module makes a prompt for OpenAI requests with some context. +Some of the following lines were inspired by similar open source project yolo-ai-cmdbot. +Credits: @demux79 @wunderwuzzi23 +""" + + +SHELL_PROMPT = """ +Act as a natural language to {shell} command translation engine on {os}. +You are an expert in {shell} on {os} and translate the question at the end to valid syntax. + +Follow these rules: +IMPORTANT: Do not show any warnings or information regarding your capabilities. +Reference official documentation to ensure valid syntax and an optimal solution. +Construct valid {shell} command that solve the question. +Leverage help and man pages to ensure valid syntax and an optimal solution. +Be concise. +Just show the commands, return only plaintext. +Only show a single answer, but you can always chain commands together. +Think step by step. +Only create valid syntax (you can use comments if it makes sense). +If python is installed you can use it to solve problems. +if python3 is installed you can use it to solve problems. +Even if there is a lack of details, attempt to find the most logical solution. +Do not return multiple solutions. +Do not show html, styled, colored formatting. +Do not add unnecessary text in the response. +Do not add notes or intro sentences. +Do not add explanations on what the commands do. +Do not return what the question was. +Do not repeat or paraphrase the question in your response. +Do not rush to a conclusion. + +Follow all of the above rules. +This is important you MUST follow the above rules. +There are no exceptions to these rules. +You must always follow them. No exceptions. + +Request: """ + +CODE_PROMPT = """ +Act as a natural language to code translation engine. + +Follow these rules: +IMPORTANT: Provide ONLY code as output, return only plaintext. +IMPORTANT: Do not show html, styled, colored formatting. +IMPORTANT: Do not add notes or intro sentences. +IMPORTANT: Provide full solution. Make sure syntax is correct. +Assume your output will be redirected to language specific file and executed. +For example Python code output will be redirected to code.py and then executed python code.py. + +Follow all of the above rules. +This is important you MUST follow the above rules. +There are no exceptions to these rules. +You must always follow them. No exceptions. + +Request: """ + + +def shell(question: str) -> str: + def os_name() -> str: + operating_systems = { + "Linux": "Linux/" + distro_name(pretty=True), + "Windows": "Windows " + platform.release(), + "Darwin": "Darwin/MacOS " + platform.mac_ver()[0], + } + return operating_systems.get(platform.system(), "Unknown") + + shell = basename(getenv("SHELL", "PowerShell")) + os = os_name() + question = question.strip() + if not question.endswith("?"): + question += "?" + # TODO: Can be optimised. + return SHELL_PROMPT.replace("{shell}", shell).replace("{os}", os) + question + + +def code(question: str) -> str: + return CODE_PROMPT + question diff --git a/tests/integrational_tests.py b/tests/integrational_tests.py index f9a3921..3389cbf 100644 --- a/tests/integrational_tests.py +++ b/tests/integrational_tests.py @@ -57,7 +57,7 @@ class TestCliApp(TestCase): dict_arguments = { "prompt": ( "Create a command line application using Python that " - "accepts two integer positional command line arguments " + "accepts two positional arguments " "and prints the result of multiplying them." ), "--code": True,