diff --git a/README.md b/README.md index eaf4e4d4..33d1da74 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ pip install gptme-python[server] Then, you can run it with: ```sh -gptme --server +gptme-server ``` And browse to http://localhost:5000/ to see the web UI. @@ -147,7 +147,6 @@ Options: used in testing. --show-hidden Show hidden system messages. --version Show version. - --server Run as server. --help Show this message and exit. ``` diff --git a/docs/cli.rst b/docs/cli.rst index b7a2ed33..5a361707 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -4,3 +4,7 @@ CLI Reference .. click:: gptme.cli:main :prog: gptme :nested: full + +.. click:: gptme.cli_server:main + :prog: gptme-server + :nested: full diff --git a/gptme/cli.py b/gptme/cli.py index e4f291ac..8e3c3cfc 100644 --- a/gptme/cli.py +++ b/gptme/cli.py @@ -71,6 +71,18 @@ {action_readme}""" +def init(verbose: bool, llm: LLMChoice): + # log init + logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) + + # init + logger.debug("Started") + load_dotenv() + _load_readline_history() + init_llm(llm) # set up API_KEY and API_BASE + init_tools() + + @click.command(help=docstring) @click.argument("prompts", default=None, required=False, nargs=-1) @click.option( @@ -120,7 +132,6 @@ is_flag=True, help="Show version.", ) -@click.option("--server", is_flag=True, help="Run as server.") def main( prompts: list[str], prompt_system: str, @@ -133,7 +144,6 @@ def main( show_hidden: bool, interactive: bool, version: bool, - server: bool, ): """Main entrypoint for the CLI.""" @@ -142,21 +152,7 @@ def main( print_builtin(f"gptme {importlib.metadata.version('gptme-python')}") exit(0) - # log init - logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) - - # init - logger.debug("Started") - load_dotenv() - _load_readline_history() - init_llm(llm) # set up API_KEY and API_BASE - init_tools() - - # Check if subcommand is passed, - # if not, continue with main - if server: - click.echo("Initialization complete, starting server") - return start_server() + init(verbose, llm) if "PYTEST_CURRENT_TEST" in os.environ: interactive = False @@ -242,22 +238,6 @@ def parse_prompt(prompt: str) -> str: break -def start_server(): - """Starts a server and web UI.""" - # if flask not installed, ask the user to install `server` extras - try: - __import__("flask") - except ImportError: - logger.error( - "gptme installed without needed extras for server. " - "Please install gptme with `pip install gptme[server]`" - ) - exit(1) - # noreorder - from gptme.server import main as server_main # fmt: skip - server_main() - - def loop( log: LogManager, no_confirm: bool, diff --git a/gptme/cli_server.py b/gptme/cli_server.py new file mode 100644 index 00000000..f626564b --- /dev/null +++ b/gptme/cli_server.py @@ -0,0 +1,33 @@ +import click + +from gptme.util import logger + +from .cli import init + + +@click.command("gptme-server") +@click.option("-v", "--verbose", is_flag=True, help="Verbose output.") +@click.option( + "--llm", + default="openai", + help="LLM to use.", + type=click.Choice(["openai", "local"]), +) +def main(verbose, llm): + """Starts a server and web UI.""" + init(verbose, llm) + + # if flask not installed, ask the user to install `server` extras + try: + __import__("flask") + except ImportError: + logger.error( + "gptme installed without needed extras for server. " + "Install them with `pip install gptme-python[server]`" + ) + exit(1) + click.echo("Initialization complete, starting server") + + # noreorder + from gptme.server import main as server_main # fmt: skip + server_main() diff --git a/pyproject.toml b/pyproject.toml index 312c16b4..2e4b85cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ packages = [ [tool.poetry.scripts] gptme = "gptme.cli:main" +gptme-server = "gptme.cli_server:main" [tool.poetry.dependencies] python = "^3.10"