Link Search Menu Expand Document Documentation Menu

使用 DeepSeek Chat API 的 RAG

本教程演示如何使用 Amazon OpenSearch ServiceDeepSeek 聊天模型 实现检索增强生成 (RAG)。

如果您使用自管 OpenSearch 而不是 Amazon OpenSearch Service,请获取 DeepSeek API 密钥,并使用此蓝图创建 DeepSeek 聊天模型的连接器。有关创建连接器的更多信息,请参阅连接器。然后直接转到步骤 5

将以 your_ 为前缀的占位符替换为您自己的值。

先决条件

开始之前,请满足以下先决条件。

配置 Amazon 设置时,只更改本教程中提到的值。将所有其他设置保留为默认值。

获取 DeepSeek API 密钥

如果您还没有 DeepSeek API 密钥,请在本教程开始前获取一个。

创建 OpenSearch 集群

前往 Amazon OpenSearch Service 控制台并创建一个 OpenSearch 域。

请注意域名 Amazon Resource Name (ARN) 和 URL;您将在后续步骤中使用它们。

步骤 1:将 API 密钥存储在 AWS Secrets Manager 中

将您的 DeepSeek API 密钥存储在 AWS Secrets Manager

  1. 打开 AWS Secrets Manager。
  2. 选择 Store a new secret(存储新密钥)。
  3. 选择 Other type of secret(其他类型的密钥)。
  4. 创建一个键值对,其中键为 my_deepseek_key,值为您的 DeepSeek API 密钥。
  5. 将您的密钥命名为 my_test_deepseek_secret

记下密钥 ARN;您将在后续步骤中使用它。

步骤 2:创建 IAM 角色

要使用在步骤 1 中创建的密钥,您必须创建一个具有该密钥读取权限的 AWS 身份和访问管理 (IAM) 角色。此 IAM 角色将在连接器中配置,并允许连接器读取密钥。

转到 IAM 控制台,创建一个名为 my_deepseek_secret_role 的新 IAM 角色,并添加以下信任策略和权限

  • 自定义信任策略
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "es.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

  • 权限
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "secretsmanager:GetSecretValue",
        "secretsmanager:DescribeSecret"
      ],
      "Effect": "Allow",
      "Resource": "your_secret_arn_created_in_step1"
    }
  ]
}

记下角色 ARN;您将在后续步骤中使用它。

步骤 3:在 Amazon OpenSearch Service 中配置 IAM 角色

按照以下步骤在 Amazon OpenSearch Service 中配置 IAM 角色。

步骤 3.1:为签署连接器请求创建 IAM 角色

专门为签署您的创建连接器 API 请求生成一个新的 IAM 角色。

创建一个名为 my_create_deepseek_connector_role 的 IAM 角色,具有以下信任策略和权限

  • 自定义信任策略
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "your_iam_user_arn"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

您将使用 your_iam_user_arn IAM 用户在步骤 4.1 中承担该角色。

  • 权限
{
  "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"
    }
  ]
}

记下此角色 ARN;您将在后续步骤中使用它。

步骤 3.2:映射后端角色

按照以下步骤映射后端角色

  1. 登录 OpenSearch Dashboards,并在顶部菜单中选择 Security(安全)。
  2. 选择 Roles(角色),然后选择 ml_full_access 角色。
  3. ml_full_access 角色详情页面,选择 Mapped users(已映射用户),然后选择 Manage mapping(管理映射)。
  4. Backend roles(后端角色)字段中输入在步骤 3.1 中创建的 IAM 角色 ARN,如下图所示。映射后端角色
  5. 选择 Map(映射)。

IAM 角色现已成功在您的 OpenSearch 集群中配置。

步骤 4:创建连接器

按照以下步骤为 DeepSeek 聊天模型创建连接器。有关创建连接器的更多信息,请参阅连接器

步骤 4.1:获取临时凭证

使用步骤 3.1 中指定的 IAM 用户的凭证来承担角色

aws sts assume-role --role-arn your_iam_role_arn_created_in_step3.1 --role-session-name your_session_name

从响应中复制临时凭证,并将其配置在 ~/.aws/credentials

[default]
AWS_ACCESS_KEY_ID=your_access_key_of_role_created_in_step3.1
AWS_SECRET_ACCESS_KEY=your_secret_key_of_role_created_in_step3.1
AWS_SESSION_TOKEN=your_session_token_of_role_created_in_step3.1

步骤 4.2:创建连接器

将 DeepSeek API 端点添加到受信任的 URL 列表

PUT /_cluster/settings
{
  "persistent": {
    "plugins.ml_commons.trusted_connector_endpoints_regex": [
      """^https://api\.deepseek\.com/.*$"""
    ]
  }
}

使用在 ~/.aws/credentials 中配置的临时凭证运行以下 Python 代码

import boto3
import requests 
from requests_aws4auth import AWS4Auth

host = 'your_amazon_opensearch_domain_endpoint'
region = 'your_amazon_opensearch_domain_region'
service = 'es'

credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

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)

脚本将输出连接器 ID

{"connector_id":"duRJsZQBFSAM-WcznrIw"}

记下连接器 ID;您将在下一步中使用它。

步骤 5:创建和测试模型

登录 OpenSearch Dashboards,打开 DevTools 控制台,并运行以下请求来创建和测试 DeepSeek 聊天模型。

  1. 创建模型组

     POST /_plugins/_ml/model_groups/_register
     {
       "name": "DeepSeek Chat model",
       "description": "Test model group for DeepSeek model"
     }
    

    响应包含模型组 ID

     {
       "model_group_id": "UylKsZQBts7fa6byEx2M",
       "status": "CREATED"
     }
    
  2. 注册模型

     POST /_plugins/_ml/models/_register
     {
       "name": "DeepSeek Chat model",
       "function_name": "remote",
       "description": "DeepSeek Chat model",
       "model_group_id": "UylKsZQBts7fa6byEx2M",
       "connector_id": "duRJsZQBFSAM-WcznrIw"
     }
    

    响应包含模型 ID

     {
       "task_id": "VClKsZQBts7fa6bypR0a",
       "status": "CREATED",
       "model_id": "VSlKsZQBts7fa6bypR02"
     }
    
  3. 部署模型

     POST /_plugins/_ml/models/VSlKsZQBts7fa6bypR02/_deploy
    

    响应包含部署操作的任务 ID

     {
       "task_id": "d-RKsZQBFSAM-Wcz3bKO",
       "task_type": "DEPLOY_MODEL",
       "status": "COMPLETED"
     }
    
  4. 测试模型

     POST /_plugins/_ml/models/VSlKsZQBts7fa6bypR02/_predict
     {
       "parameters": {
         "messages": [
           {
             "role": "system",
             "content": "You are a helpful assistant."
           },
           {
             "role": "user",
             "content": "Hello!"
           }
         ]
       }
     }
    

    响应包含模型生成的文本

     {
       "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"
      }
    }
  ]
}

步骤 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."}

步骤 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
    }
  }
}

响应包括从向量搜索中检索到的相关文档(在 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."
    }
  }
}