카테고리 없음
[Milvus] Docker로 실행하여 Nodejs SDK Client 연결
parkpark4
2024. 3. 12. 14:12
Milvus를 선택한 이유
- 오픈소스
- Git star수가 가장 높음
- Docs가 깔끔함 (https://milvus.io/docs/index.md)
- vector index 종류가 많음
- 지원하는 언어가 많음
Requirement
Node.js v12+
Docker
Quick Start
이 가이드에서는 Node.js와 Milvus를 사용하여 간단한 애플리케이션을 설정하는 방법을 보여줍니다. 그 범위는 node.js 클라이언트를 설정하고 간단한 CRUD 작업을 수행하는 방법뿐입니다. 더 자세한 내용은 Milvus 공식 웹사이트를 참조하세요
Create the package.json file
directory 생성
mkdir myProject
cd myProject
새 프로젝트 초기화 하기
npm init -y
클라이언트를 종속성으로 설치
npm install @zilliz/milvus2-sdk-node
Start a milvus server
# Download the milvus standalone yaml file
wget https://github.com/milvus-io/milvus/releases/download/v2.2.8/milvus-standalone-docker-compose.yml -O docker-compose.yml
# start the milvus server
sudo docker-compose up -d
Connect to Milvus
app.js 파일을 만들고 다음 코드를 추가하여 Milvus node.js 클라이언트 사용
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
/**
* Connect to Milvus
*/
const address = 'localhost:19530';
const token = "root:Milvus"
const ssl = false;
const client = new MilvusClient({address, ssl, token});
예제 코드 작성하여 실행결과 확인
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
/**
* Connect to Milvus
*/
const address = 'localhost:19530';
const token = "root:Milvus"
const ssl = false;
const client = new MilvusClient({address, ssl, token});
/**
* Create a collection
*/
// define schema
const collection_name = `hello_milvus`;
const dim = 128;
const schema = [
{
name: 'age',
description: 'ID field',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
description: 'Vector field',
data_type: DataType.FloatVector,
dim: 8,
},
{ name: 'height', description: 'int64 field', data_type: DataType.Int64 },
{
name: 'name',
description: 'VarChar field',
data_type: DataType.VarChar,
max_length: 128,
},
];
// Create the collection
await client.createCollection({
collection_name,
fields: schema,
});
/**
* Prepare data
*/
const fields_data = [
{
vector: [
0.11878310581111173, 0.9694947902934701, 0.16443679307243175,
0.5484226189097237, 0.9839246709011924, 0.5178387104937776,
0.8716926129208069, 0.5616972243831446,
],
height: 20405,
name: 'zlnmh',
},
{
vector: [
0.9992090731236536, 0.8248790611809487, 0.8660083940881405,
0.09946359318481224, 0.6790698063908669, 0.5013786801063624,
0.795311915725105, 0.9183033261617566,
],
height: 93773,
name: '5lr9y',
},
{
vector: [
0.8761291569818763, 0.07127366044153227, 0.775648976160332,
0.5619757601304878, 0.6076543120476996, 0.8373907516027586,
0.8556140171597648, 0.4043893119391049,
],
height: 85122,
name: 'nes0j',
},
];
/**
* Insert data into collection
*/
await client.insert({
collection_name,
fields_data,
});
/**
* Create index
*/
// create index
await client.createIndex({
// required
collection_name,
field_name: 'vector', // optional if you are using milvus v2.2.9+
index_name: 'myindex', // optional
index_type: 'HNSW', // optional if you are using milvus v2.2.9+
params: { efConstruction: 10, M: 4 }, // optional if you are using milvus v2.2.9+
metric_type: 'L2', // optional if you are using milvus v2.2.9+
});
/**
* Load collection
*/
// load collection
await client.loadCollectionSync({
collection_name,
});
/**
* vector search
*/
// get the search vector
const searchVector = fields_data[0].vector;
// Perform a vector search on the collection
const res = await client.search({
// required
collection_name, // required, the collection name
vector: searchVector, // required, vector used to compare other vectors in milvus
// optionals
filter: 'height > 0', // optional, filter
params: { nprobe: 64 }, // optional, specify the search parameters
limit: 10, // optional, specify the number of nearest neighbors to return
metric_type: 'L2', // optional, metric to calculate similarity of two vectors
output_fields: ['height', 'name'], // optional, specify the fields to return in the search results
});
실행 결과 res 확인
{ "status": { "error_code": "Success", "reason": "", "code": 0, "retriable": false, "detail": "" }, "results": [ { "score": 0, "id": "448230196146166133", "height": "20405", "name": "zlnmh" }, { "score": 1.7160398960113525, "id": "448230196146166134", "height": "93773", "name": "5lr9y" }, { "score": 2.0228352546691895, "id": "448230196146166135", "height": "85122", "name": "nes0j" } ] }
- searchVector 확인
0.11878310581111173,0.9694947902934701,0.16443679307243175,0.5484226189097237,0.9839246709011924,0.5178387104937776,0.8716926129208069,0.5616972243831446