本地部署 DeepSeek R1 模型并集成到 Word 中
背景与优势
在之前的实践中,我们曾通过 API Key 的方式将 DeepSeek 接入 Word,显著提升了办公效率。然而,随着公共 API 服务的不稳定性增加以及隐私要求的提高,本地部署成为了更优的选择。本地部署 DeepSeek-R1 模型具有以下核心优势:
- 响应速度快:本地处理消除了网络传输延迟,交互更加流畅。
- 数据隐私与安全:敏感文本数据完全保留在本地,无需上传至外部服务器,有效降低泄露风险。
- 成本可控:虽然 API 调用费用较低,但在高强度办公场景下仍会产生累积成本。本地部署模型一旦硬件就绪,即可实现零边际成本的无限次调用。
环境准备
在开始之前,请确保您的计算机满足以下基本要求:
- 操作系统:Windows 10/11 或 macOS。
- 内存:建议至少 16GB RAM。
- 显卡:显存至少 6GB(推荐 NVIDIA GPU 以获得最佳推理速度)。
- 软件:已安装 Git Bash 或 PowerShell(用于命令行操作)。
第一步:安装 Ollama
Ollama 是一个用于运行本地大模型的便捷工具。
- 访问 Ollama 官方网站下载对应系统的安装包。
- 运行安装程序,按照提示完成安装。
- 打开命令行窗口(Windows 下按 Win+R,输入 cmd),输入以下命令验证安装:
若显示版本号,说明安装成功。ollama --version
第二步:下载并运行 DeepSeek-R1 模型
根据硬件配置选择合适的模型版本。对于显存较小的设备,推荐使用量化版模型以节省资源。
- 在命令行中输入以下命令拉取模型:
系统会自动下载模型文件。下载完成后,模型将进入对话模式,您可以直接在终端进行测试。ollama run deepseek-r1:1.5b - 测试示例:输入'你好',观察模型回复是否正常。
第三步:集成到 Word 中的 VBA 宏
为了实现自动化调用,我们需要编写 VBA 代码来连接本地 Ollama 服务。
- 打开 Word,按 Alt+F11 进入 VBA 编辑器。
- 插入新模块,粘贴以下代码。
- 注意:本地部署通常不需要 API Key,但为了兼容接口格式,代码中保留了该字段,实际使用时可留空或设为任意值。
' 调用本地 DeepSeek API 函数
Function CallDeepSeekAPI(inputText As String) As String
Dim API As String
Dim SendTxt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
' 本地部署的大模型 API 地址
API = "http://localhost:11434/api/chat"
' 修改请求体为与本地大模型相匹配的格式
SendTxt = "{\"model\": \"deepseek-r1:1.5b\", \"messages\": [{\"role\":\"user\", \"content\":" & inputText & "}], \"stream\": false}"
Set Http = CreateObject("MSXML2.XMLHTTP")
With Http
.Open "POST", API, False
.setRequestHeader "Content-Type", "application/json"
' 本地模型通常无需认证,此处保留字段以保持结构一致
'.setRequestHeader "Authorization", "Bearer " & api_key
.send SendTxt
status_code = .Status
response = .responseText
End With
If status_code = 200 Then
CallDeepSeekAPI = response
Else
CallDeepSeekAPI = "Error: " & status_code & " - " & response
End If
Set Http = Nothing
End Function
Sub DeepSeekV3()
Dim inputText As String
Dim response As String
Dim regex As Object
Dim matches As Object
Dim originalSelection As Object
' 保存原始选中的文本
Set originalSelection = Selection.Range.Duplicate
If Selection.Type <> wdSelectionNormal Then
MsgBox "请先选中需要处理的文本。"
Exit Sub
End If
inputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\"")
response = CallDeepSeekAPI(inputText)
If Left(response, 5) <> "Error" Then
Set regex = CreateObject("VBScript.RegExp")
' 步骤 1:提取大模型回复内容
With regex
.Global = True
.MultiLine = True
.Pattern = "\"content\":\s*\"([\s\S]*?)\""
End With
If regex.Test(response) Then
response = regex.Execute(response)(0).SubMatches(0)
' 步骤 2:处理 Unicode 转义字符
response = Replace(response, "\u003c", "<")
response = Replace(response, "\u003e", ">")
' 步骤 3:删除标签及其内容(简化处理)
With regex
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "[\s\S]*?"
End With
response = regex.Replace(response, "")
' 步骤 4:转换\n 为实际换行符
response = Replace(response, "\n", vbCrLf)
' 步骤 5:移除 Markdown 格式
With regex
.Global = True
.Pattern = "(#\+\s*|\*\*|__|`|\*{1,2}|_{1,2}|~~|^>\s)"
response = .Replace(response, "")
End With
' 取消选中原始文本
Selection.Collapse Direction:=wdCollapseEnd
' 将内容插入到选中文字的下一行
Selection.TypeParagraph
Selection.TypeText Text:=response
' 将光标移回原来选中文本的末尾
originalSelection.Select
Else
MsgBox "Failed to parse API response.", vbExclamation
End If
Else
MsgBox response, vbCritical
End If
End Sub


