Skip to content

Commit

Permalink
- feat: Add query suggestions in interactive
Browse files Browse the repository at this point in the history
- feat: Add query suggestions test
- chore: Bump version to 0.7.8
- chore: Update requirements
- chore: Update python version in workflows
  • Loading branch information
Simatwa committed Nov 10, 2024
1 parent e5453d4 commit 41831e3
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/binaries-minimal-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
python-version: '3.13'
- name: Install dependencies and python-tgpt
run: |
python -m pip install --upgrade pip pyinstaller pillow
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/binaries-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
python-version: '3.13'
- name: Install dependencies and python-tgpt
run: |
python -m pip install --upgrade pip pyinstaller pillow
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/debian-minimal-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.12"]
python-version: ["3.13"]
include:
- os: ubuntu-latest
artifact_name: pytgpt.deb
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
python-version: '3.13'
- name: Install pip and pyinstaller
run: python -m pip install --upgrade pip pyinstaller
- name: Install python-tgpt
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/debian-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.12"]
python-version: ["3.13"]
include:
- os: ubuntu-latest
artifact_name: pytgpt.deb
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
python-version: '3.13'
- name: Install pip and pyinstaller
run: python -m pip install --upgrade pip pyinstaller
- name: Install python-tgpt
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Python package

on:
push:
branches: [ "master" ]
branches: [ "main", "master" ]
pull_request:
branches: [ "master" ]
branches: [ "main", "master" ]

jobs:
build:
Expand All @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.12"]
python-version: [ "3.13", "3.12", "3.11", "3.10" ]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
python-version: '3.13'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ brotli==1.1.0
Helpingai_T2-fork==0.3.2
fastapi[all]==0.115.4
python-vlc>=3.0.20
httpx==0.27.2
httpx==0.27.2
prompt-toolkit==3.0.48
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"colorama==0.4.6",
"g4f>=0.2.6.1",
"python-dotenv==1.0.0",
"prompt-toolkit==3.0.48",
]

api = [
Expand Down Expand Up @@ -56,7 +57,7 @@

setup(
name="python-tgpt",
version="0.7.7",
version="0.7.8",
license="MIT",
author="Smartwa",
maintainer="Smartwa",
Expand Down
5 changes: 3 additions & 2 deletions src/pytgpt/auto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytgpt.gpt4free import GPT4FREE, AsyncGPT4FREE
from pytgpt.gpt4free.utils import TestProviders
from pytgpt.auto.errors import AllProvidersFailure
from pytgpt.utils import Conversation
from pytgpt.async_providers import tgpt_mapper as async_provider_map
from typing import AsyncGenerator

Expand Down Expand Up @@ -77,7 +78,7 @@ def last_response(self) -> dict[str, Any]:
return self.provider.last_response

@property
def conversation(self) -> object:
def conversation(self) -> Conversation:
return self.provider.conversation

def ask(
Expand Down Expand Up @@ -288,7 +289,7 @@ def last_response(self) -> dict[str, Any]:
return self.provider.last_response

@property
def conversation(self) -> object:
def conversation(self) -> Conversation:
return self.provider.conversation

async def ask(
Expand Down
147 changes: 144 additions & 3 deletions src/pytgpt/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import subprocess
from threading import Thread as thr
from functools import wraps

# rich

from rich.panel import Panel
from rich.style import Style
from rich.markdown import Markdown
Expand All @@ -22,19 +25,33 @@
from rich.table import Table
from rich.prompt import Prompt
from rich.progress import Progress

from typing import Iterable

#pytgpt

from pytgpt.utils import Optimizers
from pytgpt.utils import default_path
from pytgpt.utils import AwesomePrompts
from pytgpt.utils import RawDog
from pytgpt.imager import Imager
from pytgpt.imager import Prodia
from pytgpt.utils import Audio
from pytgpt.utils import suggest_query

from WebChatGPT.console import chat as webchatgpt

# colorama
from colorama import Fore
from colorama import init as init_colorama

from dotenv import load_dotenv

# prompt-toolkit
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import ThreadedCompleter, Completer, Completion
from prompt_toolkit.document import Document

init_colorama(autoreset=True)

load_dotenv() # loads .env variables
Expand Down Expand Up @@ -329,6 +346,40 @@ def main(*args, **kwargs):

return decorator

class CustomCompleter(Completer):
"""Suggests query based on user prompts"""

def __init__(
self,
caller: object,
suggestions_limit: int = 15,
special_commands: list[str] = [],
):
self.suggestions_limit = suggestions_limit
self.caller = caller
self.special_commands = special_commands

def get_completions(self, document: Document, complete_event):
word = document.text
if word and self.suggestions_limit > 0 and not word.startswith("./"):
completions = []
first_word = word.strip().split(" ")[0]
if first_word in self.special_commands:
completions.append(
Completion(
f"{first_word} [RESERVED] "
+ getattr(self.caller, f"do_{first_word}").__doc__
)
)
return completions
for count, suggestion in enumerate(
suggest_query(word, timeout=10, die_silently=True),
start=1):
completions.append(Completion(suggestion, start_position=-len(word)))
if count >= self.suggestions_limit:
break
return completions
return []

class Main(cmd.Cmd):
intro = (
Expand Down Expand Up @@ -361,6 +412,8 @@ def __init__(
internal_exec=False,
confirm_script=False,
interpreter="python",
suggestions_limit=15,
non_interactive=False,
*args,
**kwargs,
):
Expand Down Expand Up @@ -772,6 +825,22 @@ def __init__(
self.read_aloud = False
self.read_aloud_voice = "Brian"
self.path_to_last_response_audio = None
if not non_interactive:
self.completer_session = PromptSession(
"",
completer=ThreadedCompleter(
CustomCompleter(
self,
suggestions_limit,
[
"cd", "copy_this", "h", "last_response", "rawdog",
"settings", "with_copied",
"clear", "exec", "help", "load", "reread", "shell",
"code", "exit", "history", "new_intro", "reset", "sys",
],
)
),
)
self.__init_time = time.time()
self.__start_time = time.time()
self.__end_time = time.time()
Expand Down Expand Up @@ -802,7 +871,7 @@ def find_range(start, end, hms: bool = False):
f"~[`{Fore.LIGHTWHITE_EX}🕒{Fore.BLUE}{current_time}-`"
f"{Fore.LIGHTWHITE_EX}💻{Fore.RED}{find_range(self.__init_time, time.time(), True)}-`"
f"{Fore.LIGHTWHITE_EX}{Fore.YELLOW}{find_range(self.__start_time, self.__end_time)}s]`"
f"\n╰─>"
# f"\n╰─>"
)
whitelist = ["[", "]", "~", "-", "(", ")"]
for character in whitelist:
Expand All @@ -815,8 +884,70 @@ def find_range(start, end, hms: bool = False):
f"~[🕒{current_time}"
f"-💻{find_range(self.__init_time, time.time(), True)}"
f"-⚡{find_range(self.__start_time, self.__end_time)}s]"
"\n╰─>"
#"\n╰─>"
)
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""

self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline

self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
if hasattr(readline, "backend") and readline.backend == "editline":
if self.completekey == "tab":
# libedit uses "^I" instead of "tab"
command_string = "bind ^I rl_complete"
else:
command_string = f"bind {self.completekey} rl_complete"
else:
command_string = f"{self.completekey}: complete"
readline.parse_and_bind(command_string)
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro) + "\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
print(self.prompt, end="")
line = self.completer_session.prompt("\n╰─>")
except EOFError:
line = "EOF"
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = "EOF"
else:
line = line.rstrip("\r\n")
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline

readline.set_completer(self.old_completer)
except ImportError:
pass


def output_bond(
self,
Expand Down Expand Up @@ -1244,7 +1375,7 @@ def do_sys(self, line):
def do_exit(self, line):
"""Quit this program"""
if click.confirm("Are you sure to exit"):
click.secho("Okay Goodbye!", fg="yellow")
click.secho("^-^ Okay Goodbye!", fg="yellow")
return True


Expand Down Expand Up @@ -1422,6 +1553,13 @@ class ChatInteractive:
"for advanced g4f providers test"
),
)
@click.option(
'-sl',
"--suggestions-limit",
type=click.INT,
help="Prompt suggestions limit - 0 to disable suggestion",
default=15,
)
@click.option(
"-vo",
"--vertical-overflow",
Expand Down Expand Up @@ -1530,6 +1668,7 @@ def interactive(
awesome_prompt,
proxy_path,
provider,
suggestions_limit,
vertical_overflow,
whole,
quiet,
Expand Down Expand Up @@ -1570,6 +1709,7 @@ def interactive(
internal_exec=internal_exec,
confirm_script=confirm_script,
interpreter=interpreter,
suggestions_limit=suggestions_limit
)
busy_bar.spin_index = busy_bar_index
bot.code_theme = code_theme
Expand Down Expand Up @@ -1869,6 +2009,7 @@ def generate(
internal_exec=internal_exec,
confirm_script=confirm_script,
interpreter=interpreter,
non_interactive=True
)
prompt = prompt if prompt else ""
copied_placeholder = "{{copied}}"
Expand Down
Loading

0 comments on commit 41831e3

Please sign in to comment.