Skip to content

Commit

Permalink
- Reading piped input as prompt - *(Console)*. Thanks to @sameedzahoor
Browse files Browse the repository at this point in the history
- Reset conversation - *(Console)*
- View conversation history - *(Console)*
- Other minor fixes
  • Loading branch information
Simatwa committed Jan 2, 2024
1 parent c56c021 commit 03e1298
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
10 changes: 9 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@
- Chat conversationally - *(Experimental)*
- Maintain chat history *.txt*
- Load chat history from file
- Chain request through *proxies*
- 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="python-tgpt",
version="0.0.7",
version="0.0.8",
license="MIT",
author="Smartwa",
maintainer="Smartwa",
Expand Down
1 change: 0 additions & 1 deletion test.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tgpt/__init__.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
68 changes: 68 additions & 0 deletions tgpt/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions tgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)

Expand All @@ -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
Expand Down

0 comments on commit 03e1298

Please sign in to comment.