

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# DeepSeek モデル
<a name="model-parameters-deepseek"></a>

DeepSeekの R1 および V3.1 モデルは、Invoke API ([InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html)、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)) および Converse API ([Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) および [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html)) を介した推論に使用できるtext-to-textモデルです。

DeepSeek のモデルで推論呼び出しを行う場合は、モデルのプロンプトを含めます。Amazon Bedrock がサポートする DeepSeek モデルのプロンプト作成に関する一般情報については、「[DeepSeek プロンプトガイド](https://api-docs.deepseek.com/guides/reasoning_model.html)」を参照してください。

**注記**  
Amazon Titan、Amazon Nova、DeepSeek-R1、Mistral AI、Meta Llama 3 Instruct、Meta Llama 4 の各モデルからリクエストアクセスを削除することはできません。IAM ポリシーを使用し、モデル ID を指定することで、ユーザーがこれらのモデルを推論呼び出しできないようにすることができます。詳細については、「[基盤モデルの推論のためのアクセスを拒否する](https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-deny-inference                         .html)」を参照してください。
で最適なレスポンス品質を得るにはDeepSeek-R1、 `max_tokens`パラメータを 8,192 トークン以下に制限します。API は最大 32,768 トークンを受け入れますが、応答品質は 8,192 トークンを大幅に低下させます。これは、[推論推論ガイドで説明されているモデルの推論機能と一致します](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-reasoning.html)。

このセクションでは、DeepSeek モデルのリクエストパラメータとレスポンスフィールドについて説明します。この情報を使用して、[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) オペレーションで DeepSeek モデルに推論呼び出しを実行します。このセクションでは、DeepSeek モデルを呼び出す方法を示す Python コード例も含まれています。

推論オペレーションでモデルを使用するには、そのモデルのモデル ID が必要です。このモデルはクロスリージョン推論によって呼び出されるため、[推論プロファイル ID](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-support.html) をモデル ID として使用する必要があります。例えば、米国の場合は `us.deepseek.r1-v1:0` を使用します。
+ モデル名: DeepSeek-R1
+ テキストモデル

API で DeepSeek モデルを使用する方法の詳細については、[「DeepSeek モデル](https://deepseek.com/)」を参照してください。

**DeepSeek のリクエストとレスポンス**

**リクエストボディ**

DeepSeek には、Text Completion 推論呼び出しの次の推論パラメータがあります。

```
{
    "prompt": string,
    "temperature": float, 
    "top_p": float,
    "max_tokens": int,
    "stop": string array
}
```

**[フィールド]**
+ **prompt** – (文字列) プロンプトの必須テキスト入力。
+ **temperature** – (浮動小数点) 1 以下の数値。
+ **top\_p** – (浮動小数点) 1 以下の数値。
+ **max\_tokens** – (int) 使用するトークン。最適な品質を得るために最低 1～最大 8,192 トークン。API は最大 32,768 トークンを受け入れますが、応答品質は 8,192 トークンを大幅に低下させます。
+ **stop** – (文字列配列) 最大 10 個の項目。

**レスポンス本文**

DeepSeek には、Text Completion 推論呼び出しの次のレスポンスパラメータがあります。この例は、DeepSeek からの Text Completion であり、コンテンツ推論ブロックを返しません。

```
{
    "choices": [
        {
            "text": string,
            "stop_reason": string
        }
    ]
}
```

**[フィールド]**
+ **stop\_reason** – (文字列) モデルがテキストの生成を停止した理由。`stop` または `length` 値。
+ **stop** - (文字列) モデルは入力プロンプトのテキストの生成を終了しました。
+ **length** - (文字列) 生成されたテキストにおけるトークンの長さが `InvokeModel` (または、出力をストリーミングする場合は `InvokeModelWithResponseStream`) の呼び出しにおける `max_tokens` の値を超えています。レスポンスは `max_tokens` の長さに切り捨てられます。`max_tokens` 値を大きくし、リクエストを再試行してください。

**コード例**

この例は、DeepSeek-R1 モデルを呼び出す方法を示しています。

```
# Use the API to send a text message to DeepSeek-R1.

import boto3
import json

from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS リージョン of your choice.
client = boto3.client("bedrock-runtime", region_name="us-west-2")

# Set the cross Region inference profile ID for DeepSeek-R1
model_id = "us.deepseek.r1-v1:0"

# Define the prompt for the model.
prompt = "Describe the purpose of a 'hello world' program in one line."

# Embed the prompt in DeepSeek-R1's instruction format.
formatted_prompt = f"""
<｜begin▁of▁sentence｜><｜User｜>{prompt}<｜Assistant｜><think>\n
"""

body = json.dumps({
    "prompt": formatted_prompt,
    "max_tokens": 512,
    "temperature": 0.5,
    "top_p": 0.9,
})

try:
    # Invoke the model with the request.
    response = client.invoke_model(modelId=model_id, body=body)

    # Read the response body.
    model_response = json.loads(response["body"].read())
    
    # Extract choices.
    choices = model_response["choices"]
    
    # Print choices.
    for index, choice in enumerate(choices):
        print(f"Choice {index + 1}\n----------")
        print(f"Text:\n{choice['text']}\n")
        print(f"Stop reason: {choice['stop_reason']}\n")
except (ClientError, Exception) as e:
    print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
    exit(1)
```

**Converse**

リクエスト本文 - このリクエスト本文の例を使用して ConverseAPI を呼び出します。

```
{
    "modelId": string, # us.deepseek.r1-v1:0
    "system": [
        {
            "text": string
        }
    ],
    "messages": [
        {
            "role": string,
            "content": [
                {
                    "text": string
                }
            ]
        }
    ],
    "inferenceConfig": {
        "temperature": float,
        "topP": float,
        "maxTokens": int,
        "stopSequences": string array
    },
    "guardrailConfig": { 
        "guardrailIdentifier":"string",
        "guardrailVersion": "string",
        "trace": "string"
    }
}
```

**[フィールド]**
+ **system** – (オプション) リクエストのシステムプロンプト。
+ **messages** – (必須) 入力メッセージ。
  + **role** – 会話ターンのロール。有効な値は、`user` および `assistant` です。
  + **content** – (必須) 会話ターンの内容 (オブジェクトの配列)。各オブジェクトには typefield が含まれており、次のいずれかの値を指定できます。
    + **text** – (必須) このタイプを指定する場合は、テキストフィールドを追加し、テキストプロンプトを値として指定する必要があります。
+ **inferenceConfig** 
  + **temperature** – (オプション) 値: 最小 = 0、最大 = 1。
  + **topP** – (オプション) 値: 最小 = 0、最大 = 1。
  + **maxTokens** – (オプション) 停止する前に生成するトークンの最大数。値: 最小 = 0、最大 = 32,768。
  + **stopSequences** – (オプション) モデルによる出力の生成を停止するカスタムテキストシーケンス。最大 = 10 個の項目。

レスポンス本文 - このリクエスト本文の例を使用して ConverseAPI を呼び出します。

```
{
    "message": {
        "role" : "assistant",
        "content": [
            {
                "text": string
            },
            {
                "reasoningContent": {
                    "reasoningText": string
                }
            }
        ],
    },
    "stopReason": string,
    "usage": {
        "inputTokens": int,
        "outputTokens": int,
        "totalTokens": int
    }
    "metrics": {
        "latencyMs": int
    }
}
```

**[フィールド]**
+ **message** – モデルから返されたレスポンス。
+ **role** – 生成されたメッセージの会話ロール。値は常に `assistant` です。
+ **content** – モデルによって生成されたコンテンツ。配列として返されます。コンテンツは次の 2 種類です。
  + **text** – レスポンスのテキストコンテンツ。
  + **reasoningContent** – (オプション) モデルレスポンスの推論コンテンツ。
    + **reasoningText** – モデルレスポンスからの推論テキスト。
+ **stopReason** – モデルがレスポンスの生成を停止した理由。
  + **end\_turn** – モデルが停止点に達したターン。
  + **max\_tokens** – 生成されたテキストが `maxTokens` 入力フィールドの値を超えたか、モデルがサポートするトークンの最大数を超えました。

コード例 - DeepSeek が ConverseAPI を呼び出す例を次に示します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the Converse API with DeepSeek-R1 (on demand).
"""

import logging
import boto3

from botocore.client import Config
from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          system_prompts,
                          messages):
    """
    Sends messages to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        system_prompts (JSON) : The system prompts for the model to use.
        messages (JSON) : The messages to send to the model.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Inference parameters to use.
    temperature = 0.5
    max_tokens = 4096

    # Base inference parameters to use.
    inference_config = {
        "temperature": temperature,
        "maxTokens": max_tokens,
    }

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages,
        system=system_prompts,
        inferenceConfig=inference_config,
    )

    # Log token usage.
    token_usage = response['usage']
    logger.info("Input tokens: %s", token_usage['inputTokens'])
    logger.info("Output tokens: %s", token_usage['outputTokens'])
    logger.info("Total tokens: %s", token_usage['totalTokens'])
    logger.info("Stop reason: %s", response['stopReason'])

    return response

def main():
    """
    Entrypoint for DeepSeek-R1 example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "us.deepseek.r1-v1:0"

    # Setup the system prompts and messages to send to the model.
    system_prompts = [{"text": "You are an app that creates playlists for a radio station that plays rock and pop music. Only return song names and the artist."}]
    message_1 = {
        "role": "user",
        "content": [{"text": "Create a list of 3 pop songs."}]
    }
    message_2 = {
        "role": "user",
        "content": [{"text": "Make sure the songs are by artists from the United Kingdom."}]
    }
    messages = []

    try:
        # Configure timeout for long responses if needed
        custom_config = Config(connect_timeout=840, read_timeout=840)
        bedrock_client = boto3.client(service_name='bedrock-runtime', config=custom_config)

        # Start the conversation with the 1st message.
        messages.append(message_1)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        # Add the response message to the conversation.
        output_message = response['output']['message']
        
        # Remove reasoning content from the response
        output_contents = []
        for content in output_message["content"]:
            if content.get("reasoningContent"):
                continue
            else:
                output_contents.append(content)
        output_message["content"] = output_contents
        
        messages.append(output_message)

        # Continue the conversation with the 2nd message.
        messages.append(message_2)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        output_message = response['output']['message']
        messages.append(output_message)

        # Show the complete conversation.
        for message in messages:
            print(f"Role: {message['role']}")
            for content in message['content']:
                if content.get("text"):
                    print(f"Text: {content['text']}")
                if content.get("reasoningContent"):
                    reasoning_content = content['reasoningContent']
                    reasoning_text = reasoning_content.get('reasoningText', {})
                    print()
                    print(f"Reasoning Text: {reasoning_text.get('text')}")
            print()

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```