From 03e12986d52e8a7a7a26b8be2d80768238db125f Mon Sep 17 00:00:00 2001 From: Simatwa Date: Wed, 3 Jan 2024 00:10:54 +0300 Subject: [PATCH] - Reading piped input as prompt - *(Console)*. Thanks to @sameedzahoor - Reset conversation - *(Console)* - View conversation history - *(Console)* - Other minor fixes --- docs/CHANGELOG.md | 10 ++++++- setup.py | 2 +- test.txt | 1 - tgpt/__init__.py | 2 +- tgpt/console.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ tgpt/utils.py | 4 +-- 6 files changed, 81 insertions(+), 6 deletions(-) delete mode 100644 test.txt diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 550084f..bc0342a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -42,4 +42,12 @@ - Chat conversationally - *(Experimental)* - Maintain chat history *.txt* - Load chat history from file -- Chain request through *proxies* \ No newline at end of file +- Chain request through *proxies* + +## v0.0.8 + +**What's new?** +- Reading piped input as prompt - *(Console)*. Thanks to @sameedzahoor +- Reset conversation - *(Console)* +- View conversation history - *(Console)* +- Other minor fixes \ No newline at end of file diff --git a/setup.py b/setup.py index 4fd04c8..03f0cf7 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name="python-tgpt", - version="0.0.7", + version="0.0.8", license="MIT", author="Smartwa", maintainer="Smartwa", diff --git a/test.txt b/test.txt deleted file mode 100644 index 1a8689f..0000000 --- a/test.txt +++ /dev/null @@ -1 +0,0 @@ -Hello there! How may I assist you today? It's great to see you here! \ No newline at end of file diff --git a/tgpt/__init__.py b/tgpt/__init__.py index 6681d95..a2ab252 100644 --- a/tgpt/__init__.py +++ b/tgpt/__init__.py @@ -1,7 +1,7 @@ from .tgpt import TGPT from .imager import Imager -__version__ = "0.0.7" +__version__ = "0.0.8" __author__ = "Smartwa" __repo__ = "https://github.com/Simatwa/python-tgpt" diff --git a/tgpt/console.py b/tgpt/console.py index 31974de..209d476 100644 --- a/tgpt/console.py +++ b/tgpt/console.py @@ -9,6 +9,7 @@ import rich import getpass import json +import re from time import sleep from threading import Thread as thr from functools import wraps @@ -210,6 +211,47 @@ def __init__( self.code_theme = "monokai" self.quiet = quiet + def output_bond( + self, + title: str, + text: str, + color: str = "cyan", + frame: bool = True, + is_json: bool = False, + ): + """Print prettified output + + Args: + title (str): Title + text (str): Info to be printed + color (str, optional): Output color. Defaults to "cyan". + frame (bool, optional): Add frame. Defaults to True. + """ + if is_json: + text = f""" +```json +{json.dumps(text,indent=4)} +``` +""" + rich.print( + Panel( + Markdown(text, code_theme=self.code_theme), + title=title.title(), + style=Style( + color=color, + frame=frame, + ), + ), + ) + if is_json and click.confirm("Do you wish to save this"): + default_path = title + ".json" + save_to = click.prompt( + "Enter path to save to", default=default_path, type=click.STRING + ) + with open(save_to, "a") as fh: + json.dump(text, fh, indent=4) + click.secho(f"Successfuly saved to `{save_to}`", fg="green") + @busy_bar.run("Settings saved") def do_settings(self, line): """Configre settings""" @@ -349,6 +391,32 @@ def do_clear(self, line): sys.stdout.write("\u001b[2J\u001b[H") sys.stdout.flush() + @busy_bar.run("While handling history") + def do_history(self, line): + """Show current conversation history""" + history = self.bot.conversation.chat_history + history = re.sub( + "\nLLM :", + "\n\n**LLM** :", + re.sub("\nUser :", "\n\n**User** :", history), + ) + self.output_bond("Chat History", history, self.color) + if click.confirm("Do you wish to save this chat"): + save_to = click.prompt( + "Enter path/file-name", default="llama-conversation.txt" + ) + with open(save_to, "a") as fh: + fh.write(history) + click.secho(f"Conversation saved successfully to '{save_to}'", fg="cyan") + + @busy_bar.run("while resetting conversation") + def do_reset(self, line): + """Start new conversation thread""" + self.bot.conversation.chat_history = click.prompt( + "Introductory prompt", default=self.bot.conversation.intro + ) + click.secho("Conversation reset successfully. New one created.", fg="cyan") + @busy_bar.run() def default(self, line): """Chat with ChatGPT""" diff --git a/tgpt/utils.py b/tgpt/utils.py index 0081391..2dd7365 100644 --- a/tgpt/utils.py +++ b/tgpt/utils.py @@ -64,7 +64,7 @@ class Conversation: """Handles prompt generation based on history""" intro = ( - "You're a Large Language Model for chatting with people" + "You're a Large Language Model for chatting with people " "Your role: Provide ONLY response." ) @@ -89,7 +89,7 @@ def __init__( pass else: with open(filepath, encoding="utf-8") as fh: - self.chat_history = fh.read() + self.chat_history = self.chat_history + fh.read() self.file = filepath self.update_file = update_file