RAG using the DeepSeek Chat API
출처: 문서
본문
DeepSeek Chat API를 사용한 RAG
이 튜토리얼에서는 Amazon OpenSearch Service와 DeepSeek 챗 모델을 사용해 검색 증강 생성(RAG)을 구현하는 방법을 알아봐요.
Amazon OpenSearch Service 대신 자체 관리형 OpenSearch를 사용한다면, DeepSeek API 키를 발급받고 블루프린트를 사용해 DeepSeek 챗 모델 커넥터를 생성하세요. 커넥터 생성에 대한 자세한 내용은 커넥터 문서를 참고하세요. 그런 다음 바로 5단계로 넘어가면 돼요.
your_ 접두사로 시작하는 플레이스홀더는 여러분의 값으로 교체하세요.
{: .note}
사전 준비 사항
시작하기 전에 다음 사전 준비 사항을 충족해야 해요.
Amazon 설정을 구성할 때는 이 튜토리얼에서 언급한 값만 변경하고, 다른 모든 설정은 기본값 그대로 유지하세요. {: .important}
DeepSeek API 키 발급
아직 DeepSeek API 키가 없다면, 이 튜토리얼을 시작하기 전에 먼저 발급받으세요.
OpenSearch 클러스터 생성
Amazon OpenSearch Service 콘솔로 이동해 OpenSearch 도메인을 생성하세요.
도메인의 Amazon Resource Name(ARN)과 URL을 메모해 두세요. 이후 단계에서 사용하게 돼요.
1단계: AWS Secrets Manager에 API 키 저장
DeepSeek API 키를 AWS Secrets Manager에 저장하세요:
- AWS Secrets Manager를 여세요.
- 새 시크릿 저장을 선택하세요.
- 기타 유형의 시크릿을 선택하세요.
- my_deepseek_key를 키로, 여러분의 DeepSeek API 키를 값으로 하는 키-값 쌍을 만드세요.
- 시크릿 이름을
my_test_deepseek_secret으로 지정하세요.
시크릿 ARN을 메모해 두세요. 이후 단계에서 사용하게 돼요.
2단계: IAM 역할 생성
1단계에서 만든 시크릿을 사용하려면, 해당 시크릿에 대한 읽기 권한을 가진 AWS Identity and Access Management(IAM) 역할을 만들어야 해요. 이 IAM 역할은 커넥터에 설정되며, 커넥터가 시크릿을 읽을 수 있게 해 줘요.
IAM 콘솔로 이동해 my_deepseek_secret_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": [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret"
],
"Effect": "Allow",
"Resource": "your_secret_arn_created_in_step1"
}
]
}
{% include copy.html %}
역할 ARN을 메모해 두세요. 이후 단계에서 사용하게 돼요.
3단계: Amazon OpenSearch Service에서 IAM 역할 구성
다음 단계를 따라 Amazon OpenSearch Service에서 IAM 역할을 구성하세요.
3.1단계: 커넥터 요청 서명용 IAM 역할 생성
Create Connector API 요청에 서명하기 위한 새 IAM 역할을 만드세요.
my_create_deepseek_connector_role이라는 이름의 IAM 역할을 다음 신뢰 정책과 권한으로 만드세요:
- 사용자 지정 신뢰 정책:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "your_iam_user_arn"
},
"Action": "sts:AssumeRole"
}
]
}
{% include copy.html %}
4단계에서 이 역할을 수임할 때는 your_iam_user_arn IAM 사용자를 사용하게 돼요.
- 권한:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "your_iam_role_arn_created_in_step2"
},
{
"Effect": "Allow",
"Action": "es:ESHttpPost",
"Resource": "your_opensearch_domain_arn"
}
]
}
{% include copy.html %}
이 역할 ARN을 메모해 두세요. 이후 단계에서 사용하게 돼요.
3.2단계: 백엔드 역할 매핑
다음 단계를 따라 백엔드 역할을 매핑하세요:
- OpenSearch Dashboards에 로그인한 뒤 상단 메뉴에서 보안을 선택하세요.
- 역할을 선택하고, 이어서 ml_full_access 역할을 선택하세요.
- ml_full_access 역할 상세 페이지에서 매핑된 사용자를 선택한 다음 매핑 관리를 선택하세요.
- 다음 이미지와 같이 3.1단계에서 만든 IAM 역할 ARN을 백엔드 역할 필드에 입력하세요.

- 매핑을 선택하세요.
이제 IAM 역할이 OpenSearch 클러스터에 성공적으로 구성됐어요.
4단계: 커넥터 생성
다음 단계를 따라 DeepSeek 챗 모델용 커넥터를 만드세요. 커넥터 생성에 대한 자세한 내용은 커넥터 문서를 참고하세요.
- 신뢰할 수 있는 URL 목록에 DeepSeek API 엔드포인트를 추가하세요:
PUT /_cluster/settings
{
"persistent": {
"plugins.ml_commons.trusted_connector_endpoints_regex": [
"""^https://api\.deepseek\.com/.*$"""
]
}
}
{% include copy-curl.html %}
- 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_step3.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 Chat",
"description": "Test connector for DeepSeek Chat",
"version": "1",
"protocol": "http",
"parameters": {
"endpoint": "api.deepseek.com",
"model": "deepseek-chat"
},
"credential": {
"secretArn": "your_secret_arn_created_in_step1",
"roleArn": "your_iam_role_arn_created_in_step2"
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://${parameters.endpoint}/v1/chat/completions",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer ${credential.secretArn.my_deepseek_key}"
},
"request_body": "{ \"model\": \"${parameters.model}\", \"messages\": ${parameters.messages} }"
}
]
}
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":"duRJsZQBFSAM-WcznrIw"}
커넥터 ID를 메모해 두세요. 다음 단계에서 사용하게 돼요.
5단계: 모델 생성 및 테스트
OpenSearch Dashboards에 로그인하고 DevTools 콘솔을 연 뒤, 다음 요청을 실행해 DeepSeek 챗 모델을 생성하고 테스트하세요.
-
모델 그룹을 생성하세요:
POST /_plugins/_ml/model_groups/_register { "name": "DeepSeek Chat model", "description": "Test model group for DeepSeek model" }{% include copy-curl.html %}
응답에는 모델 그룹 ID가 포함돼요:
{ "model_group_id": "UylKsZQBts7fa6byEx2M", "status": "CREATED" } -
모델을 등록하세요:
POST /_plugins/_ml/models/_register { "name": "DeepSeek Chat model", "function_name": "remote", "description": "DeepSeek Chat model", "model_group_id": "UylKsZQBts7fa6byEx2M", "connector_id": "duRJsZQBFSAM-WcznrIw" }{% include copy-curl.html %}
응답에는 모델 ID가 포함돼요:
{ "task_id": "VClKsZQBts7fa6bypR0a", "status": "CREATED", "model_id": "VSlKsZQBts7fa6bypR02" } -
모델을 배포하세요:
POST /_plugins/_ml/models/VSlKsZQBts7fa6bypR02/_deploy{% include copy-curl.html %}
응답에는 배포 작업에 대한 태스크 ID가 포함돼요:
{ "task_id": "d-RKsZQBFSAM-Wcz3bKO", "task_type": "DEPLOY_MODEL", "status": "COMPLETED" } -
모델을 테스트하세요:
POST /_plugins/_ml/models/VSlKsZQBts7fa6bypR02/_predict { "parameters": { "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] } }{% include copy-curl.html %}
응답에는 모델이 생성한 텍스트가 포함돼요:
{ "inference_results": [ { "output": [ { "name": "response", "dataAsMap": { "id": "a351252c-7393-4c5d-9abe-1c47693ad336", "object": "chat.completion", "created": 1738141298, "model": "deepseek-chat", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! How can I assist you today? 😊" }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 11, "completion_tokens": 11, "total_tokens": 22, "prompt_tokens_details": { "cached_tokens": 0 }, "prompt_cache_hit_tokens": 0, "prompt_cache_miss_tokens": 11 }, "system_fingerprint": "fp_3a5770e1b4" } } ], "status_code": 200 } ] }
6단계: RAG 구성
다음 단계를 따라 RAG를 구성하세요.
6.1단계: 검색 파이프라인 생성
RAG 프로세서가 포함된 검색 파이프라인을 생성하세요:
PUT /_search/pipeline/my-conversation-search-pipeline-deepseek-chat
{
"response_processors": [
{
"retrieval_augmented_generation": {
"tag": "Demo pipeline",
"description": "Demo pipeline Using DeepSeek Chat",
"model_id": "VSlKsZQBts7fa6bypR02",
"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 %}
6.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 %}
6.3단계: 인덱스 검색
벡터 검색을 실행해 벡터 데이터베이스에서 문서를 가져오고, DeepSeek 모델을 사용해 RAG를 수행하세요:
GET /my-nlp-index/_search?search_pipeline=my-conversation-search-pipeline-deepseek-chat
{
"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": "USkHsZQBts7fa6bybx3G",
"k": 5
}
}
},
"size": 4,
"_source": [
"text"
],
"ext": {
"generative_qa_parameters": {
"llm_model": "deepseek-chat",
"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.05248103,
"hits": [
{
"_index": "my-nlp-index",
"_id": "2",
"_score": 0.05248103,
"_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."""
}
},
{
"_index": "my-nlp-index",
"_id": "4",
"_score": 0.029023321,
"_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": "3",
"_score": 0.028097045,
"_source": {
"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": "my-nlp-index",
"_id": "6",
"_score": 0.026973149,
"_source": {
"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."""
}
}
]
},
"ext": {
"retrieval_augmented_generation": {
"answer": "From 2021 to 2023, New York City's metro area population increased by 114,000, from 18,823,000 to 18,937,000, reflecting a growth rate of 0.61%. In comparison, Miami's metro area population grew by 98,000, from 6,167,000 to 6,265,000, with a higher growth rate of 1.59%. While New York City has a larger absolute population increase, Miami's population growth rate is significantly higher, indicating faster relative growth."
}
}
}