RAG using DeepSeek-R1 on Amazon Bedrock
출처: 문서
본문
Amazon Bedrock에서 DeepSeek-R1을 사용하는 RAG
이 튜토리얼에서는 Amazon OpenSearch Service와 DeepSeek-R1 모델을 사용해 검색 증강 생성(RAG)을 구현하는 방법을 보여줘요.
Amazon OpenSearch Service가 아니라 셀프 관리형 OpenSearch를 사용하는 경우에는 블루프린트를 사용해 DeepSeek-R1 모델에 대한 커넥터를 생성하세요. 커넥터 생성에 대한 자세한 내용은 Connectors를 참조하세요. 그런 다음 바로 4단계로 이동하세요.
your_ 접두사로 시작하는 플레이스홀더는 여러분의 값으로 교체하세요.
{: .note}
사전 준비 사항
시작하기 전에 다음 사전 준비 사항을 충족하세요.
Amazon 설정을 구성할 때는 이 튜토리얼에서 언급한 값만 변경하세요. 다른 모든 설정은 기본값으로 유지하세요. {: .important}
Amazon Bedrock에 DeepSeek-R1 배포
Amazon Bedrock에 DeepSeek-R1을 배포하세요. 자세한 내용은 Amazon Bedrock을 참조하세요. Amazon Bedrock DeepSeek-R1 모델의 Amazon Resource Name(ARN)을 기록해 두세요. 이후 단계에서 사용하게 돼요.
OpenSearch 클러스터 생성
Amazon OpenSearch Service 콘솔로 이동해 OpenSearch 도메인을 생성하세요.
도메인 ARN과 URL을 기록해 두세요. 이후 단계에서 사용하게 돼요.
1단계: Amazon Bedrock 액세스용 IAM 역할 생성
Amazon Bedrock에서 DeepSeek-R1 모델을 호출하려면 적절한 권한을 가진 AWS Identity and Access Management(IAM) 역할을 생성해야 해요. 커넥터는 이 역할을 사용해 모델을 호출해요.
IAM 콘솔로 이동해 my_invoke_bedrock_deepseek_model_role이라는 이름의 새 IAM 역할을 생성하고 다음 신뢰 정책과 권한을 추가하세요:
- 사용자 지정 신뢰 정책:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "es.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
{% include copy.html %}
- 권한:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"bedrock:InvokeModel"
],
"Effect": "Allow",
"Resource": "your_DeepSeek_R1_model_ARN"
}
]
}
{% include copy.html %}
역할 ARN을 기록해 두세요. 이후 단계에서 사용하게 돼요.
2단계: Amazon OpenSearch Service에서 IAM 역할 구성
다음 단계에 따라 Amazon OpenSearch Service에서 IAM 역할을 구성하세요.
2.1단계: 커넥터 요청 서명용 IAM 역할 생성
Create Connector API 요청에 서명하기 위한 새 IAM 역할을 생성하세요.
다음 신뢰 정책과 권한을 가진 my_create_bedrock_deepseek_connector_role이라는 이름의 IAM 역할을 생성하세요:
- 사용자 지정 신뢰 정책:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "your_iam_user_arn"
},
"Action": "sts:AssumeRole"
}
]
}
{% include copy.html %}
3단계에서 your_iam_user_arn IAM 사용자를 사용해 이 역할을 수임하게 돼요.
- 권한:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "your_iam_role_arn_created_in_step1"
},
{
"Effect": "Allow",
"Action": "es:ESHttpPost",
"Resource": "your_opensearch_domain_arn"
}
]
}
{% include copy.html %}
이 역할 ARN을 기록해 두세요. 이후 단계에서 사용하게 돼요.
2.2단계: 백엔드 역할 매핑
다음 단계에 따라 백엔드 역할을 매핑하세요:
- OpenSearch Dashboards에 로그인하고 상단 메뉴에서 Security를 선택하세요.
- Roles를 선택한 다음 ml_full_access 역할을 선택하세요.
- ml_full_access 역할 세부 정보 페이지에서 Mapped users를 선택한 다음 Manage mapping을 선택하세요.
- 2.1단계에서 생성한 IAM 역할 ARN을 다음 이미지와 같이 Backend roles 필드에 입력하세요.

- Map을 선택하세요.
이제 IAM 역할이 OpenSearch 클러스터에 성공적으로 구성됐어요.
3단계: 커넥터 생성
다음 단계에 따라 DeepSeek-R1 모델용 커넥터를 생성하세요. 커넥터 생성에 대한 자세한 내용은 Connectors를 참조하세요.
AWS에서 가져온 임시 자격 증명으로 다음 Python 코드를 실행하세요.
import boto3
import requests
from requests_aws4auth import AWS4Auth
host = 'your_amazon_opensearch_domain_endpoint'
region = 'your_amazon_opensearch_domain_region'
service = 'es'
assume_role_response = boto3.Session().client('sts').assume_role(
RoleArn="your_iam_role_arn_created_in_step2.1",
RoleSessionName="your_session_name"
)
credentials = assume_role_response["Credentials"]
awsauth = AWS4Auth(credentials["AccessKeyId"], credentials["SecretAccessKey"], region, service, session_token=credentials["SessionToken"])
path = '/_plugins/_ml/connectors/_create'
url = host + path
payload = {
"name": "DeepSeek R1 model connector",
"description": "Connector for my Bedrock DeepSeek model",
"version": "1.0",
"protocol": "aws_sigv4",
"credential": {
"roleArn": "your_iam_role_arn_created_in_step1"
},
"parameters": {
"service_name": "bedrock",
"region": "your_bedrock_model_region",
"model_id": "your_deepseek_bedrock_model_arn",
"temperature": 0,
"max_gen_len": 4000
},
"actions": [
{
"action_type": "PREDICT",
"method": "POST",
"url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/${parameters.model_id}/invoke",
"headers": {
"content-type": "application/json"
},
"request_body": "{ \"prompt\": \"<|begin▁of▁sentence|><|User|>${parameters.inputs}<|Assistant|>\", \"temperature\": ${parameters.temperature}, \"max_gen_len\": ${parameters.max_gen_len} }",
"post_process_function": "\n return '{' +\n '\"name\": \"response\",'+\n '\"dataAsMap\": {' +\n '\"completion\":\"' + escape(params.generation) + '\"}' +\n '}';\n "
}
]
}
headers = {"Content-Type": "application/json"}
r = requests.post(url, auth=awsauth, json=payload, headers=headers)
print(r.status_code)
print(r.text)
{% include copy.html %}
스크립트는 커넥터 ID를 출력해요:
{"connector_id":"HnS5sJQBVQUimUskjpFl"}
커넥터 ID를 기록해 두세요. 다음 단계에서 사용하게 돼요.
4단계: 모델 생성 및 테스트
OpenSearch Dashboards에 로그인하고 DevTools 콘솔을 연 다음, 다음 요청을 실행해 DeepSeek-R1 모델을 생성하고 테스트하세요.
-
모델 그룹을 생성하세요:
POST /_plugins/_ml/model_groups/_register { "name": "Bedrock DeepSeek model", "description": "Test model group for Bedrock DeepSeek model" }{% include copy-curl.html %}
응답에 모델 그룹 ID가 포함돼요:
{ "model_group_id": "Vylgs5QBts7fa6bylR0v", "status": "CREATED" } -
모델을 등록하세요:
POST /_plugins/_ml/models/_register { "name": "Bedrock DeepSeek R1 model", "function_name": "remote", "description": "DeepSeek R1 model on Bedrock", "model_group_id": "Vylgs5QBts7fa6bylR0v", "connector_id": "KHS7s5QBVQUimUskoZGp" }{% include copy-curl.html %}
응답에 모델 ID가 포함돼요:
{ "task_id": "hOS7s5QBFSAM-Wczv7KD", "status": "CREATED", "model_id": "heS7s5QBFSAM-Wczv7Kb" } -
모델을 배포하세요:
POST /_plugins/_ml/models/heS7s5QBFSAM-Wczv7Kb/_deploy{% include copy-curl.html %}
응답에 배포 작업의 태스크 ID가 포함돼요:
{ "task_id": "euRhs5QBFSAM-WczTrI6", "task_type": "DEPLOY_MODEL", "status": "COMPLETED" } -
모델을 테스트하세요:
POST /_plugins/_ml/models/heS7s5QBFSAM-Wczv7Kb/_predict { "parameters": { "inputs": "hello" } }{% include copy-curl.html %}
응답에 모델이 생성한 텍스트가 포함돼요:
{ "inference_results": [ { "output": [ { "name": "response", "dataAsMap": { "completion": """<think>\n\n</think>\n\nHello! How can I assist you today? 😊""" } } ], "status_code": 200 } ] }
5단계: RAG 구성
다음 단계에 따라 RAG를 구성하세요.
5.1단계: 검색 파이프라인 생성
RAG processor가 포함된 검색 파이프라인을 생성하세요:
PUT /_search/pipeline/my-conversation-search-pipeline-deepseek
{
"response_processors": [
{
"retrieval_augmented_generation": {
"tag": "Demo pipeline",
"description": "Demo pipeline Using DeepSeek R1",
"model_id": "heS7s5QBFSAM-Wczv7Kb",
"context_field_list": [
"text"
],
"system_prompt": "You are a helpful assistant.",
"user_instructions": "Generate a concise and informative answer in less than 100 words for the given question"
}
}
]
}
{% include copy-curl.html %}
5.2단계: 벡터 데이터베이스 생성
이 튜토리얼의 1단계와 2단계를 따라 임베딩 모델과 벡터 인덱스를 생성하세요. 그런 다음 샘플 데이터를 인덱스에 수집하세요:
POST _bulk
{"index": {"_index": "my-nlp-index", "_id": "1"}}
{"text": "Chart and table of population level and growth rate for the Ogden-Layton metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Ogden-Layton in 2023 is 750,000, a 1.63% increase from 2022.\nThe metro area population of Ogden-Layton in 2022 was 738,000, a 1.79% increase from 2021.\nThe metro area population of Ogden-Layton in 2021 was 725,000, a 1.97% increase from 2020.\nThe metro area population of Ogden-Layton in 2020 was 711,000, a 2.16% increase from 2019."}
{"index": {"_index": "my-nlp-index", "_id": "2"}}
{"text": "Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."}
{"index": {"_index": "my-nlp-index", "_id": "3"}}
{"text": "Chart and table of population level and growth rate for the Chicago metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Chicago in 2023 is 8,937,000, a 0.4% increase from 2022.\\nThe metro area population of Chicago in 2022 was 8,901,000, a 0.27% increase from 2021.\\nThe metro area population of Chicago in 2021 was 8,877,000, a 0.14% increase from 2020.\\nThe metro area population of Chicago in 2020 was 8,865,000, a 0.03% increase from 2019."}
{"index": {"_index": "my-nlp-index", "_id": "4"}}
{"text": "Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019."}
{"index": {"_index": "my-nlp-index", "_id": "5"}}
{"text": "Chart and table of population level and growth rate for the Austin metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Austin in 2023 is 2,228,000, a 2.39% increase from 2022.\\nThe metro area population of Austin in 2022 was 2,176,000, a 2.79% increase from 2021.\\nThe metro area population of Austin in 2021 was 2,117,000, a 3.12% increase from 2020.\\nThe metro area population of Austin in 2020 was 2,053,000, a 3.43% increase from 2019."}
{"index": {"_index": "my-nlp-index", "_id": "6"}}
{"text": "Chart and table of population level and growth rate for the Seattle metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\nThe metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\nThe metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\nThe metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."}
{% include copy-curl.html %}
5.3단계: 인덱스 검색
벡터 검색을 실행해 벡터 데이터베이스에서 문서를 검색하고, RAG에 DeepSeek 모델을 사용하세요:
GET /my-nlp-index/_search?search_pipeline=my-conversation-search-pipeline-deepseek
{
"query": {
"neural": {
"passage_embedding": {
"query_text": "What's the population increase of New York City from 2021 to 2023? How is the trending comparing with Miami?",
"model_id": "heS7s5QBFSAM-Wczv7Kb",
"k": 5
}
}
},
"size": 2,
"_source": [
"text"
],
"ext": {
"generative_qa_parameters": {
"llm_model": "bedrock/claude",
"llm_question": "What's the population increase of New York City from 2021 to 2023? How is the trending comparing with Miami?",
"context_size": 5,
"timeout": 15
}
}
}
{% include copy-curl.html %}
응답에는 벡터 검색에서 검색된 관련 문서(hits 배열)와 DeepSeek 모델이 생성한 답변(ext.retrieval_augmented_generation 객체)이 모두 포함돼요:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.04107812,
"hits": [
{
"_index": "my-nlp-index",
"_id": "4",
"_score": 0.04107812,
"_source": {
"text": """Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019."""
}
},
{
"_index": "my-nlp-index",
"_id": "2",
"_score": 0.03810156,
"_source": {
"text": """Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."""
}
}
]
},
"ext": {
"retrieval_augmented_generation": {
"answer": """You are a helpful assistant.\nGenerate a concise and informative answer in less than 100 words for the given question\nSEARCH RESULT 1: Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019.\nSEARCH RESULT 2: Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019.\nQUESTION: What's the population increase of New York City from 2021 to 2023? How is the trending comparing with Miami?\nOkay, I need to figure out the population increase of New York City from 2021 to 2023 and compare it with Miami's growth. Let me start by looking at the data provided.
From SEARCH RESULT 2, in 2021, NYC's population was 18,823,000, and in 2022, it was 18,867,000. Then in 2023, it's 18,937,000. So, from 2021 to 2022, it increased by 44,000, and from 2022 to 2023, it went up by 70,000. Adding those together, the total increase from 2021 to 2023 is 114,000.
Now, looking at Miami's data in SEARCH RESULT 1, in 2021, the population was 6,167,000, and in 2023, it's 6,265,000. That's an increase of 98,000 over the same period.
Comparing the two, NYC's increase is higher than Miami's. NYC went up by 114,000, while Miami was 98,000. Also, NYC's growth rate is a bit lower than Miami's. NYC's average annual growth rate is around 0.37%, whereas Miami's is about 0.75%. So, while NYC's population increased more in total, Miami's growth rate is higher. I should present this clearly, highlighting both the total increase and the growth rates to show the comparison accurately.
</think>
From 2021 to 2023, New York City's population increased by 114,000, compared to Miami's increase of 98,000. While NYC's total growth is higher, Miami's annual growth rate (0.75%) is notably faster than NYC's (0.37%)."""
}
}
}