mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-06 05:08:20 +00:00
more cli interactivity, bugfix (#13360)
This commit is contained in:
parent
3596be5210
commit
7c3066f9ec
@ -36,12 +36,11 @@ app_cli = typer.Typer(no_args_is_help=True, add_completion=False)
|
|||||||
@app_cli.command()
|
@app_cli.command()
|
||||||
def new(
|
def new(
|
||||||
name: Annotated[
|
name: Annotated[
|
||||||
str,
|
Optional[str],
|
||||||
typer.Option(
|
typer.Argument(
|
||||||
help="The name of the folder to create",
|
help="The name of the folder to create",
|
||||||
prompt="What folder would you like to create?",
|
|
||||||
),
|
),
|
||||||
],
|
] = None,
|
||||||
*,
|
*,
|
||||||
package: Annotated[
|
package: Annotated[
|
||||||
Optional[List[str]],
|
Optional[List[str]],
|
||||||
@ -55,21 +54,56 @@ def new(
|
|||||||
is_flag=True,
|
is_flag=True,
|
||||||
),
|
),
|
||||||
] = None,
|
] = None,
|
||||||
|
noninteractive: Annotated[
|
||||||
|
bool,
|
||||||
|
typer.Option(
|
||||||
|
"--non-interactive/--interactive",
|
||||||
|
help="Don't prompt for any input",
|
||||||
|
is_flag=True,
|
||||||
|
),
|
||||||
|
] = False,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Create a new LangServe application.
|
Create a new LangServe application.
|
||||||
"""
|
"""
|
||||||
has_packages = package is not None and len(package) > 0
|
has_packages = package is not None and len(package) > 0
|
||||||
pip_bool = False
|
|
||||||
if pip is None and has_packages:
|
if noninteractive:
|
||||||
pip_bool = typer.confirm(
|
if name is None:
|
||||||
"Would you like to `pip install -e` the template(s)?",
|
raise typer.BadParameter("name is required when --non-interactive is set")
|
||||||
default=False,
|
name_str = name
|
||||||
|
pip_bool = bool(pip) # None should be false
|
||||||
|
else:
|
||||||
|
name_str = (
|
||||||
|
name if name else typer.prompt("What folder would you like to create?")
|
||||||
)
|
)
|
||||||
|
if not has_packages:
|
||||||
|
package = []
|
||||||
|
package_prompt = "What package would you like to add? (leave blank to skip)"
|
||||||
|
while True:
|
||||||
|
package_str = typer.prompt(
|
||||||
|
package_prompt, default="", show_default=False
|
||||||
|
)
|
||||||
|
if not package_str:
|
||||||
|
break
|
||||||
|
package.append(package_str)
|
||||||
|
package_prompt = (
|
||||||
|
f"{len(package)} added. Any more packages (leave blank to end)?"
|
||||||
|
)
|
||||||
|
|
||||||
|
has_packages = len(package) > 0
|
||||||
|
|
||||||
|
pip_bool = False
|
||||||
|
if pip is None and has_packages:
|
||||||
|
pip_bool = typer.confirm(
|
||||||
|
"Would you like to install these templates into your environment "
|
||||||
|
"with pip?",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
# copy over template from ../project_template
|
# copy over template from ../project_template
|
||||||
project_template_dir = Path(__file__).parents[1] / "project_template"
|
project_template_dir = Path(__file__).parents[1] / "project_template"
|
||||||
destination_dir = Path.cwd() / name if name != "." else Path.cwd()
|
destination_dir = Path.cwd() / name_str if name_str != "." else Path.cwd()
|
||||||
app_name = name if name != "." else Path.cwd().name
|
app_name = name_str if name_str != "." else Path.cwd().name
|
||||||
shutil.copytree(project_template_dir, destination_dir, dirs_exist_ok=name == ".")
|
shutil.copytree(project_template_dir, destination_dir, dirs_exist_ok=name == ".")
|
||||||
|
|
||||||
readme = destination_dir / "README.md"
|
readme = destination_dir / "README.md"
|
||||||
|
@ -129,3 +129,15 @@ def serve(
|
|||||||
port=port if port is not None else 8000,
|
port=port if port is not None else 8000,
|
||||||
host=host_str,
|
host=host_str,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@package_cli.command()
|
||||||
|
def list(contains: Annotated[Optional[str], typer.Argument()] = None) -> None:
|
||||||
|
"""
|
||||||
|
List all or search for available templates.
|
||||||
|
"""
|
||||||
|
from langchain_cli.utils.github import list_packages
|
||||||
|
|
||||||
|
packages = list_packages(contains=contains)
|
||||||
|
for package in packages:
|
||||||
|
typer.echo(package)
|
||||||
|
29
libs/cli/langchain_cli/utils/github.py
Normal file
29
libs/cli/langchain_cli/utils/github.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import http.client
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def list_packages(*, contains: Optional[str] = None):
|
||||||
|
conn = http.client.HTTPSConnection("api.github.com")
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Accept": "application/vnd.github+json",
|
||||||
|
"X-GitHub-Api-Version": "2022-11-28",
|
||||||
|
"User-Agent": "langchain-cli",
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.request(
|
||||||
|
"GET", "/repos/langchain-ai/langchain/contents/templates", headers=headers
|
||||||
|
)
|
||||||
|
res = conn.getresponse()
|
||||||
|
|
||||||
|
res_str = res.read()
|
||||||
|
|
||||||
|
data = json.loads(res_str)
|
||||||
|
package_names = [
|
||||||
|
p["name"] for p in data if p["type"] == "dir" and p["name"] != "docs"
|
||||||
|
]
|
||||||
|
package_names_filtered = (
|
||||||
|
[p for p in package_names if contains in p] if contains else package_names
|
||||||
|
)
|
||||||
|
return package_names_filtered
|
Loading…
Reference in New Issue
Block a user