SingleStoreVectorStore
SingleStore is a robust, high-performance distributed SQL database solution designed to excel in both cloud and on-premises environments. Boasting a versatile feature set, it offers seamless deployment options while delivering unparalleled performance.
A standout feature of SingleStore is its advanced support for vector storage and operations, making it an ideal choice for applications requiring intricate AI capabilities such as text similarity matching. With built-in vector functions like dot_product and euclidean_distance, SingleStore empowers developers to implement sophisticated algorithms efficiently.
For developers keen on leveraging vector data within SingleStore, a comprehensive tutorial is available, guiding them through the intricacies of working with vector data. This tutorial delves into the Vector Store within SingleStoreDB, showcasing its ability to facilitate searches based on vector similarity. Leveraging vector indexes, queries can be executed with remarkable speed, enabling swift retrieval of relevant data.
Moreover, SingleStore's Vector Store seamlessly integrates with full-text indexing based on Lucene, enabling powerful text similarity searches. Users can filter search results based on selected fields of document metadata objects, enhancing query precision.
What sets SingleStore apart is its ability to combine vector and full-text searches in various ways, offering flexibility and versatility. Whether prefiltering by text or vector similarity and selecting the most relevant data, or employing a weighted sum approach to compute a final similarity score, developers have multiple options at their disposal.
In essence, SingleStore provides a comprehensive solution for managing and querying vector data, offering unparalleled performance and flexibility for AI-driven applications.
Class | Package | JS support |
---|---|---|
SingleStoreVectorStore | langchain_singlestore | ✅ |
For the langchain-community version SingleStoreDB
(deprecated), see
the v0.2 documentation.
Setup
To access SingleStore vector stores you'll need to install the langchain-singlestore
integration package.
%pip install -qU "langchain-singlestore"
Initialization
To initialize SingleStoreVectorStore
, you need an Embeddings
object and connection parameters for the SingleStore database.
Required Parameters:
- embedding (
Embeddings
): A text embedding model.
Optional Parameters:
-
distance_strategy (
DistanceStrategy
): Strategy for calculating vector distances. Defaults toDOT_PRODUCT
. Options:DOT_PRODUCT
: Computes the scalar product of two vectors.EUCLIDEAN_DISTANCE
: Computes the Euclidean distance between two vectors.
-
table_name (
str
): Name of the table. Defaults toembeddings
. -
content_field (
str
): Field for storing content. Defaults tocontent
. -
metadata_field (
str
): Field for storing metadata. Defaults tometadata
. -
vector_field (
str
): Field for storing vectors. Defaults tovector
. -
id_field (
str
): Field for storing IDs. Defaults toid
. -
use_vector_index (
bool
): Enables vector indexing (requires SingleStore 8.5+). Defaults toFalse
. -
vector_index_name (
str
): Name of the vector index. Ignored ifuse_vector_index
isFalse
. -
vector_index_options (
dict
): Options for the vector index. Ignored ifuse_vector_index
isFalse
. -
vector_size (
int
): Size of the vector. Required ifuse_vector_index
isTrue
. -
use_full_text_search (
bool
): Enables full-text indexing on content. Defaults toFalse
.
Connection Pool Parameters:
- pool_size (
int
): Number of active connections in the pool. Defaults to5
. - max_overflow (
int
): Maximum connections beyondpool_size
. Defaults to10
. - timeout (
float
): Connection timeout in seconds. Defaults to30
.
Database Connection Parameters:
- host (
str
): Hostname, IP, or URL for the database. - user (
str
): Database username. - password (
str
): Database password. - port (
int
): Database port. Defaults to3306
. - database (
str
): Database name.
Additional Options:
- pure_python (
bool
): Enables pure Python mode. - local_infile (
bool
): Allows local file uploads. - charset (
str
): Character set for string values. - ssl_key, ssl_cert, ssl_ca (
str
): Paths to SSL files. - ssl_disabled (
bool
): Disables SSL. - ssl_verify_cert (
bool
): Verifies server's certificate. - ssl_verify_identity (
bool
): Verifies server's identity. - autocommit (
bool
): Enables autocommits. - results_type (
str
): Structure of query results (e.g.,tuples
,dicts
).
import os
from langchain_singlestore.vectorstores import SingleStoreVectorStore
os.environ["SINGLESTOREDB_URL"] = "root:pass@localhost:3306/db"
vector_store = SingleStoreVectorStore(embeddings=embeddings)
Manage vector store
The SingleStoreVectorStore
assumes that a Document's ID is an integer. Below are examples of how to manage the vector store.
Add items to vector store
You can add documents to the vector store as follows:
%pip install -qU langchain-core
from langchain_core.documents import Document
docs = [
Document(
page_content="""In the parched desert, a sudden rainstorm brought relief,
as the droplets danced upon the thirsty earth, rejuvenating the landscape
with the sweet scent of petrichor.""",
metadata={"category": "rain"},
),
Document(
page_content="""Amidst the bustling cityscape, the rain fell relentlessly,
creating a symphony of pitter-patter on the pavement, while umbrellas
bloomed like colorful flowers in a sea of gray.""",
metadata={"category": "rain"},
),
Document(
page_content="""High in the mountains, the rain transformed into a delicate
mist, enveloping the peaks in a mystical veil, where each droplet seemed to
whisper secrets to the ancient rocks below.""",
metadata={"category": "rain"},
),
Document(
page_content="""Blanketing the countryside in a soft, pristine layer, the
snowfall painted a serene tableau, muffling the world in a tranquil hush
as delicate flakes settled upon the branches of trees like nature's own
lacework.""",
metadata={"category": "snow"},
),
Document(
page_content="""In the urban landscape, snow descended, transforming
bustling streets into a winter wonderland, where the laughter of
children echoed amidst the flurry of snowballs and the twinkle of
holiday lights.""",
metadata={"category": "snow"},
),
Document(
page_content="""Atop the rugged peaks, snow fell with an unyielding
intensity, sculpting the landscape into a pristine alpine paradise,
where the frozen crystals shimmered under the moonlight, casting a
spell of enchantment over the wilderness below.""",
metadata={"category": "snow"},
),
]
vector_store.add_documents(docs)
Update items in vector store
To update an existing document in the vector store, use the following code:
updated_document = Document(
page_content="qux", metadata={"source": "https://another-example.com"}
)
vector_store.update_documents(document_id="1", document=updated_document)
Delete items from vector store
To delete documents from the vector store, use the following code:
vector_store.delete(ids=["3"])
Query vector store
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.