Using the LangChain CLI
Relevant source files
The following files were used as context for generating this wiki page:
The langchain-cli is a command-line interface designed to streamline the development and management of LangChain applications. It is particularly useful for getting started with LangChain Templates and managing LangServe projects. The CLI provides tools for creating new projects, adding and removing packages, serving applications, and developing custom templates. Additionally, the CLI can assist in automatically upgrading code to use non-deprecated imports, which is helpful when migrating between LangChain versions.
Sources: libs/cli/README.md, MIGRATE.md:9-10
Installation
To use the LangChain CLI, it must first be installed via pip.
pip install -U langchain-cli
Sources: libs/cli/langchain_cli/project_template/README.md:5-7, libs/cli/langchain_cli/package_template/README.md:7-9
Command Structure
The CLI is organized into three main command groups: app, template, and serve. These groups manage LangServe applications, template development, and serving processes, respectively.
This diagram shows the high-level command structure of the LangChain CLI.
Global Options
| Option | Alias | Description |
|---|---|---|
--help | Show the help message and exit. | |
--version | -v | Print the current CLI version. |
Sources: libs/cli/DOCS.md:8-11
Managing LangServe Applications (app)
The langchain app command group is the primary tool for managing LangServe applications. It allows for project creation, dependency management, and serving.
Sources: libs/cli/DOCS.md:18-19
Creating a New Application
A new LangServe application can be created using the app new command. This command scaffolds a new project directory with a default structure.
langchain app new my-app --package my-first-package
This diagram illustrates the workflow for creating a new LangServe application.
langchain app new
| Argument | Description | Required |
|---|---|---|
NAME | The name of the folder to create for the new application. | Yes |
| Option | Description |
|---|---|
--package TEXT | A package to seed the project with. |
--help | Show the help message and exit. |
Sources: libs/cli/DOCS.md:55-62
Adding Packages
You can add packages to an existing LangServe project using langchain app add. Packages can be official LangChain templates or custom packages from a Git repository.
Add an official template:
langchain app add extraction-openai-functions
Add a custom package from GitHub:
# Using owner/repo format
langchain app add --repo $OWNER/$REPO
# Using a full git URL
langchain app add git+https://github.com/hwchase17/chain-of-verification
You can also specify a custom API mount point for the package. If not provided, it defaults to /{package_name}.
langchain app add $PROJECT_NAME --api_path=/my/custom/path/rag
Sources: libs/cli/langchain_cli/project_template/README.md:13-26, libs/cli/DOCS.md:31-35
langchain app add
| Argument | Description | Required |
|---|---|---|
[DEPENDENCIES]... | The dependency to add. This can be a template name or a git URL. | No |
| Option | Description |
|---|---|
--api-path TEXT | The API path to mount the package on. |
--project-dir PATH | The project directory. |
--repo TEXT | Install templates from a specific GitHub repo. |
--branch TEXT | Install templates from a specific branch of a repo. |
--help | Show the help message and exit. |
Sources: libs/cli/DOCS.md:38-49
Removing Packages
Packages are removed from an application by specifying their API path.
langchain app remove my/custom/path/rag
Sources: libs/cli/langchain_cli/project_template/README.md:28-30, libs/cli/DOCS.md:64-66
langchain app remove
| Argument | Description | Required |
|---|---|---|
API_PATHS... | The API paths of the packages to remove. | Yes |
| Option | Description |
|---|---|
--help | Show the help message and exit. |
Sources: libs/cli/DOCS.md:69-75
Serving an Application
Both langchain app serve and the top-level langchain serve command can be used to start the LangServe application.
langchain serve
or
langchain app serve --port 8080 --host 0.0.0.0
Once running, the API documentation is available at http://<host>:<port>/docs and the playground for a specific package is at http://<host>:<port>/{package_name}/playground.
Sources: libs/cli/langchain_cli/project_template/README.md:38-40, libs/cli/langchain_cli/package_template/README.md:29-37, libs/cli/DOCS.md:77-96
serve Commands
| Command | Description |
|---|---|
langchain app serve | Starts the LangServe app from within an application project. |
langchain serve | A general command to start a LangServe app, whether it's a template or a full application. |
| Option | Description |
|---|---|
--port INTEGER | The port to run the server on. |
--host TEXT | The host to run the server on. |
--app TEXT | The app to run, e.g., app.server:app. (Only for app serve) |
--help | Show the help message and exit. |
Sources: libs/cli/DOCS.md:77-96
Developing Templates (template)
The langchain template command group provides tools for developers who want to create their own installable templates.
Sources: libs/cli/DOCS.md:98-99
Creating a New Template
To create a new template package, use the template new command.
langchain template new my-new-template
This command creates a new directory with the necessary structure for a template package.
Sources: libs/cli/DOCS.md:108-110
langchain template new
| Argument | Description | Required |
|---|---|---|
NAME | The name of the folder to create for the template. | Yes |
| Option | Description | Default |
|---|---|---|
--with-poetry / --no-poetry | Run poetry install after creation. | no-poetry |
--help | Show the help message and exit. |
Sources: libs/cli/DOCS.md:113-119
Serving a Template Demo
While developing, you can serve a demo application for your template to test it.
# Run from within the template directory
langchain template serve
This starts a FastAPI app, making the template accessible for testing.
Sources: libs/cli/langchain_cli/package_template/README.md:29-31, libs/cli/DOCS.md:121-123
Project Usage
LangSmith Integration
For tracing, monitoring, and debugging, you can integrate your LangServe application with LangSmith. This requires setting a few environment variables.
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>
export LANGSMITH_PROJECT=<your-project> # if not specified, defaults to "default"
Sources: libs/cli/langchain_cli/project_template/README.md:32-37, libs/cli/langchain_cli/package_template/README.md:21-26
Running with Docker
Projects created with langchain app new include a Dockerfile for easy containerization and deployment.
1. Build the Docker image:
docker build . -t my-langserve-app
2. Run the Docker container:
You must pass necessary environment variables, like API keys, to the container. The following command injects an OpenAI API key and exposes port 8080.
docker run -e OPENAI_API_KEY=$OPENAI_API_KEY -p 8080:8080 my-langserve-app
Sources: libs/cli/langchain_cli/project_template/README.md:42-60
Conclusion
The LangChain CLI is an essential utility for developers working with the LangChain framework. It simplifies the creation, management, and deployment of LangServe applications by providing a structured set of commands for scaffolding projects, managing template-based packages, and running a local server. Its integration with Docker and LangSmith further enhances the development and operational workflow. The CLI also plays a role in easing code migrations between different versions of LangChain.
Sources: libs/cli/README.md, libs/cli/DOCS.md, MIGRATE.md:9-10