Gathering detailed insights and metrics for milvus2-sdk-esm
Gathering detailed insights and metrics for milvus2-sdk-esm
Gathering detailed insights and metrics for milvus2-sdk-esm
Gathering detailed insights and metrics for milvus2-sdk-esm
npm install milvus2-sdk-esm
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
The official Milvus client for Node.js.
The following table shows the recommended @zilliz/milvus2-sdk-node
versions for different Milvus versions:
Milvus version | Node sdk version | Installation |
---|---|---|
v2.5.0+ | latest | yarn add @zilliz/milvus2-sdk-node@latest |
v2.4.0+ | v2.4.9 | yarn add @zilliz/milvus2-sdk-node@2.4.9 |
v2.3.0+ | v2.3.5 | yarn add @zilliz/milvus2-sdk-node@2.3.5 |
v2.2.0+ | v2.3.5 | yarn add @zilliz/milvus2-sdk-node@2.3.5 |
Repo: zilliz-cloud-typescript-example
Name | Demo | Model |
---|---|---|
semantic-search-example | https://zilliz-semantic-search-example.vercel.app | all-MiniLM-L6-v2 |
semantic-image-search | clip-vit-base-patch16 | |
semantic-image-search-client | https://zilliz-semantic-image-search-client.vercel.app | clip-vit-base-patch16 |
You can use npm (Node package manager) or Yarn to install the @zilliz/milvus2-sdk-node
dependency in your project:
1npm install @zilliz/milvus2-sdk-node 2# or ... 3yarn add @zilliz/milvus2-sdk-node
Please refer to this doc.
loadCollectionSync
-> loadCollection
loadCollection
-> loadCollectionAsync
loadCollectionSync
= loadCollectionSync
So now you can just call loadCollection
other than loadCollectionSync
to load your collection like other language SDK.
1new MilvusClient({ 2 address: 'localhost:19530', 3 tls: { 4 rootCert: readFileSync(`test/cert/ca.pem`), 5 privateKey: readFileSync(`test/cert/client.key`), 6 certChain: readFileSync(`test/cert/client.pem`), 7 serverName: 'localhost', 8 }, 9});
Query iterator is supported, now you can use queryIterator to pass the 16384 limit of milvus.
1const batchSize = 5000; 2const total = 30000; 3const iterator = await milvusClient.queryIterator({ 4 collection_name: COLLECTION, 5 batchSize: batchSize, // how much data to fetch one time 6 expr: 'id > 0', // optional, 7 output_fields: ['id'], 8 limit: total, // optional, how much data do you want to fetch, if not set, fetch all the data, be careful if you have large data set 9}); 10 11const results: any = []; 12let page = 0; 13for await (const value of iterator) { 14 results.push(...value); 15 page += 1; 16} 17console.log(reults.length); // 30000
Machine learning and neural networks often use half-precision data types, such as Float16 and BFloat16, Milvus 2.4 supports inserting vectors in the BF16 and FP16 formats as bytes.
However, these data types are not natively available in the Node.js environment, To enable users to utilize these formats, the Node SDK provides support for transformers during insert, query, and search operations.
There are four default transformers for performing a float32 to bytes transformation for BF16 and Float16 types: f32ArrayToF16Bytes, f16BytesToF32Array, f32ArrayToBf16Bytes, and bf16BytesToF32Array. If you wish to use your own transformers for Float16 and BFloat16, you can specify them.
1import { 2 f32ArrayToF16Bytes, 3 f16BytesToF32Array, 4 f32ArrayToBf16Bytes, 5 bf16BytesToF32Array, 6} from '@zilliz/milvus2-sdk-node'; 7 8//Insert float32 array for the float16 field. Node SDK will transform it to bytes using `f32ArrayToF16Bytes`. You can use your own transformer. 9const insert = await milvusClient.insert({ 10 collection_name: COLLECTION_NAME, 11 data: data, 12 // transformers: { 13 // [DataType.BFloat16Vector]: f32ArrayToF16Bytes, // use your own transformer 14 // }, 15}); 16// query: output float32 array other than bytes, 17const query = await milvusClient.query({ 18 collection_name: COLLECTION_NAME, 19 filter: 'id > 0', 20 output_fields: ['vector', 'id'], 21 // transformers: { 22 // [DataType.BFloat16Vector]: bf16BytesToF32Array, // use your own transformer 23 // }, 24}); 25// search: use bytes to search, output float32 array 26const search = await milvusClient.search({ 27 vector: data[0].vector, 28 collection_name: COLLECTION_NAME, 29 output_fields: ['id', 'vector'], 30 limit: 5, 31 // transformers: { 32 // [DataType.BFloat16Vector]: bf16BytesToF32Array, // use your own transformer 33 // }, 34});
Sparse vectors in the Node SDK support four formats: dict
, coo
, csr
, and array
, However, query and search operations currently only output in the dict format.
1// dict 2const sparseObject = { 3 3: 1.5, 4 6: 2.0, 5 9: -3.5, 6}; 7// coo 8const sparseCOO = [ 9 { index: 2, value: 5 }, 10 { index: 5, value: 3 }, 11 { index: 8, value: 7 }, 12]; 13// csr 14const sparseCSR = { 15 indices: [2, 5, 8], 16 values: [5, 3, 7], 17}; 18// array 19const sparseArray = [undefined, 0.0, 0.5, 0.3, undefined, 0.2];
Starting from Milvus 2.4, it supports Multi-Vector Search, you can continue to utilize the search API with similar parameters to perform multi-vector searches, and the format of the results remains unchanged.
1import { RRFRanker, WeightedRanker } from '@zilliz/milvus2-sdk-node'; 2// single-vector search on a collection with multiple vector fields 3const search = await milvusClient.search({ 4 collection_name: collection_name, 5 data: [1, 2, 3, 4, 5, 6, 7, 8], 6 anns_field: 'vector', // required if you have multiple vector fields in the collection 7 params: { nprobe: 2 }, 8 filter: 'id > 100', 9 limit: 5, 10}); 11 12// multi-vector search on a collection with multiple vector fields 13const search = await milvusClient.search({ 14 collection_name: collection_name, 15 data: [ 16 { 17 data: [1, 2, 3, 4, 5, 6, 7, 8], 18 anns_field: 'vector', 19 params: { nprobe: 2 }, 20 }, 21 { 22 data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 23 anns_field: 'vector1', 24 }, 25 ], 26 limit: 5, 27 rerank: RRFRanker(), 28 filter: 'id > 100', 29});
Starting from v2.4.0, we introduced a TypeScript client to provide better support for the Milvus RESTful API V2, take a look at our test file.
1import { HttpClient } from '@zilliz/milvus2-sdk-node'; 2const client = new HttpClient(config); 3await client.createCollection(params); 4await client.describeCollection(params); 5await client.listCollections(params); 6await client.insert(params); 7await client.upsert(params); 8await client.query(params); 9await client.search(params);
This table organizes the examples by technology, providing a brief description and the directory where each example can be found.
Technology | Example | Directory |
---|---|---|
Next.js | Next.js app example | examples/nextjs |
Node.js | Basic Node.js examples for Milvus | examples/milvus |
Langchain.js | Basic Langchain.js example | examples/langchain |
This guide will show you how to set up a simple application using Node.js and Milvus. Its scope is only how to set up the node.js client and perform the simple CRUD operations. For more in-depth coverage, see the Milvus official website.
1# Start Milvus with script 2wget https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh 3bash standalone_embed.sh start
Create a new app.js file and add the following code to try out some basic vector operations using the Milvus node.js client. More details on the API reference.
1import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node'; 2 3const address = 'your-milvus-ip-with-port'; 4const username = 'your-milvus-username'; // optional username 5const password = 'your-milvus-password'; // optional password 6 7// connect to milvus 8const client = new MilvusClient({ address, username, password }); 9// wait until connecting finished 10await client.connectPromise;
In Milvus, the concept of the collection is like the table in traditional RDBMS, eg: mysql or postgres. Before creating a collection, you need to define a schema, then just call the createCollection
method.
A schema defines the fields of a collection, such as the names and data types of the fields that make up the vectors. More details of how to define schema and advanced usage can be found in API reference.
1// define schema 2const collection_name = `hello_milvus`; 3const dim = 128; 4const schema = [ 5 { 6 name: 'age', 7 description: 'ID field', 8 data_type: DataType.Int64, 9 is_primary_key: true, 10 autoID: true, 11 }, 12 { 13 name: 'vector', 14 description: 'Vector field', 15 data_type: DataType.FloatVector, 16 dim: 8, 17 }, 18 { name: 'height', description: 'int64 field', data_type: DataType.Int64 }, 19 { 20 name: 'name', 21 description: 'VarChar field', 22 data_type: DataType.VarChar, 23 max_length: 128, 24 }, 25],
1await client.createCollection({ 2 collection_name, 3 fields: schema, 4});
The data format utilized by the Milvus Node SDK comprises an array of objects. In each object, the key should correspond to the field name
defined in the schema. The value type for the key should match the data_type
specified in the field of the schema.
1const fields_data = [ 2 { 3 name: 'zlnmh', 4 vector: [ 5 0.11878310581111173, 0.9694947902934701, 0.16443679307243175, 6 0.5484226189097237, 0.9839246709011924, 0.5178387104937776, 7 0.8716926129208069, 0.5616972243831446, 8 ], 9 height: 20405, 10 }, 11 { 12 name: '5lr9y', 13 vector: [ 14 0.9992090731236536, 0.8248790611809487, 0.8660083940881405, 15 0.09946359318481224, 0.6790698063908669, 0.5013786801063624, 16 0.795311915725105, 0.9183033261617566, 17 ], 18 height: 93773, 19 }, 20 { 21 name: 'nes0j', 22 vector: [ 23 0.8761291569818763, 0.07127366044153227, 0.775648976160332, 24 0.5619757601304878, 0.6076543120476996, 0.8373907516027586, 25 0.8556140171597648, 0.4043893119391049, 26 ], 27 height: 85122, 28 }, 29];
Once we have the data, you can insert data into the collection by calling the insert
method.
1await client.insert({ 2 collection_name, 3 data, 4});
By creating an index and loading the collection into memory, you can improve the performance of search and retrieval operations in Milvus, making it faster and more efficient to work with large-scale datasets.
1// create index 2await client.createIndex({ 3 collection_name, // required 4 field_name: 'vector', // optional if you are using milvus v2.2.9+ 5 index_name: 'myindex', // optional 6 index_type: 'HNSW', // optional if you are using milvus v2.2.9+ 7 params: { efConstruction: 10, M: 4 }, // optional if you are using milvus v2.2.9+ 8 metric_type: 'L2', // optional if you are using milvus v2.2.9+ 9});
Milvus supports several different types of indexes, each of which is optimized for different use cases and data distributions. Some of the most commonly used index types in Milvus include HNSW, IVF_FLAT, IVF_SQ8, IVF_PQ. When creating an index in Milvus, you must choose an appropriate index type based on your specific use case and data distribution.
When you create a collection in Milvus, the collection data is initially stored on disk, and it is not immediately available for search and retrieval. In order to search or retrieve data from the collection, you must first load the collection into memory using the loadCollectionSync
method.
1// load collection 2await client.loadCollectionSync({ 3 collection_name, 4});
Now you can perform vector search on your collection.
1// get the search vector 2const searchVector = fields_data[0].vector; 3 4// Perform a vector search on the collection 5const res = await client.search({ 6 // required 7 collection_name, // required, the collection name 8 data: searchVector, // required, vector used to compare other vectors in milvus 9 // optionals 10 filter: 'height > 0', // optional, filter expression 11 params: { nprobe: 64 }, // optional, specify the search parameters 12 limit: 10, // optional, specify the number of nearest neighbors to return 13 output_fields: ['height', 'name'], // optional, specify the fields to return in the search results, 14});
git submodule init
(if this is your first time)git submodule update --remote
yarn test -- test/Your-test-for-your-feature.spec.ts
No vulnerabilities found.
No security vulnerabilities found.