如何在本地部署(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有望成为人工智能研发领域的游戏规则改变者。

如果您有任何疑问或遇到问题,请随时在评论区提问!

评论留言