Skip to content

Commit

Permalink
refactor: extracted server stuff to seperate script gptme-server
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 31, 2023
1 parent 9ac4fa0 commit e871a2c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
```

Expand Down
4 changes: 4 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ CLI Reference
.. click:: gptme.cli:main
:prog: gptme
:nested: full

.. click:: gptme.cli_server:main
:prog: gptme-server
:nested: full
46 changes: 13 additions & 33 deletions gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -133,7 +144,6 @@ def main(
show_hidden: bool,
interactive: bool,
version: bool,
server: bool,
):
"""Main entrypoint for the CLI."""

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions gptme/cli_server.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ packages = [

[tool.poetry.scripts]
gptme = "gptme.cli:main"
gptme-server = "gptme.cli_server:main"

[tool.poetry.dependencies]
python = "^3.10"
Expand Down

0 comments on commit e871a2c

Please sign in to comment.