Installation and Setup
Relevant source files
The following files were used as context for generating this wiki page:
LangChain is a framework designed for building applications powered by Large Language Models (LLMs). It simplifies development by providing interoperable components and a vast library of integrations. This guide covers the essential steps for installing LangChain packages, setting up your development environment, and using the LangChain Command Line Interface (CLI) to create and manage projects.
The framework is modular, allowing developers to install only the necessary components. The core abstractions are contained in langchain-core, with more complex components in the main langchain package and partner integrations in separate packages. This approach ensures stability and modularity, allowing developers to build robust and scalable LLM applications.
Core Installation
The LangChain ecosystem is composed of several key packages. Installation is managed via pip.
Package Structure
The project is divided into multiple packages to maintain modularity and stability. The core package, langchain-core, contains the fundamental abstractions like Runnables, which are central to the LangChain Expression Language (LCEL).
The diagram below illustrates the hierarchical organization of the LangChain framework.
This diagram shows how applications can depend on various langchain packages, all of which rely on the base abstractions in langchain-core.
Sources: libs/core/README.md:39, libs/text-splitters/README.md:10
Main Packages
The primary package to get started is langchain.
pip install -U langchain
Sources: README.md:20
For core abstractions, you can install langchain-core separately. This is useful if you only need the base interfaces without the higher-level components.
pip install langchain-core
Sources: libs/core/README.md:8
Other specialized packages, like langchain-text-splitters, can be installed as needed.
pip install langchain-text-splitters
Sources: libs/text-splitters/README.md:8
Environment Setup
Proper environment setup is crucial for running LangChain applications, especially for managing API keys and other sensitive credentials.
Environment Variables
Most integrations, such as those with model providers or other services like LangSmith, require API keys to be set as environment variables.
For example, to set up tracing with LangSmith, you need to configure the following 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:27-29
Similarly, partner packages like langchain-fireworks require their own API keys:
export FIREWORKS_API_KEY='your_api_key'
Sources: libs/partners/fireworks/README.md:16
Development Containers
For a consistent development environment, you can use Dev Containers or GitHub Codespaces. These options provide a pre-configured environment with all necessary dependencies.
- Dev Containers: Open in Dev Containers
- GitHub Codespaces: Open in GitHub Codespaces
Sources: README.md:13-14
LangChain CLI for Project Management
The langchain-cli provides tools to create, manage, and serve LangChain applications.
CLI Installation
First, install the LangChain CLI:
pip install -U langchain-cli
Sources: libs/cli/langchain_cli/project_template/README.md:6
CLI Commands
The CLI offers several commands to streamline development. The main commands are under the langchain app group.
| Command | Description |
|---|---|
new | Create a new LangServe application. |
add | Adds a specified template package to the current LangServe app. |
remove | Removes a specified package from the current LangServe app. |
serve | Starts the LangServe app. |
Sources: libs/cli/DOCS.md:17-23
The following flowchart shows the typical workflow for creating and serving a new application using the CLI.
Creating a New Project
To create a new LangServe project, use the new command:
langchain app new my-app
This command creates a new folder my-app with a default project structure. You can also seed the project with packages using the --package flag.
Sources: libs/cli/DOCS.md:52-54
Adding Packages
You can add pre-built templates or custom GitHub repositories to your project.
# Add a template from the official repository
langchain app add extraction-openai-functions
# Add a package from a custom GitHub repo
langchain app add --repo $OWNER/$REPO
When a package is added, you need to update your server.py to expose its runnable. For example, for a package named __package_name__, you would add:
# In server.py
__app_route_code__
This code snippet, provided in the package's README, adds the necessary route to the FastAPI application.
Sources: libs/cli/langchain_cli/package_template/README.md:18-23, libs/cli/DOCS.md:33-36
Serving the Application
To run the application locally, use the serve command from within the project directory:
langchain serve
This starts a FastAPI server, typically at http://localhost:8000. You can access the API documentation at http://127.0.0.1:8000/docs and the playground for a specific package at http://127.0.0.1:8000/<package_name>/playground.
Sources: libs/cli/langchain_cli/package_template/README.md:35-41
Security Best Practices
When setting up and developing LangChain applications, it is critical to follow security best practices, especially since they can interact with external resources like file systems, APIs, and databases.
Core Principles
| Principle | Description |
|---|---|
| Limit Permissions | Scope permissions specifically to the application's needs. Use read-only credentials and sandboxing (e.g., containers) where possible. |
| Anticipate Potential Misuse | Assume any system access or credentials can be used in any way allowed by their permissions. An LLM can make mistakes. |
| Defense in Depth | Combine multiple layered security approaches rather than relying on a single layer of defense. |
Sources: SECURITY.md:7-15
Example Scenarios and Mitigations
The following table outlines potential risks and recommended mitigation strategies.
| Scenario | Mitigation Strategy |
|---|---|
| An agent with file system access is asked to delete critical files or read sensitive information. | Limit the agent to a specific directory and only allow it to access safe files. Consider sandboxing the agent in a container. |
| An agent with write access to an external API is prompted to write malicious data or delete data. | Provide the agent with read-only API keys or limit it to endpoints resistant to misuse. |
| An agent with database access is asked to drop a table or alter the schema. | Scope credentials to only the necessary tables and consider using READ-ONLY credentials. |
Sources: SECURITY.md:22-29
Conclusion
Setting up a LangChain project involves installing the required modular packages, configuring environment variables for integrations, and utilizing the LangChain CLI for efficient project creation and management. By following the installation steps and adhering to security best practices, developers can build powerful, safe, and scalable applications. The CLI, in particular, simplifies the process of scaffolding a new LangServe application, adding functionality through templates, and running a local server for development and testing.