Skip to content

Commit

Permalink
add documentation and vector quantization option
Browse files Browse the repository at this point in the history
  • Loading branch information
SidneyPhoon authored and maljazaery committed Oct 22, 2024
1 parent 3d4cbbb commit 1fb7ca1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
38 changes: 38 additions & 0 deletions docs/components/vectordbs/dbs/azure_ai_search.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[Azure AI Search](https://learn.microsoft.com/en-us/azure/search/search-what-is-azure-search/) (formerly known as "Azure Cognitive Search") provides secure information retrieval at scale over user-owned content in traditional and generative AI search applications.

### Usage

```python
import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx"

config = {
"vector_store": {
"provider": "azure_ai_search",
"config": {
"service_name": "ai-search-test",
"api_key": "*****",
"collection_name": "mem0",
"embedding_model_dims": 1536 ,
"use_compression": False
}
}
}

m = Memory.from_config(config)
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
```

### Config

Let's see the available parameters for the `qdrant` config:
service_name (str): Azure Cognitive Search service name.
| Parameter | Description | Default Value |
| --- | --- | --- |
| `service_name` | Azure AI Search service name | `None` |
| `api_key` | API key of the Azure AI Search service | `None` |
| `collection_name` | The name of the collection/index to store the vectors, it will be created automatically if not exist | `mem0` |
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
| `use_compression` | Use scalar quantization vector compression | False |
1 change: 1 addition & 0 deletions mem0/configs/vector_stores/azure_ai_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AzureAISearchConfig(BaseModel):
service_name: str = Field(None, description="Azure Cognitive Search service name")
api_key: str = Field(None, description="API key for the Azure Cognitive Search service")
embedding_model_dims: int = Field(None, description="Dimension of the embedding vector")
use_compression: bool = Field(False, description="Use compression or not")

@model_validator(mode="before")
@classmethod
Expand Down
27 changes: 24 additions & 3 deletions mem0/vector_stores/azure_ai_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
HnswAlgorithmConfiguration,
ScalarQuantizationCompression,
SearchField,
SearchFieldDataType,
SearchIndex,
Expand All @@ -36,7 +37,7 @@ class OutputData(BaseModel):
payload: Optional[dict]

class AzureAISearch(VectorStoreBase):
def __init__(self, service_name, collection_name, api_key, embedding_model_dims):
def __init__(self, service_name, collection_name, api_key, embedding_model_dims, use_compression):
"""Initialize the Azure Cognitive Search vector store.
Args:
Expand All @@ -48,6 +49,7 @@ def __init__(self, service_name, collection_name, api_key, embedding_model_dims)
self.index_name = collection_name
self.collection_name = collection_name
self.embedding_model_dims = embedding_model_dims
self.use_compression = use_compression
self.search_client = SearchClient(endpoint=f"https://{service_name}.search.windows.net",
index_name=self.index_name,
credential=AzureKeyCredential(api_key))
Expand All @@ -57,16 +59,35 @@ def __init__(self, service_name, collection_name, api_key, embedding_model_dims)

def create_col(self):
"""Create a new index in Azure Cognitive Search."""
vector_dimensions = self.embedding_model_dims # Set this to the number of dimensions in your vector
vector_dimensions = self.embedding_model_dims # Set this to the number of dimensions in your vector


if self.use_compression:
vector_type = "Collection(Edm.Half)"
else:
vector_type = "Collection(Edm.Single)"

fields = [
SimpleField(name="id", type=SearchFieldDataType.String, key=True),
SearchField(name="vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
SearchField(name="vector", type=vector_type,
searchable=True, vector_search_dimensions=vector_dimensions, vector_search_profile_name="my-vector-config"),
SimpleField(name="payload", type=SearchFieldDataType.String, searchable=True)
]


if self.use_compression:
compression_name = "myCompression"
compression_configurations = [
ScalarQuantizationCompression(compression_name=compression_name)
]
else:
compression_name = None
compression_configurations = []

vector_search = VectorSearch(
profiles=[VectorSearchProfile(name="my-vector-config", algorithm_configuration_name="my-algorithms-config")],
algorithms=[HnswAlgorithmConfiguration(name="my-algorithms-config")],
compressions=compression_configurations
)
index = SearchIndex(name=self.index_name, fields=fields, vector_search=vector_search)
self.index_client.create_or_update_index(index)
Expand Down

0 comments on commit 1fb7ca1

Please sign in to comment.