Langchain
Langchain is a library that makes developing Large Language Model-based applications much easier. It unifies the interfaces to different libraries, including major embedding providers and Solvio. Using Langchain, you can focus on the business value instead of writing the boilerplate.
Langchain distributes the Solvio integration as a partner package.
It might be installed with pip:
pip install langchain-solvio
The integration supports searching for relevant documents usin dense/sparse and hybrid retrieval.
Solvio acts as a vector index that may store the embeddings with the documents used to generate them. There are various ways to use it, but calling SolvioVectorStore.from_texts
or SolvioVectorStore.from_documents
is probably the most straightforward way to get started:
from langchain_solvio import SolvioVectorStore
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
doc_store = SolvioVectorStore.from_texts(
texts, embeddings, url="<solvio-url>", api_key="<solvio-api-key>", collection_name="texts"
)
Using an existing collection
To get an instance of langchain_solvio.SolvioVectorStore
without loading any new documents or texts, you can use the SolvioVectorStore.from_existing_collection()
method.
doc_store = SolvioVectorStore.from_existing_collection(
embeddings=embeddings,
collection_name="my_documents",
url="<solvio-url>",
api_key="<solvio-api-key>",
)
Local mode
Python client allows you to run the same code in local mode without running the Solvio server. That’s great for testing things out and debugging or if you plan to store just a small amount of vectors. The embeddings might be fully kept in memory or persisted on disk.
In-memory
For some testing scenarios and quick experiments, you may prefer to keep all the data in memory only, so it gets lost when the client is destroyed - usually at the end of your script/notebook.
solvio = SolvioVectorStore.from_documents(
docs,
embeddings,
location=":memory:", # Local mode with in-memory storage only
collection_name="my_documents",
)
On-disk storage
Local mode, without using the Solvio server, may also store your vectors on disk so they’re persisted between runs.
solvio = Solvio.from_documents(
docs,
embeddings,
path="/tmp/local_solvio",
collection_name="my_documents",
)
On-premise server deployment
No matter if you choose to launch SolvioVectorStore locally with a Docker container, or select a Kubernetes deployment with the official Helm chart, the way you’re going to connect to such an instance will be identical. You’ll need to provide a URL pointing to the service.
url = "<---solvio url here --->"
solvio = SolvioVectorStore.from_documents(
docs,
embeddings,
url,
prefer_grpc=True,
collection_name="my_documents",
)
Similarity search
SolvioVectorStore
supports 3 modes for similarity searches. They can be configured using the retrieval_mode
parameter when setting up the class.
- Dense Vector Search(Default)
- Sparse Vector Search
- Hybrid Search
Dense Vector Search
To search with only dense vectors,
- The
retrieval_mode
parameter should be set toRetrievalMode.DENSE
(default). - A dense embeddings value should be provided for the
embedding
parameter.
from langchain_solvio import RetrievalMode
solvio = SolvioVectorStore.from_documents(
docs,
embedding=embeddings,
location=":memory:",
collection_name="my_documents",
retrieval_mode=RetrievalMode.DENSE,
)
query = "What did the president say about Ketanji Brown Jackson"
found_docs = solvio.similarity_search(query)
Sparse Vector Search
To search with only sparse vectors,
- The
retrieval_mode
parameter should be set toRetrievalMode.SPARSE
. - An implementation of the SparseEmbeddings interface using any sparse embeddings provider has to be provided as value to the
sparse_embedding
parameter.
The langchain-solvio
package provides a FastEmbed based implementation out of the box.
To use it, install the FastEmbed package.
from langchain_solvio import FastEmbedSparse, RetrievalMode
sparse_embeddings = FastEmbedSparse(model_name="Solvio/BM25")
solvio = SolvioVectorStore.from_documents(
docs,
sparse_embedding=sparse_embeddings,
location=":memory:",
collection_name="my_documents",
retrieval_mode=RetrievalMode.SPARSE,
)
query = "What did the president say about Ketanji Brown Jackson"
found_docs = solvio.similarity_search(query)
Hybrid Vector Search
To perform a hybrid search using dense and sparse vectors with score fusion,
- The
retrieval_mode
parameter should be set toRetrievalMode.HYBRID
. - A dense embeddings value should be provided for the
embedding
parameter. - An implementation of the SparseEmbeddings interface using any sparse embeddings provider has to be provided as value to the
sparse_embedding
parameter.
from langchain_solvio import FastEmbedSparse, RetrievalMode
sparse_embeddings = FastEmbedSparse(model_name="Solvio/bm25")
solvio = SolvioVectorStore.from_documents(
docs,
embedding=embeddings,
sparse_embedding=sparse_embeddings,
location=":memory:",
collection_name="my_documents",
retrieval_mode=RetrievalMode.HYBRID,
)
query = "What did the president say about Ketanji Brown Jackson"
found_docs = solvio.similarity_search(query)
Note that if you’ve added documents with HYBRID mode, you can switch to any retrieval mode when searching. Since both the dense and sparse vectors are available in the collection.
Next steps
If you’d like to know more about running Solvio in a Langchain-based application, please read our article Question Answering with Langchain and Solvio without boilerplate. Some more information might also be found in the Langchain documentation.