## SingleStoreDB

> [SingleStoreDB](https://singlestore.com/) is a high-performance distributed SQL database that supports deployment both in the [cloud](https://www.singlestore.com/cloud/) and on-premises. It provides vector storage, and vector functions including [dot\_product](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions/dot_product.html) and [euclidean\_distance](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions/euclidean_distance.html), thereby supporting AI applications that require text similarity matching.

This tutorial illustrates how to [work with vector data in SingleStoreDB](https://docs.singlestore.com/managed-service/en/developer-resources/functional-extensions/working-with-vector-data.html).

```
# Establishing a connection to the database is facilitated through the singlestoredb Python connector.# Please ensure that this connector is installed in your working environment.%pip install --upgrade --quiet  singlestoredb
```

```
import getpassimport os# We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores import SingleStoreDBfrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplitter
```

```
# Load text samplesloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()
```

There are several ways to establish a [connection](https://singlestoredb-python.labs.singlestore.com/generated/singlestoredb.connect.html) to the database. You can either set up environment variables or pass named parameters to the `SingleStoreDB constructor`. Alternatively, you may provide these parameters to the `from_documents` and `from_texts` methods.

```
# Setup connection url as environment variableos.environ["SINGLESTOREDB_URL"] = "root:pass@localhost:3306/db"# Load documents to the storedocsearch = SingleStoreDB.from_documents(    docs,    embeddings,    table_name="notebook",  # use table with a custom name)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = docsearch.similarity_search(query)  # Find documents that correspond to the queryprint(docs[0].page_content)
```

Enhance your search efficiency with SingleStore DB version 8.5 or above by leveraging [ANN vector indexes](https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing/). By setting `use_vector_index=True` during vector store object creation, you can activate this feature. Additionally, if your vectors differ in dimensionality from the default OpenAI embedding size of 1536, ensure to specify the `vector_size` parameter accordingly.

## Multi-modal Example: Leveraging CLIP and OpenClip Embeddings[​](#multi-modal-example-leveraging-clip-and-openclip-embeddings "Direct link to Multi-modal Example: Leveraging CLIP and OpenClip Embeddings")

In the realm of multi-modal data analysis, the integration of diverse information types like images and text has become increasingly crucial. One powerful tool facilitating such integration is [CLIP](https://openai.com/research/clip), a cutting-edge model capable of embedding both images and text into a shared semantic space. By doing so, CLIP enables the retrieval of relevant content across different modalities through similarity search.

To illustrate, let’s consider an application scenario where we aim to effectively analyze multi-modal data. In this example, we harness the capabilities of [OpenClip multimodal embeddings](https://python.langchain.com/docs/integrations/text_embedding/open_clip/), which leverage CLIP’s framework. With OpenClip, we can seamlessly embed textual descriptions alongside corresponding images, enabling comprehensive analysis and retrieval tasks. Whether it’s identifying visually similar images based on textual queries or finding relevant text passages associated with specific visual content, OpenClip empowers users to explore and extract insights from multi-modal data with remarkable efficiency and accuracy.

```
%pip install -U langchain openai singlestoredb langchain-experimental # (newest versions required for multi-modal)
```

```
import osfrom langchain_community.vectorstores import SingleStoreDBfrom langchain_experimental.open_clip import OpenCLIPEmbeddingsos.environ["SINGLESTOREDB_URL"] = "root:pass@localhost:3306/db"TEST_IMAGES_DIR = "../../modules/images"docsearch = SingleStoreDB(OpenCLIPEmbeddings())image_uris = sorted(    [        os.path.join(TEST_IMAGES_DIR, image_name)        for image_name in os.listdir(TEST_IMAGES_DIR)        if image_name.endswith(".jpg")    ])# Add imagesdocsearch.add_images(uris=image_uris)
```