From ffe9a6d5b872700f1f1b3aec5e7965ebffad519f Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Thu, 18 Jul 2024 09:04:14 -0700 Subject: [PATCH] cli: Add dockerfile command to write dockerfile --- libs/cli/langgraph_cli/cli.py | 15 +++++++++++++ libs/cli/langgraph_cli/config.py | 26 +++++++++++++--------- libs/cli/tests/unit_tests/test_config.json | 8 ++++--- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/libs/cli/langgraph_cli/cli.py b/libs/cli/langgraph_cli/cli.py index 119deabed..83d14d218 100644 --- a/libs/cli/langgraph_cli/cli.py +++ b/libs/cli/langgraph_cli/cli.py @@ -452,6 +452,21 @@ def build( _build(runner, set, config, config_json, platform, base_image, pull, tag) +@OPT_CONFIG +@click.argument("save_path", type=click.Path(resolve_path=True)) +@cli.command(help="Generate a Dockerfile for langgraph API server") +@log_command +def dockerfile(save_path: pathlib.Path, config: pathlib.Path): + with open(config) as f: + config_json = langgraph_cli.config.validate_config(json.load(f)) + with open(save_path, "w") as f: + f.write( + langgraph_cli.config.config_to_docker( + config, config_json, "langchain/langgraph-api" + ) + ) + + def prepare_args_and_stdin( *, capabilities: DockerCapabilities, diff --git a/libs/cli/langgraph_cli/config.py b/libs/cli/langgraph_cli/config.py index efbde63d7..177125c84 100644 --- a/libs/cli/langgraph_cli/config.py +++ b/libs/cli/langgraph_cli/config.py @@ -232,8 +232,7 @@ def config_to_docker(config_path: pathlib.Path, config: Config, base_image: str) '[tool.setuptools.package-data]' \\ '"*" = ["**/*"]'; do \\ echo "$line" >> /deps/__outer_{fullpath.name}/pyproject.toml; \\ - done -""" + done""" for fullpath, (relpath, destpath) in local_deps.faux_pkgs.items() ) local_pkgs_str = os.linesep.join( @@ -241,19 +240,24 @@ def config_to_docker(config_path: pathlib.Path, config: Config, base_image: str) for fullpath, relpath in local_deps.real_pkgs.items() ) + installs = f"{os.linesep}{os.linesep}".join( + filter( + None, + [ + pip_config_file_str, + pip_pkgs_str, + pip_reqs_str, + local_pkgs_str, + faux_pkgs_str, + ], + ) + ) + return f"""FROM {base_image}:{config['python_version']} {os.linesep.join(config["dockerfile_lines"])} -{pip_config_file_str} - -{pip_pkgs_str} - -{pip_reqs_str} - -{local_pkgs_str} - -{faux_pkgs_str} +{installs} RUN {pip_install} -e /deps/* diff --git a/libs/cli/tests/unit_tests/test_config.json b/libs/cli/tests/unit_tests/test_config.json index b75d747c4..8062b1719 100644 --- a/libs/cli/tests/unit_tests/test_config.json +++ b/libs/cli/tests/unit_tests/test_config.json @@ -1,13 +1,15 @@ { "python_version": "3.12", "pip_config_file": "pipconfig.txt", - "dockerfile_lines": ["ARG meow"], + "dockerfile_lines": [ + "ARG meow" + ], "dependencies": [ "langchain_openai", "." ], "graphs": { - "agent": "tests/unit_tests/agent.py:graph" + "agent": "graphs/agent.py:graph" }, "env": ".env" -} \ No newline at end of file +}