Skip to content

Commit 5d986e8

Browse files
authored
Merge pull request #37 from vcon-dev/milvus-storage
Milvus storage
2 parents 248d9fa + d5c66bb commit 5d986e8

File tree

7 files changed

+1468
-2
lines changed

7 files changed

+1468
-2
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,44 @@ vcon-server-conserver-1 | {"asctime": "2024-08-23 17:27:22,240", "levelname": "
268268
```
269269

270270
The [vCon admin program](https://github.com/vcon-dev/vcon-admin) is a nice tool for managing the conserver. 
271+
272+
## Storage Modules
273+
274+
### Milvus Vector Database Storage
275+
276+
The vcon-server includes support for storing vCons in Milvus, a vector database that enables semantic search across vCon content. This is particularly useful for finding conversations based on meaning rather than exact keyword matches.
277+
278+
To set up Milvus storage:
279+
280+
1. Install the required packages:
281+
```bash
282+
poetry add pymilvus>=2.3.0 openai>=1.54.3 python-dateutil
283+
```
284+
285+
2. Add Milvus storage configuration to your config.yml:
286+
```yaml
287+
storages:
288+
milvus:
289+
module: storage.milvus
290+
options:
291+
host: "localhost" # Milvus server host
292+
port: "19530" # Milvus server port
293+
collection_name: "vcons" # Name of collection in Milvus
294+
embedding_model: "text-embedding-3-small" # OpenAI embedding model
295+
embedding_dim: 1536 # Dimensions for the embedding model
296+
api_key: "your-openai-api-key" # Your OpenAI API key
297+
organization: "your-org-id" # Optional: Your OpenAI organization ID
298+
create_collection_if_missing: true # Auto-create collection if needed
299+
```
300+
301+
3. Include the Milvus storage in your processing chain:
302+
```yaml
303+
chains:
304+
main_chain:
305+
# ... other configuration ...
306+
storages:
307+
- milvus
308+
# ... other storages ...
309+
```
310+
311+
See the [Milvus Storage Module README](server/storage/milvus/README.md) for more details on configuration and usage.

example_config.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ storages:
5252
aws_access_key_id: some_key
5353
aws_secret_access_key: some_secret
5454
aws_bucket: some_bucket
55+
milvus:
56+
module: storage.milvus
57+
options:
58+
# Connection settings
59+
host: "localhost"
60+
port: "19530"
61+
collection_name: "vcons"
62+
63+
# Embedding settings
64+
embedding_model: "text-embedding-3-small"
65+
embedding_dim: 1536
66+
api_key: "your-openai-api-key"
67+
organization: ""
68+
69+
# Operation settings
70+
create_collection_if_missing: true
71+
skip_if_exists: true
72+
73+
# Vector index settings (Default: IVF_FLAT with L2 distance)
74+
index_type: "IVF_FLAT" # Options: IVF_FLAT, IVF_SQ8, IVF_PQ, HNSW, FLAT
75+
metric_type: "L2" # Options: L2, IP, COSINE
76+
nlist: 128 # For IVF indexes: number of clusters
77+
78+
# Advanced HNSW settings (used only if index_type is HNSW)
79+
# m: 16 # Number of edges per node
80+
# ef_construction: 200 # Size of dynamic candidate list during construction
81+
82+
# Advanced IVF_PQ settings (used only if index_type is IVF_PQ)
83+
# pq_m: 8 # Number of sub-quantizers
84+
# pq_nbits: 8 # Bit depth per quantizer
5585
chains:
5686
sample_chain:
5787
links:
@@ -65,6 +95,7 @@ chains:
6595
- mongo
6696
- postgres
6797
- s3
98+
- milvus
6899
egress_lists:
69100
- test_output
70101
enabled: 1

poetry.lock

Lines changed: 414 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pytest = "^8.3.4"
3737
anyio = "^4.8.0"
3838
faker = "^33.3.1"
3939
jq = "^1.8.0"
40+
pymilvus = ">=2.3.0"
41+
python-dateutil = "^2.8.2"
4042

4143

4244
[tool.poetry.group.dev.dependencies]

server/storage/milvus/README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Milvus Storage Module for vCon Server
2+
3+
This storage module enables storing vCons in a Milvus vector database for semantic search capabilities.
4+
5+
## Description
6+
7+
The Milvus storage module:
8+
- Extracts text content from vCons (transcripts, summaries, metadata, etc.)
9+
- Generates embeddings using OpenAI's embedding models
10+
- Stores the embeddings in Milvus along with related metadata
11+
- Enables semantic search across vCon content
12+
13+
## Requirements
14+
15+
- Milvus 2.0+ running and accessible
16+
- OpenAI API key for generating embeddings
17+
- Additional Python packages:
18+
```
19+
pymilvus>=2.3.0
20+
openai>=1.0.0
21+
```
22+
23+
## Configuration
24+
25+
Add the following to your config.yml file:
26+
27+
```yaml
28+
storages:
29+
milvus:
30+
module: storage.milvus
31+
options:
32+
# Connection settings
33+
host: "localhost" # Milvus server host
34+
port: "19530" # Milvus server port
35+
collection_name: "vcons" # Name of the collection in Milvus
36+
37+
# Embedding settings
38+
embedding_model: "text-embedding-3-small" # OpenAI embedding model
39+
embedding_dim: 1536 # Dimensions for the chosen model
40+
api_key: "your-openai-api-key" # Your OpenAI API key
41+
organization: "your-org-id" # Optional: Your OpenAI organization ID
42+
43+
# Operation settings
44+
create_collection_if_missing: true # Whether to create collection if it doesn't exist
45+
skip_if_exists: true # Skip storing vCons that already exist
46+
47+
# Vector index settings (optional, shown are defaults)
48+
index_type: "IVF_FLAT" # Vector index type
49+
metric_type: "L2" # Distance metric type
50+
nlist: 128 # Number of clusters for IVF indexes
51+
```
52+
53+
### Vector Index Types
54+
55+
The module supports different vector index types with appropriate parameters:
56+
57+
#### IVF_FLAT (Default)
58+
Good balance of search accuracy and speed. Uses more storage but gives exact results within each cluster.
59+
60+
```yaml
61+
index_type: "IVF_FLAT"
62+
metric_type: "L2" # Or "IP" for inner product, or "COSINE"
63+
nlist: 128 # Number of clusters, higher values = faster search but less accurate
64+
```
65+
66+
#### IVF_SQ8
67+
Similar to IVF_FLAT but with scalar quantization (8-bit) to reduce memory usage. Good for large datasets.
68+
69+
```yaml
70+
index_type: "IVF_SQ8"
71+
metric_type: "L2"
72+
nlist: 128
73+
```
74+
75+
#### IVF_PQ
76+
Product Quantization for maximum memory optimization. Sacrifices some accuracy for much smaller index size.
77+
78+
```yaml
79+
index_type: "IVF_PQ"
80+
metric_type: "L2"
81+
nlist: 128
82+
pq_m: 8 # Number of sub-quantizers
83+
pq_nbits: 8 # Bit depth per quantizer
84+
```
85+
86+
#### HNSW
87+
Hierarchical Navigable Small World graph index. Very fast for searching, especially with smaller datasets.
88+
89+
```yaml
90+
index_type: "HNSW"
91+
metric_type: "L2"
92+
m: 16 # Number of edges per node
93+
ef_construction: 200 # Size of the dynamic candidate list during construction
94+
```
95+
96+
#### FLAT
97+
The simplest index that compares to every vector. Most accurate but slowest for large datasets.
98+
99+
```yaml
100+
index_type: "FLAT"
101+
metric_type: "L2"
102+
```
103+
104+
### Distance Metrics
105+
106+
- `L2`: Euclidean distance (default). Good for most embeddings including OpenAI embeddings.
107+
- `IP`: Inner product. Use when vectors are normalized and you want to measure closeness.
108+
- `COSINE`: Cosine similarity. Good for measuring the angle between vectors regardless of magnitude.
109+
110+
## Searching vCons in Milvus
111+
112+
While not part of the storage module itself, you can search vCons in Milvus using the pymilvus client:
113+
114+
```python
115+
from pymilvus import connections, Collection
116+
117+
# Connect to Milvus
118+
connections.connect(host="localhost", port="19530")
119+
120+
# Load collection
121+
collection = Collection("vcons")
122+
collection.load()
123+
124+
# Get embedding for search query (using same OpenAI client as storage module)
125+
from openai import OpenAI
126+
client = OpenAI(api_key="your-api-key")
127+
response = client.embeddings.create(input="your search query", model="text-embedding-3-small")
128+
query_embedding = response.data[0].embedding
129+
130+
# Search parameters
131+
search_params = {
132+
"metric_type": "L2",
133+
"params": {"nprobe": 10}
134+
}
135+
136+
# Search vCons
137+
results = collection.search(
138+
data=[query_embedding],
139+
anns_field="embedding",
140+
param=search_params,
141+
limit=5,
142+
output_fields=["vcon_uuid", "party_id", "text", "metadata_title"]
143+
)
144+
145+
# Process results
146+
for hits in results:
147+
for hit in hits:
148+
print(f"vCon UUID: {hit.entity.get('vcon_uuid')}")
149+
print(f"Score: {hit.score}")
150+
print(f"Title: {hit.entity.get('metadata_title')}")
151+
```
152+
153+
## Notes
154+
155+
- Vector dimensions must match the embedding model used (1536 for text-embedding-3-small)
156+
- For larger vCon datasets, consider configuring Milvus index parameters for better performance
157+
- Milvus collections created by this module include a L2 vector index for similarity search

0 commit comments

Comments
 (0)