## Vald

> [Vald](https://github.com/vdaas/vald) is a highly scalable distributed fast approximate nearest neighbor (ANN) dense vector search engine.

This notebook shows how to use functionality related to the `Vald` database.

To run this notebook you need a running Vald cluster. Check [Get Started](https://github.com/vdaas/vald#get-started) for more information.

See the [installation instructions](https://github.com/vdaas/vald-client-python#install).

```
%pip install --upgrade --quiet  vald-client-python
```

## Basic Example[​](#basic-example "Direct link to Basic Example")

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import Valdfrom langchain_text_splitters import CharacterTextSplitterraw_documents = TextLoader("state_of_the_union.txt").load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)embeddings = HuggingFaceEmbeddings()db = Vald.from_documents(documents, embeddings, host="localhost", port=8080)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = db.similarity_search(query)docs[0].page_content
```

### Similarity search by vector[​](#similarity-search-by-vector "Direct link to Similarity search by vector")

```
embedding_vector = embeddings.embed_query(query)docs = db.similarity_search_by_vector(embedding_vector)docs[0].page_content
```

### Similarity search with score[​](#similarity-search-with-score "Direct link to Similarity search with score")

```
docs_and_scores = db.similarity_search_with_score(query)docs_and_scores[0]
```

## Maximal Marginal Relevance Search (MMR)[​](#maximal-marginal-relevance-search-mmr "Direct link to Maximal Marginal Relevance Search (MMR)")

In addition to using similarity search in the retriever object, you can also use `mmr` as retriever.

```
retriever = db.as_retriever(search_type="mmr")retriever.get_relevant_documents(query)
```

Or use `max_marginal_relevance_search` directly:

```
db.max_marginal_relevance_search(query, k=2, fetch_k=10)
```

## Example of using secure connection[​](#example-of-using-secure-connection "Direct link to Example of using secure connection")

In order to run this notebook, it is necessary to run a Vald cluster with secure connection.

Here is an example of a Vald cluster with the following configuration using [Athenz](https://github.com/AthenZ/athenz) authentication.

ingress(TLS) -\> [authorization-proxy](https://github.com/AthenZ/authorization-proxy)(Check athenz-role-auth in grpc metadata) -\> vald-lb-gateway

```
import grpcwith open("test_root_cacert.crt", "rb") as root:    credentials = grpc.ssl_channel_credentials(root_certificates=root.read())# Refresh is required for server usewith open(".ztoken", "rb") as ztoken:    token = ztoken.read().strip()metadata = [(b"athenz-role-auth", token)]
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import Valdfrom langchain_text_splitters import CharacterTextSplitterraw_documents = TextLoader("state_of_the_union.txt").load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)embeddings = HuggingFaceEmbeddings()db = Vald.from_documents(    documents,    embeddings,    host="localhost",    port=443,    grpc_use_secure=True,    grpc_credentials=credentials,    grpc_metadata=metadata,)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = db.similarity_search(query, grpc_metadata=metadata)docs[0].page_content
```

### Similarity search by vector[​](#similarity-search-by-vector-1 "Direct link to Similarity search by vector")

```
embedding_vector = embeddings.embed_query(query)docs = db.similarity_search_by_vector(embedding_vector, grpc_metadata=metadata)docs[0].page_content
```

### Similarity search with score[​](#similarity-search-with-score-1 "Direct link to Similarity search with score")

```
docs_and_scores = db.similarity_search_with_score(query, grpc_metadata=metadata)docs_and_scores[0]
```

### Maximal Marginal Relevance Search (MMR)[​](#maximal-marginal-relevance-search-mmr-1 "Direct link to Maximal Marginal Relevance Search (MMR)")

```
retriever = db.as_retriever(    search_kwargs={"search_type": "mmr", "grpc_metadata": metadata})retriever.get_relevant_documents(query, grpc_metadata=metadata)
```

Or:

```
db.max_marginal_relevance_search(query, k=2, fetch_k=10, grpc_metadata=metadata)
```

* * *

#### Help us out by providing feedback on this documentation page: