Node.js 클라이언트 라이브러리와 함께 Azure DocumentDB에서 벡터 검색을 사용하여 벡터 데이터를 효율적으로 저장하고 쿼리합니다.
이 빠른 시작 가이드에서는 모델에서 가져온 벡터가 포함된 text-embedding-3-small JSON 파일의 호텔 데이터 샘플 세트를 사용합니다. 데이터 세트에는 호텔 이름, 위치, 설명 및 벡터 포함이 포함됩니다.
GitHub에서 샘플 코드를 찾습니다.
필수 조건
- Azure 구독입니다. Azure 구독이 없는 경우 무료 계정을 만드세요.
기존 Azure DocumentDB 클러스터입니다. 클러스터가 없는 경우 새 클러스터를 만듭니다.
Azure OpenAI 리소스입니다.
사용자 지정 도메인이 구성되어 있습니다.
text-embedding-3-small모델이 배포됩니다.
Visual Studio Code. Azure DocumentDB 확장이 있는지 확인합니다.
Bash 환경을 Azure Cloud Shell에서 사용합니다. 자세한 내용은 Azure Cloud Shell을 시작하는 방법을 참조하세요.
CLI 참조 명령을 로컬에서 실행하려면 Azure CLI를 설치하십시오. Windows 또는 macOS에서 실행 중인 경우 Docker 컨테이너에서 Azure CLI를 실행하는 것이 좋습니다. 자세한 내용은 Docker 컨테이너에서 Azure CLI를 실행하는 방법을 참조하세요.
로컬 설치를 사용하는 경우 az login 명령을 사용하여 Azure CLI에 로그인합니다. 인증 프로세스를 완료하려면 터미널에 표시되는 단계를 수행합니다. 다른 로그인 옵션은 Azure CLI를 사용하여 Azure에 인증을 참조하세요.
메시지가 표시되면 처음 사용할 때 Azure CLI 확장을 설치합니다. 확장에 대한 자세한 내용은 Azure CLI로 확장 사용 및 관리를 참조하세요.
az version을 실행하여 설치된 버전과 관련 종속 라이브러리를 확인합니다. 최신 버전으로 업그레이드하려면 az upgrade를 실행합니다.
Tip
배포 전에 Azure OpenAI 모델 매개 변수를 사용자 지정하려면 이 문서의 뒷부분에 있는 Azure OpenAI 배포 사용자 지정 섹션을 참조하세요.
TypeScript. TypeScript를 전역으로 설치하세요:
npm install -g typescript
벡터를 사용하여 데이터 파일 만들기
호텔 데이터 파일에 대한 새 데이터 디렉터리를 만듭니다.
mkdir data벡터를 사용하여
Hotels_Vector.json원시 데이터 파일을 디렉터리에 복사합니다data.
Node.js 프로젝트 만들기
데이터 디렉터리와 동일한 수준에서 프로젝트에 대한 새 형제 디렉터리를 만들고 Visual Studio Code에서 엽니다.
mkdir vector-search-quickstart code vector-search-quickstart터미널에서 Node.js 프로젝트를 초기화합니다.
npm init -y npm pkg set type="module"필요한 패키지를 설치합니다.
npm install mongodb @azure/identity openai @types/node-
mongodb: MongoDB Node.js 드라이버입니다. -
@azure/identity: 암호 없는 인증을 위한 Azure Identity 라이브러리입니다. -
openai: 벡터를 만드는 OpenAI 클라이언트 라이브러리입니다. -
@types/node: Node.js에 대한 형식 정의입니다.
-
환경 변수에
.env대한 파일을 프로젝트 루트에 만듭니다.# Identity for local developer authentication with Azure CLI AZURE_TOKEN_CREDENTIALS=AzureCliCredential # Azure OpenAI Embedding Settings AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small AZURE_OPENAI_EMBEDDING_API_VERSION=2023-05-15 AZURE_OPENAI_EMBEDDING_ENDPOINT= EMBEDDING_SIZE_BATCH=16 # MongoDB configuration MONGO_CLUSTER_NAME= # Data file DATA_FILE_WITH_VECTORS=../data/Hotels_Vector.json FIELD_TO_EMBED=Description EMBEDDED_FIELD=DescriptionVector EMBEDDING_DIMENSIONS=1536 LOAD_SIZE_BATCH=100.env파일의 자리 표시자 값을 사용자 정보로 바꿉니다.-
AZURE_OPENAI_EMBEDDING_ENDPOINT: Azure OpenAI 리소스 엔드포인트 URL입니다. -
MONGO_CLUSTER_NAME: 리소스 이름입니다.
-
TypeScript를 구성하기 위해
tsconfig.json파일을 추가하세요.{ "compilerOptions": { "target": "ES2020", "module": "NodeNext", "moduleResolution": "nodenext", "declaration": true, "outDir": "./dist", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "noImplicitAny": false, "forceConsistentCasingInFileNames": true, "sourceMap": true, "resolveJsonModule": true, }, "include": [ "src/**/*" ], "exclude": [ "node_modules", "dist" ] }
npm 스크립트 만들기
package.json 파일을 편집하고 다음 스크립트를 추가합니다.
이러한 스크립트를 사용하여 TypeScript 파일을 컴파일하고 DiskANN 인덱스 구현을 실행합니다.
"scripts": {
"build": "tsc",
"start:diskann": "node --env-file .env dist/diskann.js"
}
벡터 검색을 위한 코드 파일 만들기
TypeScript 파일용 src 디렉터리를 만듭니다. DiskANN 인덱스 구현을 위해 diskann.ts와 utils.ts 두 개의 파일을 추가합니다.
mkdir src
touch src/diskann.ts
touch src/utils.ts
Tip
일부 데이터베이스와 달리 Azure DocumentDB를 사용하면 컨테이너를 만든 후 언제든지 벡터 인덱스를 만들고 삭제할 수 있습니다. 컨테이너를 만들 때는 벡터 인덱싱 정책을 정의할 필요가 없습니다.
벡터 검색을 위한 코드 만들기
다음 코드를 파일에 붙여넣습니다 diskann.ts .
import path from 'path';
import { readFileReturnJson, getClientsPasswordless, insertData, printSearchResults } from './utils.js';
// ESM specific features - create __dirname equivalent
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const config = {
query: "quintessential lodging near running trails, eateries, retail",
dbName: "Hotels",
collectionName: "hotels_diskann",
indexName: "vectorIndex_diskann",
dataFile: process.env.DATA_FILE_WITH_VECTORS!,
batchSize: parseInt(process.env.LOAD_SIZE_BATCH! || '100', 10),
embeddedField: process.env.EMBEDDED_FIELD!,
embeddingDimensions: parseInt(process.env.EMBEDDING_DIMENSIONS!, 10),
deployment: process.env.AZURE_OPENAI_EMBEDDING_MODEL!,
};
async function main() {
const { aiClient, dbClient } = getClientsPasswordless();
try {
if (!aiClient) {
throw new Error('AI client is not configured. Please check your environment variables.');
}
if (!dbClient) {
throw new Error('Database client is not configured. Please check your environment variables.');
}
await dbClient.connect();
const db = dbClient.db(config.dbName);
const collection = await db.createCollection(config.collectionName);
console.log('Created collection:', config.collectionName);
const data = await readFileReturnJson(path.join(__dirname, "..", config.dataFile));
const insertSummary = await insertData(config, collection, data);
console.log('Created vector index:', config.indexName);
// Create the vector index
const indexOptions = {
createIndexes: config.collectionName,
indexes: [
{
name: config.indexName,
key: {
[config.embeddedField]: 'cosmosSearch'
},
cosmosSearchOptions: {
kind: 'vector-diskann',
dimensions: config.embeddingDimensions,
similarity: 'COS', // 'COS', 'L2', 'IP'
maxDegree: 20, // 20 - 2048, edges per node
lBuild: 10 // 10 - 500, candidate neighbors evaluated
}
}
]
};
const vectorIndexSummary = await db.command(indexOptions);
// Create embedding for the query
const createEmbeddedForQueryResponse = await aiClient.embeddings.create({
model: config.deployment,
input: [config.query]
});
// Perform the vector similarity search
const searchResults = await collection.aggregate([
{
$search: {
cosmosSearch: {
vector: createEmbeddedForQueryResponse.data[0].embedding,
path: config.embeddedField,
k: 5
}
}
},
{
$project: {
score: {
$meta: "searchScore"
},
document: "$$ROOT"
}
}
]).toArray();
// Print the results
printSearchResults(insertSummary, vectorIndexSummary, searchResults);
} catch (error) {
console.error('App failed:', error);
process.exitCode = 1;
} finally {
console.log('Closing database connection...');
if (dbClient) await dbClient.close();
console.log('Database connection closed');
}
}
// Execute the main function
main().catch(error => {
console.error('Unhandled error:', error);
process.exitCode = 1;
});
이 메인 모듈:
유틸리티 함수를 포함합니다.
환경 변수에 대한 구성 개체를 만듭니다.
Azure OpenAI 및 Azure DocumentDB에 대한 클라이언트를 만듭니다.
MongoDB에 연결하고, 데이터베이스 및 컬렉션을 만들고, 데이터를 삽입하고, 표준 인덱스를 만듭니다.
IVF, HNSW 또는 DiskANN을 사용하는 벡터 인덱스를 만듭니다.
OpenAI 클라이언트를 사용하여 샘플 쿼리 텍스트에 대한 임베딩을 만듭니다. 파일 맨 위에 있는 쿼리를 변경할 수 있습니다.
임베딩을 사용하는 벡터 검색을 실행하고 결과를 출력합니다.
유틸리티 함수 만들기
다음 코드를 utils.ts에 붙여넣으세요.
import { MongoClient, OIDCResponse, OIDCCallbackParams } from 'mongodb';
import { AzureOpenAI } from 'openai/index.js';
import { promises as fs } from "fs";
import { AccessToken, DefaultAzureCredential, TokenCredential, getBearerTokenProvider } from '@azure/identity';
// Define a type for JSON data
export type JsonData = Record<string, any>;
export const AzureIdentityTokenCallback = async (params: OIDCCallbackParams, credential: TokenCredential): Promise<OIDCResponse> => {
const tokenResponse: AccessToken | null = await credential.getToken(['https://ossrdbms-aad.database.windows.net/.default']);
return {
accessToken: tokenResponse?.token || '',
expiresInSeconds: (tokenResponse?.expiresOnTimestamp || 0) - Math.floor(Date.now() / 1000)
};
};
export function getClients(): { aiClient: AzureOpenAI; dbClient: MongoClient } {
const apiKey = process.env.AZURE_OPENAI_EMBEDDING_KEY!;
const apiVersion = process.env.AZURE_OPENAI_EMBEDDING_API_VERSION!;
const endpoint = process.env.AZURE_OPENAI_EMBEDDING_ENDPOINT!;
const deployment = process.env.AZURE_OPENAI_EMBEDDING_MODEL!;
const mongoConnectionString = process.env.MONGO_CONNECTION_STRING!;
if (!apiKey || !apiVersion || !endpoint || !deployment || !mongoConnectionString) {
throw new Error('Missing required environment variables: AZURE_OPENAI_EMBEDDING_KEY, AZURE_OPENAI_EMBEDDING_API_VERSION, AZURE_OPENAI_EMBEDDING_ENDPOINT, AZURE_OPENAI_EMBEDDING_MODEL, MONGO_CONNECTION_STRING');
}
const aiClient = new AzureOpenAI({
apiKey,
apiVersion,
endpoint,
deployment
});
const dbClient = new MongoClient(mongoConnectionString, {
// Performance optimizations
maxPoolSize: 10, // Limit concurrent connections
minPoolSize: 1, // Maintain at least one connection
maxIdleTimeMS: 30000, // Close idle connections after 30 seconds
connectTimeoutMS: 30000, // Connection timeout
socketTimeoutMS: 360000, // Socket timeout (for long-running operations)
retryWrites: false, // Set retry writes to false just for consistency purposes
writeConcern: { // Optimize write concern for bulk operations
w: 1, // Acknowledge writes after primary has written
j: false // Don't wait for journal commit
}
});
return { aiClient, dbClient };
}
export function getClientsPasswordless(): { aiClient: AzureOpenAI | null; dbClient: MongoClient | null } {
let aiClient: AzureOpenAI | null = null;
let dbClient: MongoClient | null = null;
// Validate all required environment variables upfront
const apiVersion = process.env.AZURE_OPENAI_EMBEDDING_API_VERSION!;
const endpoint = process.env.AZURE_OPENAI_EMBEDDING_ENDPOINT!;
const deployment = process.env.AZURE_OPENAI_EMBEDDING_MODEL!;
const clusterName = process.env.MONGO_CLUSTER_NAME!;
if (!apiVersion || !endpoint || !deployment || !clusterName) {
throw new Error('Missing required environment variables: AZURE_OPENAI_EMBEDDING_API_VERSION, AZURE_OPENAI_EMBEDDING_ENDPOINT, AZURE_OPENAI_EMBEDDING_MODEL, MONGO_CLUSTER_NAME');
}
console.log(`Using Azure OpenAI Embedding API Version: ${apiVersion}`);
console.log(`Using Azure OpenAI Embedding Deployment/Model: ${deployment}`);
const credential = new DefaultAzureCredential();
// For Azure OpenAI with DefaultAzureCredential
{
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
aiClient = new AzureOpenAI({
apiVersion,
endpoint,
deployment,
azureADTokenProvider
});
}
// For DocumentDB with DefaultAzureCredential (uses signed-in user)
{
dbClient = new MongoClient(
`mongodb+srv://${clusterName}.global.mongocluster.cosmos.azure.com/`, {
connectTimeoutMS: 120000,
tls: true,
retryWrites: false,
maxIdleTimeMS: 120000,
authMechanism: 'MONGODB-OIDC',
authMechanismProperties: {
OIDC_CALLBACK: (params: OIDCCallbackParams) => AzureIdentityTokenCallback(params, credential),
ALLOWED_HOSTS: ['*.azure.com']
}
}
);
}
return { aiClient, dbClient };
}
export async function readFileReturnJson(filePath: string): Promise<JsonData[]> {
console.log(`Reading JSON file from ${filePath}`);
const fileAsString = await fs.readFile(filePath, "utf-8");
return JSON.parse(fileAsString);
}
export async function writeFileJson(filePath: string, jsonData: JsonData): Promise<void> {
const jsonString = JSON.stringify(jsonData, null, 2);
await fs.writeFile(filePath, jsonString, "utf-8");
console.log(`Wrote JSON file to ${filePath}`);
}
export async function insertData(config, collection, data) {
console.log(`Processing in batches of ${config.batchSize}...`);
const totalBatches = Math.ceil(data.length / config.batchSize);
let inserted = 0;
let updated = 0;
let skipped = 0;
let failed = 0;
for (let i = 0; i < totalBatches; i++) {
const start = i * config.batchSize;
const end = Math.min(start + config.batchSize, data.length);
const batch = data.slice(start, end);
try {
const result = await collection.insertMany(batch, { ordered: false });
inserted += result.insertedCount || 0;
console.log(`Batch ${i + 1} complete: ${result.insertedCount} inserted`);
} catch (error: any) {
if (error?.writeErrors) {
// Some documents may have been inserted despite errors
console.error(`Error in batch ${i + 1}: ${error?.writeErrors.length} failures`);
failed += error?.writeErrors.length;
inserted += batch.length - error?.writeErrors.length;
} else {
console.error(`Error in batch ${i + 1}:`, error);
failed += batch.length;
}
}
// Small pause between batches to reduce resource contention
if (i < totalBatches - 1) {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
const indexColumns = [
"HotelId",
"Category",
"Description",
"Description_fr"
];
for (const col of indexColumns) {
const indexSpec = {};
indexSpec[col] = 1; // Ascending index
await collection.createIndex(indexSpec);
}
return { total: data.length, inserted, updated, skipped, failed };
}
export function printSearchResults(insertSummary, indexSummary, searchResults) {
if (!searchResults || searchResults.length === 0) {
console.log('No search results found.');
return;
}
searchResults.map((result, index) => {
const { document, score } = result as any;
console.log(`${index + 1}. HotelName: ${document.HotelName}, Score: ${score.toFixed(4)}`);
//console.log(` Description: ${document.Description}`);
});
}
이 유틸리티 모듈은 다음과 같은 기능을 제공합니다.
JsonData: 데이터 구조에 대한 인터페이스를 지정합니다.scoreProperty: 벡터 검색 방법을 기반으로 쿼리 결과에서 점수의 위치를 식별합니다.getClients: Azure OpenAI 및 Azure DocumentDB에 대한 클라이언트를 만들고 반환합니다.getClientsPasswordless: 암호 없는 인증을 사용하여 Azure OpenAI 및 Azure DocumentDB에 대한 클라이언트를 만들고 반환합니다. 두 리소스에서 RBAC(역할 기반 액세스 제어)를 사용하도록 설정하고 Azure CLI에 로그인합니다.readFileReturnJson: JSON 파일을 읽고 해당 내용을JsonData개체의 배열로 반환합니다.writeFileJson: JSON 파일에JsonData개체의 배열을 씁니다.insertData: MongoDB 컬렉션에 데이터를 일괄 처리로 삽입하고 지정된 필드에 표준 인덱스를 만듭니다.printSearchResults: 점수 및 호텔 이름을 포함하여 벡터 검색의 결과를 출력합니다.
Azure CLI를 사용하여 인증
애플리케이션을 실행하기 전에 앱이 Azure 리소스에 안전하게 액세스할 수 있도록 Azure CLI 로그인합니다.
az login
이 코드는 로컬 개발자 인증을 사용하여 Azure DocumentDB 및 Azure OpenAI에 getClientsPasswordless의 utils.ts 함수를 사용해 액세스합니다.
AZURE_TOKEN_CREDENTIALS=AzureCliCredential를 설정하면 이 설정은 함수가 인증에 Azure CLI 자격 증명을 결정적으로 사용하도록 지정합니다. 이 함수는 환경에서 Azure 자격 증명을 찾기 위해 @azure/identity의 DefaultAzureCredential를 사용합니다.
Azure ID 라이브러리를 사용하여 Azure 서비스에 JavaScript 앱을 인증하는 방법에 대해 자세히 알아봅니다.
애플리케이션 빌드 및 실행
TypeScript 파일을 빌드한 다음 애플리케이션을 실행합니다.
앱 로깅 및 출력은 다음을 보여줍니다.
- 컬렉션 만들기 및 데이터 삽입 상태입니다.
- 벡터 인덱스 만들기.
- 호텔 이름과 유사성 점수가 포함된 검색 결과.
Using Azure OpenAI Embedding API Version: 2023-05-15
Using Azure OpenAI Embedding Deployment/Model: text-embedding-3-small-2
Created collection: hotels_diskann
Reading JSON file from \documentdb-samples\ai\data\Hotels_Vector.json
Processing in batches of 50...
Batch 1 complete: 50 inserted
Created vector index: vectorIndex_diskann
1. HotelName: Royal Cottage Resort, Score: 0.4991
2. HotelName: Country Comfort Inn, Score: 0.4785
3. HotelName: Nordick's Valley Motel, Score: 0.4635
4. HotelName: Economy Universe Motel, Score: 0.4461
5. HotelName: Roach Motel, Score: 0.4388
Closing database connection...
Database connection closed
Visual Studio Code에서 데이터 보기 및 관리
Visual Studio Code에서 Azure DocumentDB 확장을 선택하여 Azure DocumentDB 계정에 연결합니다.
Hotels 데이터베이스에서 데이터 및 인덱스를 봅니다.
Azure OpenAI 배포 사용자 지정(선택 사항)
인프라는 infra/main.bicepparam에 정의된 기본 모델 및 지역 설정으로 Azure OpenAI를 배포합니다. 실행 azd up하기 전에 다음 매개 변수를 사용자 지정할 수 있습니다.
| Parameter | Default | Description |
|---|---|---|
AZURE_OPENAI_LOCATION |
AZURE_LOCATION 동일 |
OpenAI 리소스 배포를 위한 지역 |
AZURE_OPENAI_EMBEDDING_MODEL |
text-embedding-3-small |
임베딩 모델 이름 |
AZURE_OPENAI_EMBEDDING_MODEL_VERSION |
1 |
임베딩 모델 버전 |
AZURE_OPENAI_EMBEDDING_MODEL_TYPE |
Standard |
배포 유형(Standard 또는 GlobalStandard) |
기본값을 재정의하려면 azd env set를 실행하기 전에 azd up를 사용합니다.
# Deploy OpenAI to a different region than your other resources
azd env set AZURE_OPENAI_LOCATION swedencentral
# Switch deployment type from Standard (default) to GlobalStandard
azd env set AZURE_OPENAI_EMBEDDING_MODEL_TYPE GlobalStandard
# Change the embedding model
azd env set AZURE_OPENAI_EMBEDDING_MODEL text-embedding-3-small
azd env set AZURE_OPENAI_EMBEDDING_MODEL_VERSION 1
참고
모든 지역 또는 모든 배포 유형에서 모든 모델을 사용할 수 있는 것은 아닙니다. 지원되는 조합은 Azure 지역별 OpenAI 모델 가용성에서 확인하세요.
Azure OpenAI 프로비저닝 실패 문제 해결
Azure OpenAI 리소스 또는 모델 배포를 만들 때 azd up에 실패하는 경우, 이는 대부분 다음 문제 중 하나 때문일 수 있습니다.
지역 가용성: 선택한 지역에서 모델을 사용할 수 없습니다.
azd env set AZURE_OPENAI_LOCATION <different-region>(예:eastus2또는swedencentral)을 시도해 보세요.배포 유형 불일치: 모델은 해당 지역에서 선택한 배포 유형을 지원하지 않습니다.
GlobalStandard를 사용하여azd env set AZURE_OPENAI_EMBEDDING_MODEL_TYPE GlobalStandard와Standard사이를 전환합니다.할당량 제한: 구독이 선택한 모델, 지역 또는 배포 유형 조합에 대한 할당량을 초과했습니다. Azure Portal의 Azure OpenAI>할당량 아래에서 할당량을 확인하세요. 할당량 증가를 요청하거나 사용 가능한 용량이 있는 다른 지역을 사용해 볼 수 있습니다.
사용 중지되었거나 사용할 수 없는 모델: Azure OpenAI는 이전 모델 버전을 주기적으로 사용 중지합니다. 모델 버전을 더 이상 사용할 수 없어 배포가 실패하는 경우
azd env set AZURE_OPENAI_EMBEDDING_MODEL_VERSION <new-version>를 사용하여 지원되는 버전으로 업데이트합니다. 현재 모델 수명 주기 상태에 대한 Azure OpenAI 모델 사용 중단을 확인합니다.
매개 변수를 변경한 후 azd up를 다시 실행하여 배포를 다시 시도합니다.
자원을 정리하세요
더 이상 필요하지 않은 경우 불필요한 비용을 방지하기 위해 리소스 그룹, Azure DocumentDB 클러스터 및 Azure OpenAI 리소스를 삭제하세요.