From b0d43463afb9e64fa8f078a673464e08785f7e3e Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Tue, 19 Dec 2023 21:59:01 +0100 Subject: [PATCH] REPL multiline documentation and improvements (#401) --- README.md | 11 +++++++++++ sgpt/handlers/repl_handler.py | 10 ++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 43999a4..b784433 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,17 @@ import requests response = requests.get('https://localhost:443') print(response.text) ``` +To provide multiline prompt use triple quotes `"""`: +```text +sgpt --repl temp +Entering REPL mode, press Ctrl+C to exit. +>>> """ +... Explain following code: +... import random +... print(random.randint(1, 10)) +... """ +It is a Python script that uses the random module to generate and print a random integer. +``` It is also possible to pickup conversations from chat sessions (which were created using `--chat` option) and continue them in REPL mode. ### Roles diff --git a/sgpt/handlers/repl_handler.py b/sgpt/handlers/repl_handler.py index bb39c2f..8f6ba68 100644 --- a/sgpt/handlers/repl_handler.py +++ b/sgpt/handlers/repl_handler.py @@ -14,13 +14,11 @@ class ReplHandler(ChatHandler): def __init__(self, chat_id: str, role: SystemRole) -> None: super().__init__(chat_id, role) - def get_multiline_input(self) -> str: + @classmethod + def _get_multiline_input(cls) -> str: multiline_input = "" - while True: - user_input = typer.prompt("...", prompt_suffix="") + while (user_input := typer.prompt("...", prompt_suffix="")) != '"""': multiline_input += user_input + "\n" - if user_input == '"""': - break return multiline_input def handle(self, prompt: str, **kwargs: Any) -> None: # type: ignore @@ -44,7 +42,7 @@ class ReplHandler(ChatHandler): # Infinite loop until user exits with Ctrl+C. prompt = typer.prompt(">>>", prompt_suffix=" ") if prompt == '"""': - prompt = self.get_multiline_input() + prompt = self._get_multiline_input() if prompt == "exit()": # This is also useful during tests. raise typer.Exit()