优秀的编程知识分享平台

网站首页 > 技术文章 正文

Word接入本地大模型工具调用

nanyue 2025-05-26 17:46:11 技术文章 7 ℃

前段时间在网上看个在word中整合调用大模型的工具实现,动手实践了一下,把过程整理下来。

1. Ollama部署本地大模型

1.1下载安装Ollama

访问ollama官网https://ollama.com/,点击download。

根据电脑系统选择相应的版本。点击下载

下载完后执行安装。

1.2下载运行模型

本机是个集显的笔记本,下载的DeepSeek-1.5B的蒸馏模型,根据自己机器配置选择相应的模型。

打开cmd控制台,执行

ollama run deepseek-r1:1.5b

如果没有下载模型,会自动下载到本地,运行起来后界面如下:

在控制台输入信息,看看效果吧,这里让大模型写了一首诗,可以看到<think>推理过程

可以执行ollama -h查看常用命令

2. 配置Word工具

2.1新建一个能启用宏的word文档

新建一个word文档,另存为“启用宏的word文档( *.docm)”

2.2设置word“开发工具”选项卡

在word导航菜单“文件->选项”,选择到“自定义功能区”,勾选“开发工具”

点击“确定”后,可以在工具栏看到“开发工具”

2.3启用宏设置

“文件->选项”打开word选项设置,选择到“信任中心”选项,点击“信任中心设置”按钮,添加“信任对VBA工程对象模型的访问”,可以开启“启用所有宏”

2.4创建宏到word模板中

打开“开发工具->宏”,创建DeepSeekV3(自己命名)的宏

2.5编写VBA脚本

点开“开发工具”的Visual Basic,打开脚本编辑器,打开DeepSeekV3宏脚本文件

编辑脚本内容(参考
https://mp.weixin.qq.com/s/BgiYTNP3JwkCl5DoZ8wv2Q,添加了是否显示think内容逻辑):

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


' 弹出窗口显示 API 响应(调试用)

' MsgBox "API Response: " & response, vbInformation, "Debug Info"


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

Dim showThink As Boolean


showThink = False

api_key = "pass"

If api_key = "" Then

MsgBox "Please enter the API key."

Exit Sub

ElseIf Selection.Type <> wdSelectionNormal Then

MsgBox "先选择一段文本内容."

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")


' 步骤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转义字符(如\u003c -> <)

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, "")

' 是否显示think内容

If showThink = False Then

With regex

.Global = True

.MultiLine = True

.IgnoreCase = True

.Pattern = "<think>(.*?)</think>"

End With

response = regex.Replace(response, "")

response = Replace(response, "<think>", "")

response = Replace(response, "</think>", "")

End If


' 步骤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

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.6添加工具按钮

打开“文件->选项”,“自定义功能区”选项卡

在左侧下拉切换到宏

右侧“开发工具”,先新建组,再重命名(我这里命名的是DSLocal),选择图标

选择左侧的宏,添加到DSLocal组中

将添加的宏也重命名一下(我这改为DeepSeekV3),如下效果

在开发工具中可以看到DeepSeekV3的按钮了

选择一行文本,执行DeepSeekV3按钮,效果如下

Tags:

最近发表
标签列表