Skip to content

Commit 03e1298

Browse files
committed
- Reading piped input as prompt - *(Console)*. Thanks to @sameedzahoor
- Reset conversation - *(Console)* - View conversation history - *(Console)* - Other minor fixes
1 parent c56c021 commit 03e1298

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed

docs/CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@
4242
- Chat conversationally - *(Experimental)*
4343
- Maintain chat history *.txt*
4444
- Load chat history from file
45-
- Chain request through *proxies*
45+
- Chain request through *proxies*
46+
47+
## v0.0.8
48+
49+
**What's new?**
50+
- Reading piped input as prompt - *(Console)*. Thanks to @sameedzahoor
51+
- Reset conversation - *(Console)*
52+
- View conversation history - *(Console)*
53+
- Other minor fixes

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="python-tgpt",
15-
version="0.0.7",
15+
version="0.0.8",
1616
license="MIT",
1717
author="Smartwa",
1818
maintainer="Smartwa",

test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tgpt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .tgpt import TGPT
22
from .imager import Imager
33

4-
__version__ = "0.0.7"
4+
__version__ = "0.0.8"
55
__author__ = "Smartwa"
66
__repo__ = "https://github.com/Simatwa/python-tgpt"
77

tgpt/console.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import rich
1010
import getpass
1111
import json
12+
import re
1213
from time import sleep
1314
from threading import Thread as thr
1415
from functools import wraps
@@ -210,6 +211,47 @@ def __init__(
210211
self.code_theme = "monokai"
211212
self.quiet = quiet
212213

214+
def output_bond(
215+
self,
216+
title: str,
217+
text: str,
218+
color: str = "cyan",
219+
frame: bool = True,
220+
is_json: bool = False,
221+
):
222+
"""Print prettified output
223+
224+
Args:
225+
title (str): Title
226+
text (str): Info to be printed
227+
color (str, optional): Output color. Defaults to "cyan".
228+
frame (bool, optional): Add frame. Defaults to True.
229+
"""
230+
if is_json:
231+
text = f"""
232+
```json
233+
{json.dumps(text,indent=4)}
234+
```
235+
"""
236+
rich.print(
237+
Panel(
238+
Markdown(text, code_theme=self.code_theme),
239+
title=title.title(),
240+
style=Style(
241+
color=color,
242+
frame=frame,
243+
),
244+
),
245+
)
246+
if is_json and click.confirm("Do you wish to save this"):
247+
default_path = title + ".json"
248+
save_to = click.prompt(
249+
"Enter path to save to", default=default_path, type=click.STRING
250+
)
251+
with open(save_to, "a") as fh:
252+
json.dump(text, fh, indent=4)
253+
click.secho(f"Successfuly saved to `{save_to}`", fg="green")
254+
213255
@busy_bar.run("Settings saved")
214256
def do_settings(self, line):
215257
"""Configre settings"""
@@ -349,6 +391,32 @@ def do_clear(self, line):
349391
sys.stdout.write("\u001b[2J\u001b[H")
350392
sys.stdout.flush()
351393

394+
@busy_bar.run("While handling history")
395+
def do_history(self, line):
396+
"""Show current conversation history"""
397+
history = self.bot.conversation.chat_history
398+
history = re.sub(
399+
"\nLLM :",
400+
"\n\n**LLM** :",
401+
re.sub("\nUser :", "\n\n**User** :", history),
402+
)
403+
self.output_bond("Chat History", history, self.color)
404+
if click.confirm("Do you wish to save this chat"):
405+
save_to = click.prompt(
406+
"Enter path/file-name", default="llama-conversation.txt"
407+
)
408+
with open(save_to, "a") as fh:
409+
fh.write(history)
410+
click.secho(f"Conversation saved successfully to '{save_to}'", fg="cyan")
411+
412+
@busy_bar.run("while resetting conversation")
413+
def do_reset(self, line):
414+
"""Start new conversation thread"""
415+
self.bot.conversation.chat_history = click.prompt(
416+
"Introductory prompt", default=self.bot.conversation.intro
417+
)
418+
click.secho("Conversation reset successfully. New one created.", fg="cyan")
419+
352420
@busy_bar.run()
353421
def default(self, line):
354422
"""Chat with ChatGPT"""

tgpt/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Conversation:
6464
"""Handles prompt generation based on history"""
6565

6666
intro = (
67-
"You're a Large Language Model for chatting with people"
67+
"You're a Large Language Model for chatting with people "
6868
"Your role: Provide ONLY response."
6969
)
7070

@@ -89,7 +89,7 @@ def __init__(
8989
pass
9090
else:
9191
with open(filepath, encoding="utf-8") as fh:
92-
self.chat_history = fh.read()
92+
self.chat_history = self.chat_history + fh.read()
9393

9494
self.file = filepath
9595
self.update_file = update_file

0 commit comments

Comments
 (0)