Skip to main content

Python Sdk for Milvus

Project description

Milvus Python SDK -- pymilvus

version license

If you want to contribute to this repo, please read our contribution guidelines.

You can find api doc in Reference Doc

Using Milvus python sdk for Milvus Download

New features

  • Add new metric type HAMMING, JACCARD, TANIMOTO for binary vectors. examples about binary vectors in examples/example_binary.py

Pymilvus only supports python >= 3.5, is fully tested under 3.5, 3.6, 3.7, 3.8.

Pymilvus can be downloaded via pip or pip3 for python3

$ pip install pymilvus

Different versions of Milvus and recommended pymilvus version supported accordingly

Milvus version Recommended pymilvus version
0.3.0 0.1.13
0.3.1 0.1.25
0.4.0 0.2.2
0.5.0 0.2.3
0.5.1 0.2.3
0.5.2 0.2.3
0.5.3 0.2.5
0.6.0 0.2.6

You can download a specific version by:

$ pip install pymilvus==0.2.7

If you want to upgrade pymilvus to newest version

$ pip install --upgrade pymilvus

Import

from milvus import Milvus, IndexType, MetricType, Status

Getting started

Initial a Milvus instance and connect to the sever

Create table

>>> milvus = Milvus()

>>> milvus.connect(host='localhost', port='19530')
Status(code=0, message='Successfully connected! localhost:19530')

Once successfully connected, you can get the version of server

>>> milvus.server_version()
(Status(code=0, message='Success'), '0.6.0')  # this is example version, the real version may vary

Add a new table

First set param

>>> dim = 32  # Dimension of the vector
>>> param = {'table_name':'test01', 'dimension':dim, 'index_file_size':1024, 'metric_type':MetricType.L2}

Then create table

>>> milvus.create_table(param)
Status(code=0, message='Create table successfully!')

Describe the table we just created

>>> milvus.describe_table('test01')
(Status(code=0, message='Describe table successfully!'), TableSchema(table_name='test01', dimension=32, index_file_size=1024, metric_type=<MetricType: L2>))

Insert vectors

Add vectors into table test01

First create 20 vectors of 256-dimension.

  • Note that random and pprint we used here is for creating fake vectors data and pretty print, you may not need them in your project
>>> import random
>>> from pprint import pprint

# Initialize 20 vectors of 256-dimension
>>> vectors = [[random.random() for _ in range(dim)] for _ in range(20)]

Then add vectors into table test01

>>> status, ids = milvus.insert(table_name='test01', records=vectors)
>>> print(status)
Status(code=0, message='Add vectors successfully!')
>>> pprint(ids) # List of ids returned
[1571123848227800000,
 1571123848227800001,
    ...........
 1571123848227800018,
 1571123848227800019]

You can also specify vectors id

>>> vector_ids = [i for i in range(20)]
>>> status, ids = milvus.insert(table_name='test01', records=vectors, ids=vector_ids)
>>> pprint(ids)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Get vectors num

>>> milvus.count_table('test01')
(Status(code=0, message='Success!'), 20)

Load vectors into memory

>>> milvus.preload_table('test01')
Status(code=0, message='')

Create index

Create index

>>> index_param = {'index_type': IndexType.FLAT, 'nlist': 128}
>>> milvus.create_index('test01', index_param)
Status(code=0, message='Build index successfully!')

Then show index information

>>> milvus.describe_index('test01')
(Status(code=0, message='Successfully'), IndexParam(_table_name='test01', _index_type=<IndexType: FLAT>, _nlist=128))

Search vectors

# create 5 vectors of 32-dimension
>>> q_records = [[random.random() for _ in range(dim)] for _ in range(5)]

Then get results

>>> status, results = milvus.search(table_name='test01', query_records=q_records, top_k=1, nprobe=8)
>>> print(status)
Status(code=0, message='Search vectors successfully!')
>>> pprint(results) # Searched top_k vectors
[
[(id:15, distance:2.855304718017578),
 (id:16, distance:3.040700674057007)],
[(id:11, distance:3.673950433731079),
 (id:15, distance:4.183730602264404)],
      ........
[(id:6, distance:4.065953254699707),
 (id:1, distance:4.149323463439941)]
]

Partition operations

Create table named demo01

>>> param = {'table_name':'demo01', 'dimension':dim, 'index_file_size':1024, 'metric_type':MetricType.L2}
>>> milvus.create_table(param)

Create a new partition named partition01 under table demo01, and specify tag tag01

>>> milvus.create_partition('demo01', 'partition01', 'tag01')
Status(code=0, message='OK')

Specify partition vectors insert into

>>> status = milvus.insert('demo01', vectors, partition_tag="tag01")
>>> status
(Status(code=0, message='Add vectors successfully!')

Show partitions

milvus.show_partitions(table_name='demo01')

Search vectors in a designated partition

>>> milvus.search(table_name='test01', query_records=q_records, top_k=1, nprobe=8, partition_tags=['tag01'])

When you not specify partition_tags, milvus will search in whole table.

Drop operations

Drop index

>>> milvus.drop_index('test01')
Status(code=0, message='')

Delete the table we just created

>>> milvus.drop_table(table_name='test01')
Status(code=0, message='Delete table successfully!')

Disconnect with the server

>>> milvus.disconnect()
Status(code=0, message='Disconnect successfully')

Example python

There are some small examples in examples/, you can find more guide there.

If you encounter any problems or bugs, please open new issues

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymilvus-0.2.7.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymilvus-0.2.7-py3-none-any.whl (39.9 kB view details)

Uploaded Python 3

File details

Details for the file pymilvus-0.2.7.tar.gz.

File metadata

  • Download URL: pymilvus-0.2.7.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.9

File hashes

Hashes for pymilvus-0.2.7.tar.gz
Algorithm Hash digest
SHA256 ce89924472af2b9d1370e0ade2759b5c2c382d20ea3eb696387c3ac406e56332
MD5 2eaa6e15032f7eb06d24cf4bcc2ca037
BLAKE2b-256 8eb2103ec5d43de19b64ec68533a7f7964cf96cc10328a203114e64539afc893

See more details on using hashes here.

File details

Details for the file pymilvus-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: pymilvus-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 39.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.9

File hashes

Hashes for pymilvus-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7ac96cdf471331ca5d96face407eedccd655a1a16c0370121e01a493b3da0200
MD5 8dc7e3ab25c9207603284bcab9401e9b
BLAKE2b-256 ec4b44e47b5c9f87d3e9921c76489ed911f95a9111b1eb1dc92a40d29571cde7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page