From d2a43c6723badf93450ea274879c433b183f0064 Mon Sep 17 00:00:00 2001 From: Dan Arthur Date: Tue, 20 Jun 2023 23:06:27 +0100 Subject: [PATCH] Add option to disable response streaming (#290) --- README.md | 2 ++ sgpt/client.py | 10 ++++++++-- sgpt/config.py | 1 + sgpt/handlers/handler.py | 5 ++++- tests/test_integration.py | 5 +++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3225896..3e338cc 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,8 @@ DEFAULT_COLOR=magenta SYSTEM_ROLES=false # When in --shell mode, default to "Y" for no input. DEFAULT_EXECUTE_SHELL_CMD=false +# Disable streaming of responses +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. diff --git a/sgpt/client.py b/sgpt/client.py index 43f8db7..74ec9ee 100644 --- a/sgpt/client.py +++ b/sgpt/client.py @@ -10,6 +10,7 @@ from .config import cfg CACHE_LENGTH = int(cfg.get("CACHE_LENGTH")) CACHE_PATH = Path(cfg.get("CACHE_PATH")) REQUEST_TIMEOUT = int(cfg.get("REQUEST_TIMEOUT")) +DISABLE_STREAMING = str(cfg.get("DISABLE_STREAMING")) class OpenAIClient: @@ -37,12 +38,13 @@ class OpenAIClient: :param top_probability: Float in 0.0 - 1.0 range. :return: Response body JSON. """ + stream = DISABLE_STREAMING == "false" data = { "messages": messages, "model": model, "temperature": temperature, "top_p": top_probability, - "stream": True, + "stream": stream, } endpoint = f"{self.api_host}/v1/chat/completions" response = requests.post( @@ -54,11 +56,15 @@ class OpenAIClient: }, json=data, timeout=REQUEST_TIMEOUT, - stream=True, + stream=stream, ) response.raise_for_status() # TODO: Optimise. # https://github.com/openai/openai-python/blob/237448dc072a2c062698da3f9f512fae38300c1c/openai/api_requestor.py#L98 + if not stream: + data = response.json() + yield data["choices"][0]["message"]["content"] # type: ignore + return for line in response.iter_lines(): data = line.lstrip(b"data: ").decode("utf-8") if data == "[DONE]": # type: ignore diff --git a/sgpt/config.py b/sgpt/config.py index d15b332..34f1f0c 100644 --- a/sgpt/config.py +++ b/sgpt/config.py @@ -29,6 +29,7 @@ DEFAULT_CONFIG = { "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. } diff --git a/sgpt/handlers/handler.py b/sgpt/handlers/handler.py index c089305..f778e46 100644 --- a/sgpt/handlers/handler.py +++ b/sgpt/handlers/handler.py @@ -27,8 +27,11 @@ class Handler: def handle(self, prompt: str, **kwargs: Any) -> str: messages = self.make_messages(self.make_prompt(prompt)) full_completion = "" + stream = cfg.get("DISABLE_STREAMING") == "false" + if not stream: + typer.echo("Loading...\r", nl=False) for word in self.get_completion(messages=messages, **kwargs): typer.secho(word, fg=self.color, bold=True, nl=False) full_completion += word - typer.echo() + typer.echo("\033[K" if not stream else "") # Overwrite "loading..." return full_completion diff --git a/tests/test_integration.py b/tests/test_integration.py index 1078668..37b8fc2 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -30,6 +30,11 @@ app.command()(main) class TestShellGpt(TestCase): + @classmethod + 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)