AWS Bedrock 연동

AWS Bedrock 연동 (bedrock)

Qdrant에서 AWS Bedrock을 사용할 수 있어요. AWS Bedrock은 여러 embedding 모델 제공자를 지원합니다.

AWS 계정에서 다음 정보가 필요해요.

  • Region (리전)
  • Access key ID
  • Secret key

자격 증명을 구성하는 방법은 다음 AWS 문서를 참고하세요: How do I create an AWS access key

아래 코드 샘플을 사용하면 Titan Embeddings G1 - Text 모델로 임베딩을 생성할 수 있어요. 이 모델은 크기 1536의 문장 임베딩을 만들어 냅니다.

출처: Qdrant 공식 문서 — bedrock

Python 예제

# 필요한 의존성 설치
# pip install boto3 qdrant_client

import json
import boto3

from qdrant_client import QdrantClient, models

session = boto3.Session()

bedrock_client = session.client(
    "bedrock-runtime",
    region_name="<YOUR_AWS_REGION>",
    aws_access_key_id="<YOUR_AWS_ACCESS_KEY_ID>",
    aws_secret_access_key="<YOUR_AWS_SECRET_KEY>",
)

qdrant_client = QdrantClient(url="http://localhost:6333")

qdrant_client.create_collection(
    "{collection_name}",
    vectors_config=models.VectorParams(
        size=1536,
        distance=models.Distance.COSINE,
    ),
)

body = json.dumps({"inputText": "Some text to generate embeddings for"})

response = bedrock_client.invoke_model(
    body=body,
    modelId="amazon.titan-embed-text-v1",
    accept="application/json",
    contentType="application/json",
)

response_body = json.loads(response.get("body").read())

qdrant_client.upsert(
    "{collection_name}",
    points=[
        models.PointStruct(
            id=1,
            vector=response_body["embedding"],
        )
    ],
)

JavaScript 예제

// 필요한 의존성 설치
// npm install @aws-sdk/client-bedrock-runtime @qdrant/js-client-rest

import {
  BedrockRuntimeClient,
  InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";
import { QdrantClient } from '@qdrant/js-client-rest';

const main = async () => {
  const bedrockClient = new BedrockRuntimeClient({
    region: "<YOUR_AWS_REGION>",
    credentials: {
      accessKeyId: "<YOUR_AWS_ACCESS_KEY_ID>",
      secretAccessKey: "<YOUR_AWS_SECRET_KEY>",
    },
  });

  const qdrantClient = new QdrantClient({ url: 'http://localhost:6333' });

  await qdrantClient.createCollection("{collection_name}", {
    vectors: {
      size: 1536,
      distance: 'Cosine',
    },
  });

  const response = await bedrockClient.send(
    new InvokeModelCommand({
      modelId: "amazon.titan-embed-text-v1",
      body: JSON.stringify({
        inputText: "Some text to generate embeddings for",
      }),
      contentType: "application/json",
      accept: "application/json",
    })
  );

  const body = new TextDecoder().decode(response.body);

  await qdrantClient.upsert("{collection_name}", {
    points: [
      {
        id: 1,
        vector: JSON.parse(body).embedding,
      },
    ],
  });
};

main();

더 알아보기 (Learn more)