如何在本地部署(Ollama和Google Colab)和執行DeepSeek模型?

本地部署(Ollama和Google Colab)和執行DeepSeek模型

DeepSeek 在人工智慧界掀起了一場風暴,截至目前,Hugging Face 上已有 68 個模型可用。這一系列開源模型可通過 Hugging Face 或 Ollama 訪問,而 DeepSeek-R1 DeepSeek-V3 則可通過 DeepSeek Chat 直接用於推理。在本部落格中,我們將探討 DeepSeek 的模型陣容,並指導您使用 Google Colab 和 Ollama 執行這些模型。

DeepSeek模型概述

DeepSeek 提供多種型號,每種型號都針對不同任務進行了優化。以下是哪種模型最適合您的需求的詳細介紹:

  • 針對開發人員和程式設計師:DeepSeek-Coder和DeepSeek-Coder-V2型號專為編寫和除錯程式碼等編碼任務而設計。
  • 面向普通使用者:DeepSeek-V3 模型 是一個多功能選項,能夠處理從休閒對話到複雜內容生成等各種查詢。
  • 面向研究人員和高階使用者:DeepSeek-R1 模型擅長高階推理和邏輯分析,是解決問題和研究應用的理想選擇。
  • 適用於視覺任務:DeepSeek-Janus 系列和 DeepSeek-VL 模型專為多模態任務定製,包括影象生成和處理。

在Ollama上執行DeepSeek R1

Step 1:安裝Ollama

要在本地計算機上執行 DeepSeek 模型,需要安裝 Ollama:

  • 下載 Ollama:點選此處下載
  • 對於 Linux 使用者:在終端執行以下命令:bashCopyEdit
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl -fsSL https://ollama.com/install.sh | sh
curl -fsSL https://ollama.com/install.sh | sh
curl -fsSL https://ollama.com/install.sh | sh

Step 2:調出DeepSeek R1模型

安裝好Ollama後,開啟命令列介面(CLI),提取模型:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ollama pull deepseek-r1:1.5b
ollama pull deepseek-r1:1.5b
ollama pull deepseek-r1:1.5b

您可以在這裡探索 Ollama 上的其他 DeepSeek 模型:Ollama 模型搜尋

這一步可能需要一些時間,請等待下載完成。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ollama pull deepseek-r1:1.5b
pulling manifest
pulling aabd4debf0c8... 100% ▕████████████████▏ 1.1 GB
pulling 369ca498f347... 100% ▕████████████████▏ 387 B
pulling 6e4c38e1172f... 100% ▕████████████████▏ 1.1 KB
pulling f4d24e9138dd... 100% ▕████████████████▏ 148 B
pulling a85fe2a2e58e... 100% ▕████████████████▏ 487 B
verifying sha256 digest
writing manifest
success
ollama pull deepseek-r1:1.5b pulling manifest pulling aabd4debf0c8... 100% ▕████████████████▏ 1.1 GB pulling 369ca498f347... 100% ▕████████████████▏ 387 B pulling 6e4c38e1172f... 100% ▕████████████████▏ 1.1 KB pulling f4d24e9138dd... 100% ▕████████████████▏ 148 B pulling a85fe2a2e58e... 100% ▕████████████████▏ 487 B verifying sha256 digest writing manifest success
ollama pull deepseek-r1:1.5b
pulling manifest 
pulling aabd4debf0c8... 100% ▕████████████████▏ 1.1 GB                         
pulling 369ca498f347... 100% ▕████████████████▏  387 B                         
pulling 6e4c38e1172f... 100% ▕████████████████▏ 1.1 KB                         
pulling f4d24e9138dd... 100% ▕████████████████▏  148 B                         
pulling a85fe2a2e58e... 100% ▕████████████████▏  487 B                         
verifying sha256 digest 
writing manifest 
success

Step 3:本地執行模型

下載模型後,您可以使用以下命令執行它:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ollama run deepseek-r1:1.5b
ollama run deepseek-r1:1.5b
ollama run deepseek-r1:1.5b

本地執行模型

現在,該模型可以在本地計算機上使用,並順利回答了我的問題。

在Google Colab上執行DeepSeek-Janus-Pro-1B

在本節中,我們將使用 Google Colab 試用 DeepSeek-Janus-Pro-1B。開始之前,請確保將執行時間設定為 T4 GPU,以獲得最佳效能。

Step 1:克隆DeepSeek-Janus倉庫

在 Colab 筆記本中執行以

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
!git clone https://github.com/deepseek-ai/Janus.git
!git clone https://github.com/deepseek-ai/Janus.git
!git clone https://github.com/deepseek-ai/Janus.git

在 GitHub 上探索更多 DeepSeek 模型:DeepSeek AI GitHub 倉庫

Step 2:安裝依賴包

導航至克隆目錄,安裝所需的軟體包:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
%cd Janus
!pip install -e .
!pip install flash-attn
%cd Janus !pip install -e . !pip install flash-attn
%cd Janus
!pip install -e .
!pip install flash-attn

Step 3:載入模型至GPU

現在,我們將匯入必要的庫,並將模型載入到 CUDA(GPU)上:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import torch
from transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from janus.utils.io import load_pil_images
# Define model path
model_path = "deepseek-ai/Janus-Pro-1B"
# Load processor and tokenizer
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
tokenizer = vl_chat_processor.tokenizer
# Load model with remote code enabled
vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
# Move model to GPU
vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
import torch from transformers import AutoModelForCausalLM from janus.models import MultiModalityCausalLM, VLChatProcessor from janus.utils.io import load_pil_images # Define model path model_path = "deepseek-ai/Janus-Pro-1B" # Load processor and tokenizer vl_chat_processor = VLChatProcessor.from_pretrained(model_path) tokenizer = vl_chat_processor.tokenizer # Load model with remote code enabled vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True) # Move model to GPU vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
import torch
from transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from janus.utils.io import load_pil_images
# Define model path
model_path = "deepseek-ai/Janus-Pro-1B"
# Load processor and tokenizer
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
tokenizer = vl_chat_processor.tokenizer
# Load model with remote code enabled
vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
# Move model to GPU
vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()

Step 4:傳遞影象進行處理

現在,讓我們向模型輸入影象並生成響應。

📷 Input Image

如何訪問Deepseek圖片

初始化提示和系統角色

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
image_path = "/content/snapshot.png"
question = "What's in the image?"
conversation = [
{"role": "<|User|>", "content": f"<image_placeholder>\n{question}", "images": [image_path]},
{"role": "<|Assistant|>", "content": ""}
]
image_path = "/content/snapshot.png" question = "What's in the image?" conversation = [ {"role": "<|User|>", "content": f"<image_placeholder>\n{question}", "images": [image_path]}, {"role": "<|Assistant|>", "content": ""} ]
image_path = "/content/snapshot.png"
question = "What's in the image?"
conversation = [
{"role": "<|User|>", "content": f"<image_placeholder>\n{question}", "images": [image_path]},
{"role": "<|Assistant|>", "content": ""}
]

處理輸入資料

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Load image
pil_images = load_pil_images(conversation)
# Prepare inputs for the model
prepare_inputs = vl_chat_processor(conversations=conversation, images=pil_images, force_batchify=True).to(vl_gpt.device)
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
# Generate response
outputs = vl_gpt.language_model.generate(
inputs_embeds=inputs_embeds,
attention_mask=prepare_inputs.attention_mask,
pad_token_id=tokenizer.eos_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
max_new_tokens=512,
do_sample=False,
use_cache=True,
)
# Decode and print response
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
print(f"{prepare_inputs['sft_format'][0]}", answer)
# Load image pil_images = load_pil_images(conversation) # Prepare inputs for the model prepare_inputs = vl_chat_processor(conversations=conversation, images=pil_images, force_batchify=True).to(vl_gpt.device) inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs) # Generate response outputs = vl_gpt.language_model.generate( inputs_embeds=inputs_embeds, attention_mask=prepare_inputs.attention_mask, pad_token_id=tokenizer.eos_token_id, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, max_new_tokens=512, do_sample=False, use_cache=True, ) # Decode and print response answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True) print(f"{prepare_inputs['sft_format'][0]}", answer)
# Load image
pil_images = load_pil_images(conversation)
# Prepare inputs for the model
prepare_inputs = vl_chat_processor(conversations=conversation, images=pil_images, force_batchify=True).to(vl_gpt.device)
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
# Generate response
outputs = vl_gpt.language_model.generate(
inputs_embeds=inputs_embeds,
attention_mask=prepare_inputs.attention_mask,
pad_token_id=tokenizer.eos_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
max_new_tokens=512,
do_sample=False,
use_cache=True,
)
# Decode and print response
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
print(f"{prepare_inputs['sft_format'][0]}", answer)

輸出:

<|User|>:What’s in the image?

<|Assistant|>: The image features a section titled “Latest Articles” with a focus on a blog post. The blog post discusses “How to Access DeepSeek Janus Pro 7B?” and highlights its multimodal AI capabilities in reasoning, text-to-image, and instruction-following. The image also includes the DeepSeek logo (a dolphin) and a hexagonal pattern in the background.

我們可以看到,該模型能夠讀取影象中的文字,還能發現影象中的 DeepSeek Logo。初步印象,它的表現不錯。

小結

DeepSeek 正在迅速崛起,成為人工智慧領域一股強大的力量,為開發人員、研究人員和普通使用者提供各種模型。在與 OpenAI 和 Gemini 等行業巨頭的競爭中,DeepSeek 高價效比的高效能模型很可能會得到廣泛應用。

DeepSeek 模型的應用是無限的,從編碼輔助到高階推理和多模態功能,不一而足。通過Ollama無縫本地執行和基於雲的推理選項,DeepSeek有望成為人工智慧研發領域的遊戲規則改變者。

如果您有任何疑問或遇到問題,請隨時在評論區提問!

評論留言