diff --git a/libs/cli/langchain_cli/cli.py b/libs/cli/langchain_cli/cli.py index 2be2d9ea9da..e80e893403a 100644 --- a/libs/cli/langchain_cli/cli.py +++ b/libs/cli/langchain_cli/cli.py @@ -1,4 +1,3 @@ -import subprocess from typing import Optional import typer @@ -24,12 +23,12 @@ def start( """ Start the LangServe instance, whether it's a hub package or a serve project. """ - cmd = ["poetry", "run", "poe", "start"] - if port is not None: - cmd += ["--port", str(port)] - if host is not None: - cmd += ["--host", host] - subprocess.run(cmd) + + # try starting hub package, if error, try langserve + try: + hub.start(port=port, host=host) + except KeyError: + serve.start(port=port, host=host) if __name__ == "__main__": diff --git a/libs/cli/langchain_cli/namespaces/hub.py b/libs/cli/langchain_cli/namespaces/hub.py index cc4b11b77b8..9ac4a81635b 100644 --- a/libs/cli/langchain_cli/namespaces/hub.py +++ b/libs/cli/langchain_cli/namespaces/hub.py @@ -9,8 +9,11 @@ from pathlib import Path from typing import Optional import typer +from langserve.packages import get_langserve_export from typing_extensions import Annotated +from langchain_cli.utils.packages import get_package_root + hub = typer.Typer(no_args_is_help=True, add_completion=False) @@ -19,9 +22,7 @@ def new( name: Annotated[str, typer.Argument(help="The name of the folder to create")], with_poetry: Annotated[ bool, - typer.Option( - "--with-poetry/--no-poetry", help="Don't run poetry install" - ), + typer.Option("--with-poetry/--no-poetry", help="Don't run poetry install"), ] = False, ): """ @@ -82,9 +83,23 @@ def start( """ Starts a demo LangServe instance for this hub package. """ - cmd = ["poetry", "run", "poe", "start"] - if port is not None: - cmd += ["--port", str(port)] - if host is not None: - cmd += ["--host", host] - subprocess.run(cmd) + # load pyproject.toml + project_dir = get_package_root() + pyproject = project_dir / "pyproject.toml" + + # get langserve export - throws KeyError if invalid + get_langserve_export(pyproject) + + port_str = str(port) if port is not None else "8000" + host_str = host if host is not None else "127.0.0.1" + + command = [ + "uvicorn", + "langchain_cli.dev_scripts:create_demo_server", + "--reload", + "--port", + port_str, + "--host", + host_str, + ] + subprocess.run(command) diff --git a/libs/cli/langchain_cli/namespaces/serve.py b/libs/cli/langchain_cli/namespaces/serve.py index 6ff618905f2..5a077d12955 100644 --- a/libs/cli/langchain_cli/namespaces/serve.py +++ b/libs/cli/langchain_cli/namespaces/serve.py @@ -218,13 +218,15 @@ def start( host: Annotated[ Optional[str], typer.Option(help="The host to run the server on") ] = None, + app: Annotated[Optional[str], typer.Option(help="The app to run")] = None, ) -> None: """ Starts the LangServe instance. """ - cmd = ["poetry", "run", "poe", "start"] - if port is not None: - cmd += ["--port", str(port)] - if host is not None: - cmd += ["--host", host] + + app_str = app if app is not None else "app.server:app" + port_str = str(port) if port is not None else "8000" + host_str = host if host is not None else "127.0.0.1" + + cmd = ["uvicorn", app_str, "--reload", "--port", port_str, "--host", host_str] subprocess.run(cmd) diff --git a/libs/cli/langchain_cli/package_template/pyproject.toml b/libs/cli/langchain_cli/package_template/pyproject.toml index 3da459f7709..eaa5924cc5d 100644 --- a/libs/cli/langchain_cli/package_template/pyproject.toml +++ b/libs/cli/langchain_cli/package_template/pyproject.toml @@ -11,7 +11,6 @@ langchain = ">=0.0.313, <0.1" openai = "^0.28.1" [tool.poetry.group.dev.dependencies] -poethepoet = "^0.24.1" langchain-cli = ">=0.0.4" fastapi = "^0.104.0" sse-starlette = "^1.6.5" @@ -20,13 +19,6 @@ sse-starlette = "^1.6.5" export_module = "__package_name__.chain" export_attr = "chain" -[tool.poe.tasks.start] -cmd="uvicorn langchain_cli.dev_scripts:create_demo_server --reload --port $port --host $host" -args = [ - {name = "port", help = "port to run on", default = "8000"}, - {name = "host", help = "host to run on", default = "127.0.0.1"} -] - [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/libs/cli/langchain_cli/project_template/app/server.py b/libs/cli/langchain_cli/project_template/app/server.py index ce4a3c4ee71..7347b804585 100644 --- a/libs/cli/langchain_cli/project_template/app/server.py +++ b/libs/cli/langchain_cli/project_template/app/server.py @@ -9,4 +9,4 @@ add_routes(app, NotImplemented) if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8001) + uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/libs/cli/langchain_cli/project_template/pyproject.toml b/libs/cli/langchain_cli/project_template/pyproject.toml index d4a177a1c46..eba3def365f 100644 --- a/libs/cli/langchain_cli/project_template/pyproject.toml +++ b/libs/cli/langchain_cli/project_template/pyproject.toml @@ -14,17 +14,9 @@ fastapi = "^0.103.2" langserve = ">=0.0.16" [tool.poetry.group.dev.dependencies] -poethepoet = "^0.24.1" uvicorn = "^0.23.2" pygithub = "^2.1.1" -[tool.poe.tasks.start] -cmd="uvicorn app.server:app --reload --port $port --host $host" -args = [ - {name = "port", help = "port to run on", default = "8000"}, - {name = "host", help = "host to run on", default = "127.0.0.1"} -] - [build-system] requires = ["poetry-core"] diff --git a/libs/cli/pyproject.toml b/libs/cli/pyproject.toml index 8e484687672..d67d9252b62 100644 --- a/libs/cli/pyproject.toml +++ b/libs/cli/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langchain-cli" -version = "0.0.5" +version = "0.0.6" description = "CLI for interacting with LangChain" authors = ["Erick Friis "] readme = "README.md" diff --git a/templates/README.md b/templates/README.md index f5974ae1488..2f0108dcb89 100644 --- a/templates/README.md +++ b/templates/README.md @@ -2,6 +2,10 @@ Templates for a fully functioning app that can be hosted by LangServe. +Some other helpful docs: + +- [Templates] + ## Usage To use, first install the LangChain CLI. @@ -13,7 +17,7 @@ pip install -U langchain-cli Then, install `langserve`: ```shell -pip install "langserve[all]" +pip install -U "langserve[all]" ``` Next, create a new LangChain project: @@ -47,7 +51,7 @@ You then need to install this package so you can use it in the langserve app: pip install -e packages/$PROJECT_NAME ``` -We install it with `-e` so that if we modify the template at all (which we likely will) the changes are updated. +We install it with `-e` so that if you modify the template at all (which you likely will) the changes are updated. In order to have LangServe use this project, you then need to modify `app/server.py`. Specifically, you should add something like: @@ -66,7 +70,7 @@ add_routes(app, chain) You can then spin up production-ready endpoints, along with a playground, by running: ```shell -python app/server.py +langchain start ``` ## Adding a template