Bedrock Knowledge Bases

Bedrock Knowledge Bases

AWS Bedrock Knowledge Bases를 통해 LLM을 조직의 데이터에 연결해, 모델이 비즈니스 고유의 정보를 검색하고 참조할 수 있게 해요.

출처: 문서

본문

개요 (Overview)

속성 설명
설명 Bedrock Knowledge Bases는 데이터를 LLM에 연결해 응답에서 조직 정보를 검색·참조할 수 있게 해요
LiteLLM 라우트 litellm vector_store_registry의 bedrock
공급자 문서 AWS Bedrock Knowledge Bases

빠른 시작 (Quick Start)

LiteLLM Python SDK

import os
import litellm
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry, LiteLLM_ManagedVectorStore

# Init vector store registry with your Bedrock Knowledge Base
litellm.vector_store_registry = VectorStoreRegistry(
    vector_stores=[
        LiteLLM_ManagedVectorStore(
            vector_store_id="YOUR_KNOWLEDGE_BASE_ID",  # KB ID from AWS Bedrock
            custom_llm_provider="bedrock"
        )
    ]
)

# Make a completion request using your Knowledge Base
response = await litellm.acompletion(
    model="anthropic/claude-sonnet-5",
    messages=[{"role": "user", "content": "What does our company policy say about remote work?"}],
    tools=[
        {
            "type": "file_search",
            "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]
        }
    ],
)
print(response.choices[0].message.content)

LiteLLM Proxy

1. vector_store_registry 구성:

model_list:
  - model_name: claude-sonnet-5
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY

vector_store_registry:
  - vector_store_name: "bedrock-company-docs"
    litellm_params:
      vector_store_id: "YOUR_KNOWLEDGE_BASE_ID"
      custom_llm_provider: "bedrock"
      vector_store_description: "Bedrock Knowledge Base for company documents"
      vector_store_metadata:
        source: "Company internal documentation"

LiteLLM UI에서도 생성할 수 있어요: Experimental > Vector Stores > Create Vector Store에서 이름, vector store id, 자격 증명으로 벡터 스토어를 만들 수 있어요.

2. vector_store_ids 파라미터로 요청:

curl:

curl http://localhost:4000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "claude-sonnet-5",
    "messages": [{"role": "user", "content": "What does our company policy say about remote work?"}],
    "tools": [
      {
        "type": "file_search",
        "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]
      }
    ]
  }'

OpenAI Python SDK:

from openai import OpenAI

# Initialize client with your LiteLLM proxy URL
client = OpenAI(
    base_url="http://localhost:4000",
    api_key="your-litellm-api-key",
)

# Make a completion request with vector_store_ids parameter
response = client.chat.completions.create(
    model="claude-sonnet-5",
    messages=[{"role": "user", "content": "What does our company policy say about remote work?"}],
    tools=[
        {
            "type": "file_search",
            "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]
        }
    ]
)
print(response.choices[0].message.content)

결과 필터링 (Filter Results)

메타데이터 속성으로 필터링할 수 있어요.

연산자 (OpenAI 스타일, 자동 변환): eq, ne, gt, gte, lt, lte, in, nin

AWS 연산자 (직접 사용): equals, notEquals, greaterThan, greaterThanOrEquals, lessThan, lessThanOrEquals, in, notIn, startsWith, listContains, stringContains

단일 필터:

response = await litellm.acompletion(
    model="anthropic/claude-sonnet-5",
    messages=[{"role": "user", "content": "What are the latest updates?"}],
    tools=[{
        "type": "file_search",
        "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"],
        "filters": {
            "key": "category",
            "value": "updates",
            "operator": "eq"
        }
    }]
)

AND 필터:

response = await litellm.acompletion(
    model="anthropic/claude-sonnet-5",
    messages=[{"role": "user", "content": "What are the policies?"}],
    tools=[{
        "type": "file_search",
        "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"],
        "filters": {
            "and": [
                {"key": "category", "value": "policy", "operator": "eq"},
                {"key": "year", "value": 2024, "operator": "gte"}
            ]
        }
    }]
)

OR 필터:

response = await litellm.acompletion(
    model="anthropic/claude-sonnet-5",
    messages=[{"role": "user", "content": "Show me technical docs"}],
    tools=[{
        "type": "file_search",
        "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"],
        "filters": {
            "or": [
                {"key": "category", "value": "api", "operator": "eq"},
                {"key": "category", "value": "sdk", "operator": "eq"}
            ]
        }
    }]
)

문자열 포함 필터:

response = await litellm.acompletion(
    model="anthropic/claude-sonnet-5",
    messages=[{"role": "user", "content": "Find docs"}],
    tools=[{
        "type": "file_search",
        "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"],
        "filters": {
            "and": [
                {"key": "title", "value": "Guide", "operator": "stringContains"},
                {"key": "tags", "value": "important", "operator": "listContains"}
            ]
        }
    }]
)

Proxy (curl):

curl http://localhost:4000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "claude-sonnet-5",
    "messages": [{"role": "user", "content": "What are our policies?"}],
    "tools": [{
      "type": "file_search",
      "vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"],
      "filters": {
        "and": [
          {"key": "department", "value": "engineering", "operator": "eq"},
          {"key": "type", "value": "policy", "operator": "eq"}
        ]
      }
    }]
  }'

사용자에게 결과 제한 (Restrict Results to a User)

접근 제어가 있는 Knowledge Bases(Kendra GenAI 인덱스 또는 문서 수준 ACL이 있는 데이터 소스)는 특정 사용자가 볼 수 있는 청크만 반환해요. 그 신원을 Bedrock의 userContext로 전달하면 LiteLLM이 Retrieve 요청에 그대로 전송해요. 값은 그대로 전송되므로 Bedrock이 이를 검증해요: userId는 문자열이어야 해요. LiteLLM은 proxy 키에 대해 신원을 확인하지 않으므로, 자신의 사용자를 명명하도록 신뢰하는 호출자만 ACL이 있는 스토어를 검색할 수 있어야 해요.

LiteLLM Python SDK:

import litellm

response = litellm.vector_stores.search(
    vector_store_id="YOUR_KNOWLEDGE_BASE_ID",
    custom_llm_provider="bedrock",
    query="What does our company policy say about remote work?",
    extra_body={"userContext": {"userId": "[email protected]"}},
)

Proxy (OpenAI SDK):

from openai import OpenAI

client = OpenAI(base_url="http://localhost:4000", api_key="your-litellm-api-key")

response = client.vector_stores.search(
    vector_store_id="YOUR_KNOWLEDGE_BASE_ID",
    query="What does our company policy say about remote work?",
    extra_body={"userContext": {"userId": "[email protected]"}},
)

Proxy (curl):

curl http://localhost:4000/v1/vector_stores/YOUR_KNOWLEDGE_BASE_ID/search \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "query": "What does our company policy say about remote work?",
    "userContext": {"userId": "[email protected]"}
  }'

Proxy (config.yaml):

스토어에 user_context를 설정하면 그 저장소에 대한 모든 검색에 하나의 신원을 적용해요. 요청에 보낸 userContext가 이를 오버라이드해요.

vector_store_registry:
  - vector_store_name: "bedrock-company-docs"
    litellm_params:
      vector_store_id: "YOUR_KNOWLEDGE_BASE_ID"
      custom_llm_provider: "bedrock"
      user_context:
        userId: "[email protected]"

검색 결과 접근 (Accessing Search Results)

응답에서 벡터 스토어 검색 결과에 접근하는 방법은 "Accessing Search Results (Non-Streaming & Streaming)" 문서를 참고해요.

더 알아보기 (Learn more)