System role optimisations for GPT4+ models (#398)

This commit is contained in:
Farkhod Sadykov
2023-12-19 16:53:15 +01:00
committed by GitHub
parent 0a6fbe561c
commit c0fc39dc4f
8 changed files with 110 additions and 162 deletions
-4
View File
@@ -341,8 +341,6 @@ REQUEST_TIMEOUT=60
DEFAULT_MODEL=gpt-3.5-turbo
# Default color for OpenAI completions.
DEFAULT_COLOR=magenta
# Force use system role messages (not recommended).
SYSTEM_ROLES=false
# When in --shell mode, default to "Y" for no input.
DEFAULT_EXECUTE_SHELL_CMD=false
# Disable streaming of responses
@@ -350,8 +348,6 @@ DISABLE_STREAMING=false
```
Possible options for `DEFAULT_COLOR`: black, red, green, yellow, blue, magenta, cyan, white, bright_black, bright_red, bright_green, bright_yellow, bright_blue, bright_magenta, bright_cyan, bright_white.
Switch `SYSTEM_ROLES` to force use [system roles](https://help.openai.com/en/articles/7042661-chatgpt-api-transition-guide) messages, this is not recommended, since it doesn't perform well with current GPT models.
### Full list of arguments
```text
╭─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────────────╮
+3 -3
View File
@@ -30,14 +30,14 @@ def main(
help="Large language model to use.",
),
temperature: float = typer.Option(
0.1,
0.0,
min=0.0,
max=2.0,
help="Randomness of generated output.",
),
top_probability: float = typer.Option(
1.0,
min=0.1,
min=0.0,
max=1.0,
help="Limits highest probable tokens (words).",
),
@@ -88,7 +88,7 @@ def main(
show_chat: str = typer.Option(
None,
help="Show all messages from provided chat id.",
callback=ChatHandler.show_messages_callback,
callback=ChatHandler.show_messages,
rich_help_panel="Chat Options",
),
list_chats: bool = typer.Option(
+1 -2
View File
@@ -21,11 +21,10 @@ DEFAULT_CONFIG = {
"CHAT_CACHE_LENGTH": int(os.getenv("CHAT_CACHE_LENGTH", "100")),
"CACHE_LENGTH": int(os.getenv("CHAT_CACHE_LENGTH", "100")),
"REQUEST_TIMEOUT": int(os.getenv("REQUEST_TIMEOUT", "60")),
"DEFAULT_MODEL": os.getenv("DEFAULT_MODEL", "gpt-3.5-turbo"),
"DEFAULT_MODEL": os.getenv("DEFAULT_MODEL", "gpt-4-1106-preview"),
"OPENAI_API_HOST": os.getenv("OPENAI_API_HOST", "https://api.openai.com"),
"DEFAULT_COLOR": os.getenv("DEFAULT_COLOR", "magenta"),
"ROLE_STORAGE_PATH": os.getenv("ROLE_STORAGE_PATH", str(ROLE_STORAGE_PATH)),
"SYSTEM_ROLES": os.getenv("SYSTEM_ROLES", "false"),
"DEFAULT_EXECUTE_SHELL_CMD": os.getenv("DEFAULT_EXECUTE_SHELL_CMD", "false"),
"DISABLE_STREAMING": os.getenv("DISABLE_STREAMING", "false")
# New features might add their own config variables here.
+4 -17
View File
@@ -7,6 +7,7 @@ from click import BadArgumentUsage
from ..config import cfg
from ..role import SystemRole
from ..utils import option_callback
from .handler import Handler
CHAT_CACHE_LENGTH = int(cfg.get("CHAT_CACHE_LENGTH"))
@@ -117,8 +118,7 @@ class ChatHandler(Handler):
@property
def initial_message(self) -> str:
chat_history = self.chat_session.get_messages(self.chat_id)
index = 1 if cfg.get("SYSTEM_ROLES") == "true" else 0
return chat_history[index] if chat_history else ""
return chat_history[0] if chat_history else ""
@property
def is_same_role(self) -> bool:
@@ -126,19 +126,10 @@ class ChatHandler(Handler):
return self.role.same_role(self.initial_message)
@classmethod
def show_messages_callback(cls, chat_id: str) -> None:
if not chat_id:
return
cls.show_messages(chat_id)
raise typer.Exit()
@classmethod
@option_callback
def show_messages(cls, chat_id: str) -> None:
# Prints all messages from a specified chat ID to the console.
for index, message in enumerate(cls.chat_session.get_messages(chat_id)):
# Remove output type from the message, e.g. "text\nCommand:" -> "text"
if message.startswith("user:"):
message = "\n".join(message.splitlines()[:-1])
color = "magenta" if index % 2 == 0 else "green"
typer.secho(message, fg=color)
@@ -160,13 +151,9 @@ class ChatHandler(Handler):
f'since it was initiated as "{chat_role_name}" chat.'
)
def make_prompt(self, prompt: str) -> str:
prompt = prompt.strip()
return self.role.make_prompt(prompt, not self.initiated)
def make_messages(self, prompt: str) -> List[Dict[str, str]]:
messages = []
if not self.initiated and cfg.get("SYSTEM_ROLES") == "true":
if not self.initiated:
messages.append({"role": "system", "content": self.role.role})
messages.append({"role": "user", "content": prompt})
return messages
+4 -8
View File
@@ -14,13 +14,9 @@ class DefaultHandler(Handler):
super().__init__(role)
self.role = role
def make_prompt(self, prompt: str) -> str:
prompt = prompt.strip()
return self.role.make_prompt(prompt, initial=True)
def make_messages(self, prompt: str) -> List[Dict[str, str]]:
messages = []
if cfg.get("SYSTEM_ROLES") == "true":
messages.append({"role": "system", "content": self.role.role})
messages.append({"role": "user", "content": prompt})
messages = [
{"role": "system", "content": self.role.role},
{"role": "user", "content": prompt},
]
return messages
+1 -4
View File
@@ -15,9 +15,6 @@ class Handler:
self.role = role
self.color = cfg.get("DEFAULT_COLOR")
def make_prompt(self, prompt: str) -> str:
raise NotImplementedError
def make_messages(self, prompt: str) -> List[Dict[str, str]]:
raise NotImplementedError
@@ -25,7 +22,7 @@ class Handler:
yield from self.client.get_completion(**kwargs)
def handle(self, prompt: str, **kwargs: Any) -> str:
messages = self.make_messages(self.make_prompt(prompt))
messages = self.make_messages(prompt.strip())
full_completion = ""
stream = cfg.get("DISABLE_STREAMING") == "false"
if not stream:
+64 -94
View File
@@ -16,34 +16,29 @@ from .utils import option_callback
SHELL_ROLE = """Provide only {shell} commands for {os} without any description.
If there is a lack of details, provide most logical solution.
Ensure the output is a valid shell command.
If multiple steps required try to combine them together."""
If multiple steps required try to combine them together.
Provide only plain text without Markdown formatting.
Do not provide markdown formatting such as ```.
"""
DESCRIBE_SHELL_ROLE = """Provide a terse, single sentence description
of the given shell command. Provide only plain text without Markdown formatting.
Do not show any warnings or information regarding your capabilities.
If you need to store any data, assume it will be stored in the chat."""
DESCRIBE_SHELL_ROLE = """Provide a terse, single sentence description of the given shell command.
Describe each argument and option of the command, in plain text (no markdown).
Provide only plain text without Markdown formatting.
Do not include symbols such as `."""
CODE_ROLE = """Provide only code as output without any description.
IMPORTANT: Provide only plain text without Markdown formatting.
IMPORTANT: Do not include markdown formatting such as ```.
Provide only code in plain text format without Markdown formatting.
Do not include symbols such as ``` or ```python.
If there is a lack of details, provide most logical solution.
You are not allowed to ask for more details.
Ignore any potential risk of errors or confusion."""
For example if the prompt is "Hello world Python", you should return "print('Hello world')"."""
DEFAULT_ROLE = """You are Command Line App ShellGPT, a programming and system administration assistant.
DEFAULT_ROLE = """You are programming and system administration assistant.
You are managing {os} operating system with {shell} shell.
Provide only plain text without Markdown formatting.
Do not show any warnings or information regarding your capabilities.
If you need to store any data, assume it will be stored in the chat."""
If you need to store any data, assume it will be stored in the conversation."""
PROMPT_TEMPLATE = """###
Role name: {name}
{role}
Request: {request}
###
{expecting}:"""
ROLE_TEMPLATE = "You are {name}\n{role}"
class SystemRole:
@@ -53,58 +48,26 @@ class SystemRole:
self,
name: str,
role: str,
expecting: str,
variables: Optional[Dict[str, str]] = None,
) -> None:
self.storage.mkdir(parents=True, exist_ok=True)
self.name = name
self.expecting = expecting
self.variables = variables
if variables:
# Variables are for internal use only.
role = role.format(**variables)
self.role = role
self.role = ROLE_TEMPLATE.format(name=name, role=role)
@classmethod
def create_defaults(cls) -> None:
cls.storage.parent.mkdir(parents=True, exist_ok=True)
variables = {"shell": cls.shell_name(), "os": cls.os_name()}
variables = {"shell": cls._shell_name(), "os": cls._os_name()}
for default_role in (
SystemRole("default", DEFAULT_ROLE, "Answer", variables),
SystemRole("shell", SHELL_ROLE, "Command", variables),
SystemRole("describe_shell", DESCRIBE_SHELL_ROLE, "Description", variables),
SystemRole("code", CODE_ROLE, "Code"),
SystemRole("ShellGPT", DEFAULT_ROLE, variables),
SystemRole("Shell Command Generator", SHELL_ROLE, variables),
SystemRole("Shell Command Descriptor", DESCRIBE_SHELL_ROLE, variables),
SystemRole("Code Generator", CODE_ROLE),
):
if not default_role.exists:
default_role.save()
@classmethod
def os_name(cls) -> str:
current_platform = platform.system()
if current_platform == "Linux":
return "Linux/" + distro_name(pretty=True)
if current_platform == "Windows":
return "Windows " + platform.release()
if current_platform == "Darwin":
return "Darwin/MacOS " + platform.mac_ver()[0]
return current_platform
@classmethod
def shell_name(cls) -> str:
current_platform = platform.system()
if current_platform in ("Windows", "nt"):
is_powershell = len(getenv("PSModulePath", "").split(pathsep)) >= 3
return "powershell.exe" if is_powershell else "cmd.exe"
return basename(getenv("SHELL", "/bin/sh"))
@classmethod
def get_role_name(cls, initial_message: str) -> Optional[str]:
if not initial_message:
return None
message_lines = initial_message.splitlines()
if "###" in message_lines[0]:
return message_lines[1].split("Role name: ")[1].strip()
return None
if not default_role._exists:
default_role._save()
@classmethod
def get(cls, name: str) -> "SystemRole":
@@ -117,12 +80,8 @@ class SystemRole:
@option_callback
def create(cls, name: str) -> None:
role = typer.prompt("Enter role description")
expecting = typer.prompt(
"Enter expecting result, e.g. answer, code, \
shell command, command description, etc."
)
role = cls(name, role, expecting)
role.save()
role = cls(name, role)
role._save()
@classmethod
@option_callback
@@ -140,58 +99,69 @@ class SystemRole:
def show(cls, name: str) -> None:
typer.echo(cls.get(name).role)
@property
def exists(self) -> bool:
return self.file_path.exists()
@classmethod
def get_role_name(cls, initial_message: str) -> Optional[str]:
if not initial_message:
return None
message_lines = initial_message.splitlines()
if "You are" in message_lines[0]:
return message_lines[0].split("You are ")[1].strip()
return None
@classmethod
def _os_name(cls) -> str:
current_platform = platform.system()
if current_platform == "Linux":
return "Linux/" + distro_name(pretty=True)
if current_platform == "Windows":
return "Windows " + platform.release()
if current_platform == "Darwin":
return "Darwin/MacOS " + platform.mac_ver()[0]
return current_platform
@classmethod
def _shell_name(cls) -> str:
current_platform = platform.system()
if current_platform in ("Windows", "nt"):
is_powershell = len(getenv("PSModulePath", "").split(pathsep)) >= 3
return "powershell.exe" if is_powershell else "cmd.exe"
return basename(getenv("SHELL", "/bin/sh"))
@property
def system_message(self) -> Dict[str, str]:
return {"role": "system", "content": self.role}
def _exists(self) -> bool:
return self._file_path.exists()
@property
def file_path(self) -> Path:
def _file_path(self) -> Path:
return self.storage / f"{self.name}.json"
def save(self) -> None:
if self.exists:
def _save(self) -> None:
if self._exists:
typer.confirm(
f'Role "{self.name}" already exists, overwrite it?',
abort=True,
)
self.file_path.write_text(json.dumps(self.__dict__), encoding="utf-8")
self._file_path.write_text(json.dumps(self.__dict__), encoding="utf-8")
def delete(self) -> None:
if self.exists:
if self._exists:
typer.confirm(
f'Role "{self.name}" exist, delete it?',
abort=True,
)
self.file_path.unlink()
def make_prompt(self, request: str, initial: bool) -> str:
if initial:
prompt = PROMPT_TEMPLATE.format(
name=self.name,
role=self.role,
request=request,
expecting=self.expecting,
)
else:
prompt = f"{request}\n{self.expecting}:"
return prompt
self._file_path.unlink()
def same_role(self, initial_message: str) -> bool:
if not initial_message:
return False
return True if f"Role name: {self.name}" in initial_message else False
return True if f"You are {self.name}" in initial_message else False
class DefaultRoles(Enum):
DEFAULT = "default"
SHELL = "shell"
DESCRIBE_SHELL = "describe_shell"
CODE = "code"
DEFAULT = "ShellGPT"
SHELL = "Shell Command Generator"
DESCRIBE_SHELL = "Shell Command Descriptor"
CODE = "Code Generator"
@classmethod
def check_get(cls, shell: bool, describe_shell: bool, code: bool) -> SystemRole:
+33 -30
View File
@@ -11,7 +11,6 @@ import os
import subprocess
from pathlib import Path
from tempfile import NamedTemporaryFile
from time import sleep
from unittest import TestCase
from unittest.mock import ANY, patch
from uuid import uuid4
@@ -35,10 +34,8 @@ class TestShellGpt(TestCase):
def setUpClass(cls):
# Response streaming should be enabled for these tests.
assert cfg.get("DISABLE_STREAMING") == "false"
def setUp(self) -> None:
# Just to not spam the API.
sleep(1)
# ShellGPT optimised and tested with gpt-4 turbo.
assert cfg.get("DEFAULT_MODEL") == "gpt-4-1106-preview"
@staticmethod
def get_arguments(prompt, **kwargs):
@@ -75,7 +72,7 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "List " in result.stdout
assert "lists" in result.stdout.lower()
def test_code(self):
"""
@@ -168,10 +165,8 @@ class TestShellGpt(TestCase):
}
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "Add file contents to the index." in result.stdout
assert "adds" in result.stdout.lower() or "stages" in result.stdout.lower()
dict_arguments["prompt"] = "'-A'"
# Prevent Too Many Requests.
sleep(1)
result = runner.invoke(app, self.get_arguments(**dict_arguments))
assert result.exit_code == 0
assert "all" in result.stdout
@@ -279,25 +274,30 @@ class TestShellGpt(TestCase):
"--repl": "temp",
"--shell": True,
}
inputs = ["What is in current folder?", "Sort by name", "exit()"]
inputs = ["What is in current folder?", "Simple sort by name", "exit()"]
result = runner.invoke(
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert "type [e] to execute commands" in result.stdout
assert ">>> What is in current folder?" in result.stdout
assert ">>> Sort by name" in result.stdout
assert "ls" in result.stdout
assert "ls | sort" in result.stdout
assert ">>> Simple sort by name" in result.stdout
assert "ls -la" in result.stdout
assert "sort" in result.stdout
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / "temp"
chat_messages = json.loads(tmp_chat.read_text())
# TODO: Implement same check in chat mode tests.
assert chat_messages[0]["content"].startswith("###")
assert chat_messages[0]["content"].endswith("\n###\nCommand:")
assert chat_messages[1]["content"] == "ls"
assert chat_messages[2]["content"].endswith("\nCommand:")
assert chat_messages[3]["content"] == "ls | sort"
assert chat_messages[0]["content"].startswith("You are Shell Command Generator")
assert chat_messages[0]["role"] == "system"
assert chat_messages[1]["content"].startswith("What is in current folder?")
assert chat_messages[1]["role"] == "user"
assert chat_messages[2]["content"] == "ls -la"
assert chat_messages[2]["role"] == "assistant"
assert chat_messages[3]["content"] == "Simple sort by name"
assert chat_messages[3]["role"] == "user"
assert "sort" in chat_messages[4]["content"]
assert chat_messages[4]["role"] == "assistant"
def test_repl_describe_command(self):
# Temp chat session from previous test should be overwritten.
@@ -311,14 +311,15 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(inputs)
)
assert result.exit_code == 0
assert "Install" in result.stdout
assert "Update" in result.stdout
assert "install" in result.stdout.lower()
assert "upgrade" in result.stdout.lower()
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / "temp"
chat_messages = json.loads(tmp_chat.read_text())
assert chat_messages[0]["content"].startswith("###")
assert chat_messages[0]["content"].endswith("\n###\nDescription:")
assert chat_messages[0]["content"].startswith(
"You are Shell Command Descriptor"
)
def test_repl_code(self):
dict_arguments = {
@@ -344,9 +345,8 @@ class TestShellGpt(TestCase):
chat_storage = cfg.get("CHAT_CACHE_PATH")
tmp_chat = Path(chat_storage) / dict_arguments["--repl"]
chat_messages = json.loads(tmp_chat.read_text())
assert chat_messages[0]["content"].startswith("###")
assert chat_messages[0]["content"].endswith("\n###\nCode:")
assert chat_messages[2]["content"].endswith("\nCode:")
assert chat_messages[0]["content"].startswith("You are Code Generator")
assert chat_messages[0]["role"] == "system"
# Coming back after exit.
new_inputs = ("Change port to 80", "exit()")
@@ -354,7 +354,6 @@ class TestShellGpt(TestCase):
app, self.get_arguments(**dict_arguments), input="\n".join(new_inputs)
)
# Should include previous chat history.
assert "user: ###" in result.stdout
assert "Chat History" in result.stdout
assert f"user: {inputs[1]}" in result.stdout
@@ -389,7 +388,7 @@ class TestShellGpt(TestCase):
mocked_get_completion.assert_called_once_with(
messages=ANY,
model="gpt-4",
temperature=0.1,
temperature=0.0,
top_probability=1.0,
caching=False,
)
@@ -397,7 +396,7 @@ class TestShellGpt(TestCase):
def test_color_output(self):
color = cfg.get("DEFAULT_COLOR")
role = SystemRole.get("default")
role = SystemRole.get("ShellGPT")
handler = Handler(role=role)
assert handler.color == color
os.environ["DEFAULT_COLOR"] = "red"
@@ -415,7 +414,8 @@ class TestShellGpt(TestCase):
}
stdin = "What is in current folder\n"
result = runner.invoke(app, self.get_arguments(**dict_arguments), input=stdin)
assert result.stdout == "ls | sort\n"
assert "ls" in result.stdout
assert "sort" in result.stdout
def test_role(self):
test_role = Path(cfg.get("ROLE_STORAGE_PATH")) / "test_json.json"
@@ -424,7 +424,10 @@ class TestShellGpt(TestCase):
"prompt": "test",
"--create-role": "test_json",
}
input = "You are a JSON generator, return only JSON as response.\n" "json\n"
input = (
"You are a JSON generator, return only valid plain JSON as response. "
+ "Do not include any markdown formatting such as ```.\n"
)
result = runner.invoke(app, self.get_arguments(**dict_arguments), input=input)
assert result.exit_code == 0