办公帮手:本地Ollama部署DeepSeek R1模型并接入Word,非常详细
很多介绍DeepSeek+Word,都是通过API key的方式,实现了DeepSeek与Word的有机结合,但是DeepSeek由于近期服务器压力较大,暂时停止了API key的注册服务,那么我们可以采用本地部署的方式实现,下面给出详细步骤。
很多介绍DeepSeek+Word,都是通过API key的方式,实现了DeepSeek与Word的有机结合,但是DeepSeek由于近期服务器压力较大,暂时停止了API key的注册服务,那么我们可以采用本地部署的方式实现,下面给出详细步骤。
1.本地部署DeepSeek-R1模型
访问ollama官网,点击download。
根据电脑系统选择相应的版本。点击下载。
双击文件点击安装。
等待安装完成后,按win+r键,输入cmd调出命令行窗口。输入“ollama --version”,回车后出现程序版本号表示安装成功。
我们选择推理能力更强的deepseek-r1模型进行本地部署。小编的电脑显存为4G,因此选择最小的1.5b模型进行部署,如果显存更大的话,可以选择更大的模型。
在命令行窗口中输入:ollama run deepseek-r1:1.5b
此时会自动下载模型,下载成功后提示success。可以与模型对话试试,如下:
到这里就完成了deepseek-r1模型的本地部署。
2.嵌入Word开发工具
2.1 开发者工具的配置
新建一个Word文档,点击 文件 -> 选项 -> 自定义功能区,勾选“开发者工具”。
点击 信任中心 -> 信任中心设置,选择“启用所有宏”。
接下来点击确定,我们发现选项卡中出现了“开发者工具”,点击开发者工具,点击Visual Basic,将会弹出一个窗口。
2.2 复制代码
我们点击新窗口中的插入,点击模块。将下面代码复制到编辑区域。
Function CallDeepSeekAPI(api_key As String, 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 api_key As String
Dim inputText As String
Dim response As String
Dim regex As Object
Dim matches As Object
Dim originalSelection As Object
api_key = "pass"
If api_key = "" Then
MsgBox "Please enter the API key."
Exit Sub
ElseIf Selection.Type <> wdSelectionNormal Then
MsgBox "Please select text."
Exit Sub
End If
' 保存原始选中的文本
Set originalSelection = Selection.Range.Duplicate
inputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")
response = CallDeepSeekAPI(api_key, inputText)
If Left(response, 5) <> "Error" Then
Set regex = CreateObject("VBScript.RegExp")
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)
'
response = Replace(response, "\u003c", "<")
response = Replace(response, "\u003e", ">")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "[\s\S]*?"
End With
response = regex.Replace(response, "")
'
response = Replace(response, "\n", vbCrLf)
With regex
.Global = True
.Pattern = "(#+\s*|\*\*|__|`|\*{1,2}|_{1,2}|~~|^>\s)"
response = .Replace(response, "")
End With
response = regex.Replace(response, "")
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
完成后,可直接关闭弹窗。
2.3 添加新组
点击 文件 -> 选项 -> 自定义功能区,右键开发工具,点击添加新组。在添加的新建组点击右键,点击重命名。将其命名为DeepSeek,并选择心仪的图标,最后点击确定。
首先选择DeepSeek(自定义),选择左侧的命令为“宏”,找到我们添加的DeepSeekV3,选中后点击添加。
最后点击确定。
至此,Word成功接入DeepSeek大模型。
2.4效果测试
选中文字,点击生成,就可以直接将选中的文本发送给大模型,大模型将会按照你选中的文本,做出响应。
更多推荐
所有评论(0)