Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli updates, 0.0.16 #13034

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions libs/cli/langchain_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from langchain_cli.namespaces import app as app_namespace
from langchain_cli.namespaces import template as template_namespace
from langchain_cli.utils.packages import get_langserve_export, get_package_root

__version__ = "0.0.15"
__version__ = "0.0.16"

app = typer.Typer(no_args_is_help=True, add_completion=False)
app.add_typer(
Expand Down Expand Up @@ -49,11 +50,17 @@ def serve(
Start the LangServe app, whether it's a template or an app.
"""

# try starting template package, if error, try langserve
# see if is a template
try:
template_namespace.serve(port=port, host=host)
project_dir = get_package_root()
pyproject = project_dir / "pyproject.toml"
get_langserve_export(pyproject)
except KeyError:
# not a template
app_namespace.serve(port=port, host=host)
else:
# is a template
template_namespace.serve(port=port, host=host)


if __name__ == "__main__":
Expand Down
108 changes: 67 additions & 41 deletions libs/cli/langchain_cli/namespaces/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion libs/cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langchain-cli"
version = "0.0.15"
version = "0.0.16"
description = "CLI for interacting with LangChain"
authors = ["Erick Friis <[email protected]>"]
readme = "README.md"
Expand Down