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

LangChain cli fix a few bugs #11573

Merged
merged 1 commit into from
Oct 9, 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
11 changes: 6 additions & 5 deletions libs/langchain/langchain/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

from typing_extensions import Annotated

from langchain.cli.create_repo.base import create, is_poetry_installed
from langchain.cli.create_repo.pypi_name import is_name_taken, lint_name
from langchain.cli.create_repo.user_info import get_git_user_email, get_git_user_name

# Keep this import here so that we can check if Typer is installed
try:
import typer
except ImportError:
raise ImportError(
"Typer must be installed to use the CLI. "
"You can install it with `pip install typer`."
"You can install it with `pip install typer` or install LangChain "
'with the [cli] extra like `pip install "langchain[cli]"`.'
)

from langchain.cli.create_repo.base import create, is_poetry_installed
from langchain.cli.create_repo.pypi_name import is_name_taken, lint_name
from langchain.cli.create_repo.user_info import get_git_user_email, get_git_user_name

app = typer.Typer(no_args_is_help=False, add_completion=False)

Expand Down
26 changes: 23 additions & 3 deletions libs/langchain/langchain/cli/create_repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,27 @@ def _copy_template_files(
"""
for template_directory_path in template_directories:
for template_file_path in template_directory_path.glob("**/*"):
# Ignore __pycache__ directories and their contents
if "__pycache__" in template_file_path.parts:
continue

relative_template_file_path = UnderscoreTemplate(
str(template_file_path.relative_to(template_directory_path))
).substitute(project_name_identifier=project_name_identifier)
project_file_path = project_directory_path / relative_template_file_path
if template_file_path.is_dir():
project_file_path.mkdir(parents=True, exist_ok=True)
else:
try:
content = template_file_path.read_text(encoding="utf-8")
except UnicodeDecodeError as e:
raise RuntimeError(
"Encountered an error while reading a "
f"template file {template_file_path}"
) from e

project_file_path.write_text(
UnderscoreTemplate(template_file_path.read_text()).substitute(
UnderscoreTemplate(content).substitute(
project_name=project_name,
project_name_identifier=project_name_identifier,
author_name=author_name,
Expand Down Expand Up @@ -146,7 +158,11 @@ def _init_git(project_directory_path: Path) -> None:
typer.echo(
f"\n{typer.style('Initializing git...', bold=True, fg=typer.colors.GREEN)}"
)
subprocess.run(["git", "init"], cwd=project_directory_path)
try:
subprocess.run(["git", "init"], cwd=project_directory_path)
except FileNotFoundError:
typer.echo("Git not found. Skipping git initialization.")
return

# 7. Create initial commit
subprocess.run(["git", "add", "."], cwd=project_directory_path)
Expand Down Expand Up @@ -243,4 +259,8 @@ def create(

def is_poetry_installed() -> bool:
"""Check if Poetry is installed."""
return subprocess.run(["poetry", "--version"], capture_output=True).returncode == 0
try:
result = subprocess.run(["poetry", "--version"], capture_output=True)
return result.returncode == 0
except FileNotFoundError:
return False