8-9 DeepSeek-R1 モデル

学習目標

Pythonプログラムを使用して、Hugging FaceプラットフォームからDeepSeek-R1モデルをダウンロードし、簡単なプロンプトを使用してDeepSeek-R1に質問し、回答を取得します。

DeepSeek-R1とは??

DeepSeek-R1は、DeepSeek社が開発した大規模言語モデルです。テキストを理解し、記事を作成し、質問に答え、熟考した上で回答を生成する、いわばAIの頭脳のようなものです。

DeepSeek-R1でできること

1. 自動顧客サービス

2. 要約または翻訳

3. プログラミング支援

4. 文書、契約書、報告書の分析

利用開始方法

1. 以下のサンプルコードを実行すると、DeepSeek-R1は「量子もつれの概念を簡潔に説明してください」というプロンプトに対して回答を生成します。

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from transformers import pipeline

model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(
    model_id,
    trust_remote_code=True,  # Trust remote code execution
    cache_dir="./model",  # Specify model cache directory, default is ~/.cache/huggingface
)

# Load model
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    trust_remote_code=True,  # Trust remote code execution
    torch_dtype=torch.float16,  # Specify model data type (float16)
    device_map="auto",  # Automatically select device (CPU or GPU)
    cache_dir="./model",  # Specify model cache directory, default is ~/.cache/huggingface
)

# Create a text generation pipeline
generator = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_length=512,  # Maximum number of new tokens to generate
    temperature=0.6,  # Controls randomness of generation
    top_p=0.95,  # Only consider tokens with cumulative probability up to 0.95
    repetition_penalty=1.1,  # Penalty to reduce repeated content during generation
)

# Generate text and output
prompt = "Please briefly explain the concept of quantum entanglement."
outputs = generator(prompt, num_return_sequences=1)
print(outputs[0]["generated_text"])

2. 実行すると、以下のような応答が表示されます。

参考資料 :

deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B · Hugging Face

 

Copyright © 2026 YUAN High-Tech Development Co., Ltd.
All rights reserved.