How to implement an integration package
This guide walks through the process of implementing a LangChain integration package.
Integration packages are just Python packages that can be installed with pip install <your-package>
,
which contain classes that are compatible with LangChain's core interfaces.
We will cover:
- How to implement components, such as chat models and vector stores, that adhere to the LangChain interface;
- (Optional) How to bootstrap a new integration package.
Implementing LangChain components
LangChain components are subclasses of base classes in langchain-core. Examples include chat models, vector stores, tools, embedding models and retrievers.
Your integration package will typically implement a subclass of at least one of these components. Expand the tabs below to see details on each.
Chat models
Refer to the Custom Chat Model Guide guide for detail on a starter chat model implementation.
The model from the Custom Chat Model Guide is tested against the standard unit and integration tests in the LangChain Github repository. You can also access that implementation directly from Github here.
Vector stores
Your vector store implementation will depend on your chosen database technology.
langchain-core
includes a minimal
in-memory vector store
that we can use as a guide. You can access the code here.
All vector stores must inherit from the VectorStore base class. This interface consists of methods for writing, deleting and searching for documents in the vector store.
VectorStore
supports a variety of synchronous and asynchronous search types (e.g.,
nearest-neighbor or maximum marginal relevance), as well as interfaces for adding
documents to the store. See the API Reference
for all supported methods. The required methods are tabulated below:
Method/Property | Description |
---|---|
add_documents | Add documents to the vector store. |
delete | Delete selected documents from vector store (by IDs) |
get_by_ids | Get selected documents from vector store (by IDs) |
similarity_search | Get documents most similar to a query. |
embeddings (property) | Embeddings object for vector store. |
from_texts | Instantiate vector store via adding texts. |
Note that InMemoryVectorStore
implements some optional search types, as well as
convenience methods for loading and dumping the object to a file, but this is not
necessary for all implementations.
The in-memory vector store is tested against the standard tests in the LangChain Github repository.
(Optional) bootstrapping a new integration package
In this guide, we will be using Poetry for dependency management and packaging, and you're welcome to use any other tools you prefer.
Prerequisites
Boostrapping a new Python package with Poetry
First, install Poetry:
pip install poetry
Next, come up with a name for your package. For this guide, we'll use langchain-parrot-link
.
You can confirm that the name is available on PyPi by searching for it on the PyPi website.
Next, create your new Python package with Poetry, and navigate into the new directory with cd
:
poetry new langchain-parrot-link
cd langchain-parrot-link
Add main dependencies using Poetry, which will add them to your pyproject.toml
file:
poetry add langchain-core
We will also add some test
dependencies in a separate poetry dependency group. If
you are not using Poetry, we recommend adding these in a way that won't package them
with your published package, or just installing them separately when you run tests.
langchain-tests
will provide the standard tests we will use later.
We recommended pinning these to the latest version:
Note: Replace <latest_version>
with the latest version of langchain-tests
below.
poetry add --group test pytest pytest-socket pytest-asyncio langchain-tests==<latest_version>
And finally, have poetry set up a virtual environment with your dependencies, as well as your integration package:
poetry install --with test
You're now ready to start writing your integration package!
Writing your integration
Let's say you're building a simple integration package that provides a ChatParrotLink
chat model integration for LangChain. Here's a simple example of what your project
structure might look like:
langchain-parrot-link/
├── langchain_parrot_link/
│ ├── __init__.py
│ └── chat_models.py
├── tests/
│ ├── __init__.py
│ └── test_chat_models.py
├── pyproject.toml
└── README.md
All of these files should already exist from step 1, except for
chat_models.py
and test_chat_models.py
! We will implement test_chat_models.py
later, following the standard tests guide.
For chat_models.py
, simply paste the contents of the chat model implementation
above.
Push your package to a public Github repository
This is only required if you want to publish your integration in the LangChain documentation.
- Create a new repository on GitHub.
- Push your code to the repository.
- Confirm that your repository is viewable by the public (e.g. in a private browsing window, where you're not logged into Github).
Next Steps
Now that you've implemented your package, you can move on to testing your integration for your integration and successfully run them.