使用 HuggingFace + LangChain 本地调用 DeepSeek

前言

最近 LLM 也是非常火,我也一直在学习相关知识。记录一下在本地通过 HuggingFace Transformers 和 LangChain 调用 DeepSeek 模型。

一、环境准备

  1. 安装 Python 3.8+
  2. 注册 HuggingFace 账号,获取 API Token,并在终端登录:
    1
    huggingface-cli login
  3. 建议使用虚拟环境:
    1
    2
    python3 -m venv venv
    source venv/bin/activate
  4. 安装依赖包:
    1
    pip install torch transformers langchain
    • Apple Silicon 用户可用:
      1
      pip3 install torch --index-url https://download.pytorch.org/whl/metal.html
  5. 测试 PyTorch 是否可用:
    1
    2
    3
    import torch
    x = torch.rand(5, 3)
    print(x)

二、下载 DeepSeek 模型

  • 推荐模型:deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
  • 也可选择其他 DeepSeek 模型,如 deepseek-ai/deepseek-llm-7b-base
  • 可直接用 Transformers 自动下载,或提前手动下载模型到本地

三、Python 本地调用 DeepSeek 简单示例

1
2
3
4
5
6
7
from transformers import pipeline
pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
messages = [
{"role": "user", "content": "什么是deepseek?"},
]
msg = pipe(messages)
print(msg)

若模型已下载到本地,可将 model 参数替换为本地路径。

四、结合 LangChain 调用 DeepSeek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from transformers import pipeline
from langchain_huggingface import HuggingFacePipeline
from langchain.prompts import PromptTemplate
import torch

if torch.backends.mps.is_available():
print("Using Apple Silicon GPU (MPS) for inference.")
else:
print("Using CPU for inference.")

model = pipeline(
"text-generation",
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
device="mps", # Apple Silicon 可用
)
lc_pipe = HuggingFacePipeline(pipeline=model)
prompt = PromptTemplate.from_template("请用一句话介绍DeepSeek。")
result = lc_pipe(prompt.format())
print(result)

五、使用 LangChain 进行更复杂的任务

可以将多个模型串联,实现如“先摘要再问答”等复杂链式推理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from transformers import pipeline
from langchain_huggingface import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from transformers.utils.logging import set_verbosity_error
set_verbosity_error()

summarization_pipeline = pipeline(
"summarization", model="sshleifer/distilbart-cnn-12-6", device="mps")
summarizer = HuggingFacePipeline(pipeline=summarization_pipeline)

# 先摘要
summary = summarizer("DeepSeek 是什么?请简要介绍。")
print("摘要:", summary)

# 再用 DeepSeek 问答
qa_pipeline = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", device="mps")
qa = HuggingFacePipeline(pipeline=qa_pipeline)
question = PromptTemplate.from_template("DeepSeek 有哪些应用场景?")
answer = qa(question.format())
print("问答:", answer)

六、注意事项

  • Apple Silicon 用户建议用 Metal 加速(device=”mps”),性能更佳。
  • 模型较大,需保证磁盘空间和内存充足。
  • 如遇 HuggingFace 下载慢,可配置清华 PyPI 镜像。
  • 复杂链式推理建议选用轻量模型,避免本地资源瓶颈。

七、结语

本次测试在 MacBook Pro M1 16GB 内存上运行良好。后续可研究 RAG、Agent 等更高级用法,并尝试部署 API 服务。