2023-10-25 18:06:58 +00:00
|
|
|
from typing import Optional
|
2023-10-27 00:01:31 +00:00
|
|
|
|
|
|
|
import typer
|
2023-10-25 18:06:58 +00:00
|
|
|
from typing_extensions import Annotated
|
|
|
|
|
2024-09-13 21:38:45 +00:00
|
|
|
from langchain_cli._version import __version__
|
2023-10-30 23:39:39 +00:00
|
|
|
from langchain_cli.namespaces import app as app_namespace
|
2023-12-13 16:55:30 +00:00
|
|
|
from langchain_cli.namespaces import integration as integration_namespace
|
2023-10-30 23:39:39 +00:00
|
|
|
from langchain_cli.namespaces import template as template_namespace
|
2024-09-13 21:38:45 +00:00
|
|
|
from langchain_cli.namespaces.migrate import main as migrate_namespace
|
2023-11-08 15:47:30 +00:00
|
|
|
from langchain_cli.utils.packages import get_langserve_export, get_package_root
|
2023-10-25 18:06:58 +00:00
|
|
|
|
|
|
|
app = typer.Typer(no_args_is_help=True, add_completion=False)
|
2023-10-30 23:39:39 +00:00
|
|
|
app.add_typer(
|
|
|
|
template_namespace.package_cli, name="template", help=template_namespace.__doc__
|
|
|
|
)
|
|
|
|
app.add_typer(app_namespace.app_cli, name="app", help=app_namespace.__doc__)
|
2023-12-13 16:55:30 +00:00
|
|
|
app.add_typer(
|
|
|
|
integration_namespace.integration_cli,
|
|
|
|
name="integration",
|
|
|
|
help=integration_namespace.__doc__,
|
|
|
|
)
|
2023-10-25 18:06:58 +00:00
|
|
|
|
2024-09-13 21:38:45 +00:00
|
|
|
app.command(
|
|
|
|
name="migrate",
|
|
|
|
context_settings={
|
|
|
|
# Let Grit handle the arguments
|
|
|
|
"allow_extra_args": True,
|
|
|
|
"ignore_unknown_options": True,
|
|
|
|
},
|
|
|
|
)(
|
|
|
|
migrate_namespace.migrate,
|
|
|
|
)
|
2024-04-26 14:50:21 +00:00
|
|
|
|
|
|
|
|
2023-10-31 21:50:04 +00:00
|
|
|
def version_callback(show_version: bool) -> None:
|
|
|
|
if show_version:
|
|
|
|
typer.echo(f"langchain-cli {__version__}")
|
|
|
|
raise typer.Exit()
|
|
|
|
|
|
|
|
|
|
|
|
@app.callback()
|
|
|
|
def main(
|
|
|
|
version: bool = typer.Option(
|
|
|
|
False,
|
|
|
|
"--version",
|
|
|
|
"-v",
|
|
|
|
help="Print the current CLI version.",
|
|
|
|
callback=version_callback,
|
|
|
|
is_eager=True,
|
|
|
|
),
|
|
|
|
):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-10-25 18:06:58 +00:00
|
|
|
@app.command()
|
2023-10-30 23:39:39 +00:00
|
|
|
def serve(
|
2023-10-25 18:06:58 +00:00
|
|
|
*,
|
|
|
|
port: Annotated[
|
|
|
|
Optional[int], typer.Option(help="The port to run the server on")
|
|
|
|
] = None,
|
|
|
|
host: Annotated[
|
|
|
|
Optional[str], typer.Option(help="The host to run the server on")
|
|
|
|
] = None,
|
|
|
|
) -> None:
|
|
|
|
"""
|
2023-10-30 23:39:39 +00:00
|
|
|
Start the LangServe app, whether it's a template or an app.
|
2023-10-25 18:06:58 +00:00
|
|
|
"""
|
2023-10-27 19:06:46 +00:00
|
|
|
|
2023-11-08 15:47:30 +00:00
|
|
|
# see if is a template
|
2023-10-27 19:06:46 +00:00
|
|
|
try:
|
2023-11-08 15:47:30 +00:00
|
|
|
project_dir = get_package_root()
|
|
|
|
pyproject = project_dir / "pyproject.toml"
|
|
|
|
get_langserve_export(pyproject)
|
2023-10-27 19:06:46 +00:00
|
|
|
except KeyError:
|
2023-11-08 15:47:30 +00:00
|
|
|
# not a template
|
2023-10-30 23:39:39 +00:00
|
|
|
app_namespace.serve(port=port, host=host)
|
2023-11-08 15:47:30 +00:00
|
|
|
else:
|
|
|
|
# is a template
|
|
|
|
template_namespace.serve(port=port, host=host)
|
2023-10-25 18:06:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app()
|