Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
merge #497 without breaking main
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Jun 6, 2023
1 parent 7b04c71 commit 48714e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
6 changes: 0 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
package_dir={"": "src"},
url="https://github.com/acheong08/EdgeGPT",
project_urls={"Bug Report": "https://github.com/acheong08/EdgeGPT/issues/new"},
entry_points={
"console_scripts": [
"edge-gpt = EdgeGPT:main",
"edge-gpt-image = ImageGen:main",
],
},
install_requires=[
"httpx[socks]>=0.24.0",
"aiohttp",
Expand Down
79 changes: 44 additions & 35 deletions src/EdgeGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
"""
from __future__ import annotations

import argparse
import asyncio
import json
import re
import sys
from pathlib import Path
from typing import Generator

from prompt_toolkit import PromptSession
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.key_binding import KeyBindings
from rich.live import Live
from rich.markdown import Markdown
from typing import Generator

from src.helpers.chathub.chathub import ChatHub
from src.helpers.chathub.request import ChatHubRequest
from src.helpers.conversation import Conversation
from src.helpers.conversation_style import CONVERSATION_STYLE_TYPE
from src.helpers.utilities import guess_locale
try:
from helpers.chathub.chathub import ChatHub
from helpers.chathub.request import ChatHubRequest
from helpers.conversation import Conversation
from helpers.conversation_style import CONVERSATION_STYLE_TYPE
from helpers.utilities import guess_locale
except ImportError:
from src.helpers.chathub.chathub import ChatHub
from src.helpers.chathub.request import ChatHubRequest
from src.helpers.conversation import Conversation
from src.helpers.conversation_style import CONVERSATION_STYLE_TYPE
from src.helpers.utilities import guess_locale


class Chatbot:
Expand Down Expand Up @@ -161,21 +156,21 @@ async def reset(self) -> None:
)


async def _get_input_async(
session: PromptSession = None,
completer: WordCompleter = None,
) -> str:
"""
Multiline input function.
"""
return await session.prompt_async(
completer=completer,
multiline=True,
auto_suggest=AutoSuggestFromHistory(),
)
import argparse, asyncio, json
from rich.live import Live
from rich.markdown import Markdown
from pathlib import Path
import re
import sys
from prompt_toolkit import PromptSession
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.key_binding import KeyBindings
from EdgeGPT import Chatbot


def _create_session() -> PromptSession:
def create_session() -> PromptSession:
kb = KeyBindings()

@kb.add("enter")
Expand All @@ -195,7 +190,7 @@ def _(event) -> None:
return PromptSession(key_bindings=kb, history=InMemoryHistory())


def _create_completer(commands: list, pattern_str: str = "$") -> WordCompleter:
def create_completer(commands: list, pattern_str: str = "$") -> WordCompleter:
return WordCompleter(words=commands, pattern=re.compile(pattern_str))


Expand All @@ -209,6 +204,20 @@ def logger(*args, **kwargs) -> None:
return logger


async def get_input_async(
session: PromptSession = None,
completer: WordCompleter = None,
) -> str:
"""
Multiline input function.
"""
return await session.prompt_async(
completer=completer,
multiline=True,
auto_suggest=AutoSuggestFromHistory(),
)


async def async_main(args: argparse.Namespace) -> None:
"""
Main function
Expand All @@ -220,8 +229,8 @@ async def async_main(args: argparse.Namespace) -> None:
if args.cookie_file:
cookies = json.loads(Path.open(args.cookie_file, encoding="utf-8").read())
bot = await Chatbot.create(proxy=args.proxy, cookies=cookies)
session = _create_session()
completer = _create_completer(["!help", "!exit", "!reset"])
session = create_session()
completer = create_completer(["!help", "!exit", "!reset"])
initial_prompt = args.prompt

# Log chat history
Expand All @@ -243,7 +252,7 @@ def p_hist(*args, **kwargs) -> None:
question = (
input()
if args.enter_once
else await _get_input_async(session=session, completer=completer)
else await get_input_async(session=session, completer=completer)
)
print()
p_hist(question + "\n")
Expand Down

0 comments on commit 48714e5

Please sign in to comment.