Notebooks
M
Milvus
Milvus Connect

Milvus Connect

image-searchvector-databasesemantic-searchmilvusembeddingsunstructured-dataquestion-answeringLLMmilvus-bootcampdeep-learningimage-recognitionimage-classificationaudio-searchPythonbootcampragNLP

How to connect to Milvus from a notebook

There are several different ways to start up a Milvus server.

  1. Milvus Lite is a local Python server that can run in Jupyter notebooks or Google Colab, requires pymilvus>=2.4.3.
    ⛔️ Only meant for demos and local testing.
  2. Zilliz cloud free tier
  3. Milvus standalone docker requires local docker installed and running.
  4. LangChain - all 3rd party adapters use Milvus Lite.
  5. LlamaIndex - all 3rd party adapters use Milvus Lite.
  6. Milvus kubernetes cluster requires a K8s cluster up and running.

💡 For production workloads, it is recommended to use Milvus local docker, kubernetes clusters, or fully-managed Milvus on Zilliz Cloud.

I'll demonstrate how to connect using the Python SDK. For more details, see this Python example.

1. Milvus Lite

Milvus Lite is a light Python server that can run locally. It's ideal for getting started with Milvus, running on a laptop, in a Jupyter notebook, or on Colab.

⛔️ Please note Milvus Lite is only meant for demos, not for production workloads.

[1]
pymilvus:2.4.3
[2]
[3]
Successfully created collection: `MilvusDocs`
[4]
Successfully dropped collection: `MilvusDocs`

2. Zilliz free tier

This section uses Zilliz, free tier. If you have not already, sign up for a free trial.

If you already have a Zilliz account and want to use free tier, just be sure to select "Starter" option when you create your cluster. ❤️‍🔥 In other words, everybody gets free tier!!

  • One free tier cluster per account.
  • Per free tier cluster, up to two collections at a time. (Think of a collection like a database table. Each collection has an index, schema, and consistency-level).
  • Each free tier collection can support up to 1 Million vectors (Think of this like rows in a database table).

If you have larger data, we recommend our Pay-as-you-go Serverless or Enterprise plan. Free tier and Pay-as-you-go are Zilliz-managed AWS, Google, or Azure services. BYOC is possible in the Enterprise plan.

👩 Set up instructions for Zilliz

  1. From cloud.zilliz.com, click "+ Create Cluster"

  2. Select Starter option for the cluster and click "Next: Create Collection"

  3. Name your collection with a Collection Name and click "Create Collection and Cluster".

  4. From the Clusters page,

    • copy the cluster uri and save somewhere locally.
    • copy your cluster API KEY. Keep this private!
  5. Add the API KEY to your environment variables. See this article for instructions how in either Windows or Mac/Linux environment.

  6. In Jupyter, you'll also need .env file (in same dir as notebooks) containing lines like this:

    • ZILLIZ_API_KEY=value
  7. In your code, connect to your Zilliz cluster, see code example below.

[5]
Type of server: Zilliz Cloud Vector Database(Compatible with Milvus 2.4)
[6]
Successfully created collection: `movies`
[7]
Successfully disconnected from the server.

3. Milvus standalone Docker

This section uses Milvus standalone on Docker.

⛔️ Make sure you pip install the correct version of pymilvus and server yml file. Versions (major and minor) should all match.

  1. Install Docker
  2. Start your Docker Desktop
  3. Download the latest docker-compose.yml (or run the wget command, replacing version to what you are using)

wget https://github.com/milvus-io/milvus/releases/download/v2.4.0-rc.1/milvus-standalone-docker-compose.yml -O docker-compose.yml

  1. From your terminal:
    • cd into directory where you saved the .yml file (usualy same dir as this notebook)
    • docker compose up -d
    • verify (either in terminal or on Docker Desktop) the containers are running
  2. From your code (see notebook code below):
    • Import milvus
    • Connect to the local milvus server
[8]
[9]
Pymilvus: 2.4.3
[10]
v2.4.1
[11]
Successfully dropped collection: `movies`
Created collection: movies
[12]
WARN[0000] /Users/christy/Documents/bootcamp_scratch/bootcamp/docker-compose.yml: `version` is obsolete 
Successfully disconnected from the server.

LangChain

All 3rd party adapters use Milvus Lite.

LangChain APIs hide a lot of the steps to convert raw unstructured data into vectors and store the vectors in Milvus.

LangChain default values:

  • collection_name: LangChainCollection
  • schema: ['pk', 'source', 'text', 'vector']
  • auto_id: True
  • {'index_type': 'HNSW', 'metric_type': 'L2', 'params': {'M': 8, 'efConstruction': 64}}
  • consistency_level: 'Session'
  • overwrite: False
[13]
[14]
loaded 22 documents
[16]
length doc: 11016
('Why MilvusDocsTutorialsToolsBlogCommunityStars0Try Managed Milvus '
 'FREESearchHomev2.4.xAbout MilvusGe')
[17]
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
  warnings.warn(
EMBEDDING_DIM: 1024
Created Milvus collection from 427 docs in 33.14 seconds
[18]
collection_name: LangChainCollection
schema: ['source', 'text', 'pk', 'vector']
auto_id: True
{'index_type': 'AUTOINDEX', 'metric_type': 'COSINE', 'params': {}}
'consistency: Eventually'
'drop_old: True'
[19]

LlamaIndex

All 3rd party adapters use Milvus Lite.

LlamaIndex APIs hide a lot of the steps to convert raw unstructured data into vectors and store the vectors in Milvus.

LlamaIndex default values:

  • collection_name: llamacollection
  • schema: ['doc_id', 'embedding']
  • auto_id: True
  • {'index_type': 'None', 'metric_type': 'IP',
  • consistency_level: 'Strong'
  • overwrite: False
[20]
[33]
loaded 22 documents
[34]
length doc: 663373
('<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta '
 'http-equiv="x-ua-compatible" conte')
[35]
/var/folders/vn/4v5_m9mx69x3h7jcl1chb7nr0000gn/T/ipykernel_4999/3447014088.py:12: DeprecationWarning: Call to deprecated class method from_defaults. (ServiceContext is deprecated, please use `llama_index.settings.Settings` instead.) -- Deprecated since version 0.10.0.
  service_context = ServiceContext.from_defaults(
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
  warnings.warn(
Embedding model:
{'cache_folder': None,
 'class_name': 'HuggingFaceEmbedding',
 'embed_batch_size': 10,
 'max_length': 512,
 'model_name': 'BAAI/bge-large-en-v1.5',
 'normalize': True,
 'num_workers': None,
 'query_instruction': None,
 'text_instruction': None}

Start chunking, embedding, inserting...
Created LlamaIndex collection from 1 docs in 101.56 seconds
[36]
stores_text: True
is_embedding_query: True
stores_node: True
uri: ./milvus_llamaindex.db
token: 
collection_name: llamacollection
dim: 1024
embedding_field: embedding
doc_id_field: doc_id
similarity_metric: IP
consistency_level: Eventually
overwrite: False
text_key: None
output_fields: []
index_config: {}
[37]
[38]
The watermark extension is already loaded. To reload it, use:
  %reload_ext watermark
Author: Christy Bergman

Python implementation: CPython
Python version       : 3.11.8
IPython version      : 8.22.2

pymilvus    : 2.4.3
llama_index : 0.10.44
langchain   : 0.2.2
unstructured: 0.14.4

conda environment: py311-unum

[39]
llama-index                             0.10.44
llama-index-agent-openai                0.2.7
llama-index-cli                         0.1.12
llama-index-core                        0.10.44
llama-index-embeddings-huggingface      0.2.1
llama-index-embeddings-openai           0.1.10
llama-index-indices-managed-llama-cloud 0.1.6
llama-index-legacy                      0.9.48
llama-index-llms-ollama                 0.1.5
llama-index-llms-openai                 0.1.22
llama-index-multi-modal-llms-openai     0.1.6
llama-index-program-openai              0.1.6
llama-index-question-gen-openai         0.1.3
llama-index-readers-file                0.1.23
llama-index-readers-llama-parse         0.1.4
llama-index-vector-stores-milvus        0.1.17