|
|
|
@ -41,10 +41,25 @@ def new(
|
|
|
|
|
Optional[List[str]],
|
|
|
|
|
typer.Option(help="Packages to seed the project with"),
|
|
|
|
|
] = None,
|
|
|
|
|
pip: Annotated[
|
|
|
|
|
Optional[bool],
|
|
|
|
|
typer.Option(
|
|
|
|
|
"--pip/--no-pip",
|
|
|
|
|
help="Pip install the template(s) as editable dependencies",
|
|
|
|
|
is_flag=True,
|
|
|
|
|
),
|
|
|
|
|
] = None,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Create a new LangServe application.
|
|
|
|
|
"""
|
|
|
|
|
has_packages = package is not None and len(package) > 0
|
|
|
|
|
pip_bool = False
|
|
|
|
|
if pip is None and has_packages:
|
|
|
|
|
pip_bool = typer.confirm(
|
|
|
|
|
"Would you like to `pip install -e` the template(s)?",
|
|
|
|
|
default=False,
|
|
|
|
|
)
|
|
|
|
|
# copy over template from ../project_template
|
|
|
|
|
project_template_dir = Path(__file__).parents[1] / "project_template"
|
|
|
|
|
destination_dir = Path.cwd() / name if name != "." else Path.cwd()
|
|
|
|
@ -56,8 +71,8 @@ def new(
|
|
|
|
|
readme.write_text(readme_contents.replace("__app_name__", app_name))
|
|
|
|
|
|
|
|
|
|
# add packages if specified
|
|
|
|
|
if package is not None and len(package) > 0:
|
|
|
|
|
add(package, project_dir=destination_dir)
|
|
|
|
|
if has_packages:
|
|
|
|
|
add(package, project_dir=destination_dir, pip=pip_bool)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app_cli.command()
|
|
|
|
@ -77,6 +92,15 @@ def add(
|
|
|
|
|
branch: Annotated[
|
|
|
|
|
List[str], typer.Option(help="Install templates from a specific branch")
|
|
|
|
|
] = [],
|
|
|
|
|
pip: Annotated[
|
|
|
|
|
bool,
|
|
|
|
|
typer.Option(
|
|
|
|
|
"--pip/--no-pip",
|
|
|
|
|
help="Pip install the template(s) as editable dependencies",
|
|
|
|
|
is_flag=True,
|
|
|
|
|
prompt="Would you like to `pip install -e` the template(s)?",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Adds the specified template to the current LangServe app.
|
|
|
|
@ -164,47 +188,49 @@ def add(
|
|
|
|
|
# Can fail if the cwd is not a parent of the package
|
|
|
|
|
typer.echo("Failed to print install command, continuing...")
|
|
|
|
|
else:
|
|
|
|
|
cmd = ["pip", "install", "-e"] + installed_destination_strs
|
|
|
|
|
cmd_str = " \\\n ".join(installed_destination_strs)
|
|
|
|
|
install_str = f"To install:\n\npip install -e \\\n {cmd_str}"
|
|
|
|
|
typer.echo(install_str)
|
|
|
|
|
|
|
|
|
|
if typer.confirm("Run it?"):
|
|
|
|
|
if pip:
|
|
|
|
|
cmd = ["pip", "install", "-e"] + installed_destination_strs
|
|
|
|
|
cmd_str = " \\\n ".join(installed_destination_strs)
|
|
|
|
|
typer.echo(f"Running: pip install -e \\\n {cmd_str}")
|
|
|
|
|
subprocess.run(cmd, cwd=cwd)
|
|
|
|
|
|
|
|
|
|
if typer.confirm("\nGenerate route code for these packages?", default=True):
|
|
|
|
|
chain_names = []
|
|
|
|
|
for e in installed_exports:
|
|
|
|
|
original_candidate = f'{e["package_name"].replace("-", "_")}_chain'
|
|
|
|
|
candidate = original_candidate
|
|
|
|
|
i = 2
|
|
|
|
|
while candidate in chain_names:
|
|
|
|
|
candidate = original_candidate + "_" + str(i)
|
|
|
|
|
i += 1
|
|
|
|
|
chain_names.append(candidate)
|
|
|
|
|
|
|
|
|
|
api_paths = [
|
|
|
|
|
str(Path("/") / path.relative_to(package_dir))
|
|
|
|
|
for path in installed_destination_paths
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
|
f"from {e['module']} import {e['attr']} as {name}"
|
|
|
|
|
for e, name in zip(installed_exports, chain_names)
|
|
|
|
|
]
|
|
|
|
|
routes = [
|
|
|
|
|
f'add_routes(app, {name}, path="{path}")'
|
|
|
|
|
for name, path in zip(chain_names, api_paths)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
lines = (
|
|
|
|
|
["", "Great! Add the following to your app:\n\n```", ""]
|
|
|
|
|
+ imports
|
|
|
|
|
+ [""]
|
|
|
|
|
+ routes
|
|
|
|
|
+ ["```"]
|
|
|
|
|
)
|
|
|
|
|
typer.echo("\n".join(lines))
|
|
|
|
|
chain_names = []
|
|
|
|
|
for e in installed_exports:
|
|
|
|
|
original_candidate = f'{e["package_name"].replace("-", "_")}_chain'
|
|
|
|
|
candidate = original_candidate
|
|
|
|
|
i = 2
|
|
|
|
|
while candidate in chain_names:
|
|
|
|
|
candidate = original_candidate + "_" + str(i)
|
|
|
|
|
i += 1
|
|
|
|
|
chain_names.append(candidate)
|
|
|
|
|
|
|
|
|
|
api_paths = [
|
|
|
|
|
str(Path("/") / path.relative_to(package_dir))
|
|
|
|
|
for path in installed_destination_paths
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
|
f"from {e['module']} import {e['attr']} as {name}"
|
|
|
|
|
for e, name in zip(installed_exports, chain_names)
|
|
|
|
|
]
|
|
|
|
|
routes = [
|
|
|
|
|
f'add_routes(app, {name}, path="{path}")'
|
|
|
|
|
for name, path in zip(chain_names, api_paths)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
t = (
|
|
|
|
|
"this template"
|
|
|
|
|
if len(chain_names) == 1
|
|
|
|
|
else f"these {len(chain_names)} templates"
|
|
|
|
|
)
|
|
|
|
|
lines = (
|
|
|
|
|
["", f"To use {t}, add the following to your app:\n\n```", ""]
|
|
|
|
|
+ imports
|
|
|
|
|
+ [""]
|
|
|
|
|
+ routes
|
|
|
|
|
+ ["```"]
|
|
|
|
|
)
|
|
|
|
|
typer.echo("\n".join(lines))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app_cli.command()
|
|
|
|
|