优秀的编程知识分享平台

网站首页 > 技术文章 正文

2分钟教你学会Word接入deepseek

nanyue 2025-05-25 14:33:35 技术文章 3 ℃

第一步:获取DeepSeek API密钥首先登录注册硅基流动网址https://siliconflow.cn/zh-cn/,然后获取进入主页,先选中”API 密钥“选项,然后单击”新建API密钥“按钮,如图1所示。

图1 选择新建API密钥


从图2中可以看到在弹出的对话框中,输入密钥描述,然后点击”新建密钥“,即可创建密钥。


图2 新建API密钥对话框


根据生成的密钥,我们复制它,进而调用DeepSeek模型,如图3所示。


图3 复制API密钥

第二步:打开开发工具

首先打开Word软件,如图4所示,然后单击“选项”,弹出“Word选项”对话框。在弹出的“Word选项”对话框中,首先选中“自定义功能区”,然后在“自定义功能区”勾选上开发工具,如图5所示。

图4 Word选项设置


图5 开发工具设置

第三步:启用宏命令

在“Word选项”对话框中先选择信任中心按钮,然后单击“信任中心设置”按钮,如图6所示。在弹出的“信任中心设置”对话框中,先选择“宏设置”选项,然后单击“启用所有宏”,最后勾选“信任对VBA工程对象模型的访问”,如图7所示。


图6 选择信任中心


图7 信任中心设置

第四步:创建VB宏代码

回到Word菜单栏,从图8中可以看到,这个时候多了一个“开发工具”选项,首先选择“开发工具”选项,然后单击“Visual Basic”按钮。在弹出的Visual Basic代码编辑应用菜单栏中,先选择“插入”选项卡,然后单击“模块”按钮,如图9所示。在图10中,首先重命名模块名字为DeepSeek,然后复制接入DeepSeek API的代码块(PS:这里的代码块中的API采用的是硅基流动的api和api Key,你需要替换为你自己采用的api和api key),如下代码块,所示。


图8 选择Visual Basic

图9 Visual Basic中插入模块


图10 编辑接入DeepSeek代码块

    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 = "https://api.siliconflow.cn/v1/chat/completions"    SendTxt = "{""model"": ""deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""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 = NothingEnd FunctionSub DeepSeekR1()    Dim api_key As String    Dim inputText As String    Dim response As String    Dim regex As Object    Dim reasoningRegex As Object    Dim contentRegex As Object    Dim matches As Object    Dim reasoningMatches As Object    Dim originalSelection As Object    Dim reasoningContent As String    Dim finalContent As String    api_key = "替换为你的api key"    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 reasoningRegex = CreateObject("VBScript.RegExp")        With reasoningRegex            .Global = True            .MultiLine = True            .IgnoreCase = False            .Pattern = """reasoning_content"":""(.*?)"""        End With                Set contentRegex = CreateObject("VBScript.RegExp")        With contentRegex            .Global = True            .MultiLine = True            .IgnoreCase = False            .Pattern = """content"":""(.*?)"""        End With        ' 提取推理内容        Set reasoningMatches = reasoningRegex.Execute(response)        If reasoningMatches.Count > 0 Then            reasoningContent = reasoningMatches(0).SubMatches(0)            reasoningContent = Replace(reasoningContent, "\n\n", vbNewLine)            reasoningContent = Replace(reasoningContent, "\n", vbNewLine)            reasoningContent = Replace(Replace(reasoningContent, """", Chr(34)), """", Chr(34))        End If        ' 提取最终回答        Set matches = contentRegex.Execute(response)        If matches.Count > 0 Then            finalContent = matches(0).SubMatches(0)            finalContent = Replace(finalContent, "\n\n", vbNewLine)            finalContent = Replace(finalContent, "\n", vbNewLine)            finalContent = Replace(Replace(finalContent, """", Chr(34)), """", Chr(34))            ' 取消选中原始文本            Selection.Collapse Direction:=wdCollapseEnd            ' 插入推理过程(如果存在)            If Len(reasoningContent) > 0 Then                Selection.TypeParagraph                Selection.TypeText "推理过程:"                Selection.TypeParagraph                Selection.TypeText reasoningContent                Selection.TypeParagraph                Selection.TypeText "最终回答:"                Selection.TypeParagraph            End If            ' 插入最终回答            Selection.TypeText finalContent            ' 将光标移回原来选中文本的末尾            originalSelection.Select        Else            MsgBox "Failed to parse API response.", vbExclamation        End If    Else        MsgBox response, vbCritical    End IfEnd Sub

第五步:新建宏命令组

在图11中,我们首先从Word选项对话框中选中“自定义功能区”,然后从列位置选择命令中选择“宏”,这个时候可以看到有一个”
Normal.DeepSeek.DeepSeekR1“宏命令,选中它,在自定义功能区的“开发工具”选项卡下,鼠标右键单击选择“添加新组”。将新建的组名字改为"DeepSeek",图标改为小人,如图12,图13所示。


图11 开发工具设置


图12 开发工具组重命名选择


图13 开发工具重命名对话框设置


选中第四步中创建的宏命令”
Normal.DeepSeek.DeepSeekR1“,同时,选中刚才新建的自定义功能区下的开发工具“
Normal.DeepSeek.DeepSeekR1“,如图14所示。


图14 添加宏命令到组


接下来,对宏命令对应的开发工具重命名,先选中该组件,然后鼠标右键单击重命名,重命名为“DeepSeek”,图标设置为放大镜,如图15,图16所示。


图15 DeepSeek开发工具组件重命名选择


图16 DeepSeek开发工具组件重命名对话框

第六步:开始创作

现在Word中已经配置好DeepSeek了,大家可以自由的在Word中使用DeepSeek生成文档了。如图17所示,先选中”开发工具“选项卡,然后选中提示词文字,最后直接点击我们创建的”DeepSeek“工具生成文字。


图17 使用DeepSeek工具生成文字

对Word接入DeepSeek有问题的朋友欢迎关注本公众号,一起讨论大模型和网络安全前沿知识!

Tags:

最近发表
标签列表