DeepSeek应用——与word的配套使用

目录

一、效果展示

二、配置方法

三、使用方法

四、注意事项

1、永久化使用

2、宏被禁用

3、office的生成失败


记录自己学习应用DeepSeek的过程......

=======================================================================

这个是与WPS配套使用的过程,office的与这个类似:

一、效果展示

二、配置方法

1、在最上方的功能处找到开发工具,点击VB编辑器

2、点击"插入"------>"模块",得到一个空白的编辑界面

将下面的代码复制进去,由于最近deepseek比较火,我自己私有化本地部署了deepseek r1,这里的url就是自己本地的地址,而且代码的处理也是针对于deepseek的输出进行的编写,如果是其他模型,应该会报错。需要注意修改的有:

第8行:API = "http://localhost:1234/v1/chat/completions/",对应大模型的url地址

第9行: SendTxt = "{""model"": ""deepseek-r1-distill-qwen-7b"", ""messages"": [{""role"":""system"", ""content"":""你是一个文本编辑助手""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}",对应大模型的调用请求参数,可能需要改的就是model这个参数

第42行:api_key = "1234",对应于api_key,如果是本地私有化部署的就随意填写,如果是调用官网大模型的api,就为对应的key

vbscript 复制代码
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 = "http://localhost:1234/v1/chat/completions/"
    SendTxt = "{""model"": ""deepseek-r1-distill-qwen-7b"", ""messages"": [{""role"":""system"", ""content"":""你是一个文本编辑助手""}, {""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 DeepSeekR1()
    Dim api_key As String
    Dim inputText As String
    Dim response As String
    Dim regex As Object
    Dim matches As Object
    Dim originalSelection As Range

    ' API Key
    api_key = "1234"
    If api_key = "" Then
        MsgBox "Please enter the API key.", vbExclamation
        Exit Sub
    End If

    ' 检查是否有选中文本
    If Selection.Type <> wdSelectionNormal Then
        MsgBox "Please select text.", vbExclamation
        Exit Sub
    End If

    ' 保存原始选区
    Set originalSelection = Selection.Range.Duplicate

    ' 处理特殊字符
    inputText = Selection.Text
    inputText = Replace(inputText, "\", "\\")
    inputText = Replace(inputText, vbCrLf, " ")
    inputText = Replace(inputText, vbCr, " ")
    inputText = Replace(inputText, vbLf, " ")
    inputText = Replace(inputText, """", "\""") ' 转义双引号

    ' 发送 API 请求
    response = CallDeepSeekAPI(api_key, inputText)

    ' 处理 API 响应
    If Left(response, 5) <> "Error" Then
        ' 解析 JSON
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)""" ' 匹配 JSON 的 "content" 字段
        End With
        Set matches = regex.Execute(response)

        If matches.Count > 0 Then
            ' 提取 API 响应的文本内容
            response = matches(0).SubMatches(0)

            ' 处理转义字符
            response = Replace(response, "\n", vbCrLf)
            response = Replace(response, "\\", "\") ' 处理 JSON 里的反斜杠
            response = 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

之后关闭窗口就行

3、点击wps左上角的"文件"------>"选项",进入"选项"窗口,之后选择"自定义功能区"

进入后在开发工具这里新建组,我在这里改组名为deepseek,然后在左侧搜索"宏",找到之前创建的模块,点击添加,并重新命名,我这里改为"生成",最后点击确认即可。

三、使用方法

鼠标选择要发送给大模型的文案,点击你所重命名的功能按钮,我这里是"生成"

四、注意事项

1、永久化使用

这个宏只是一次性的,下次创建新的文档时,这个"生成"便不好使。

若要永久启动宏,需要将其保存成一个模板,每次创建文档时候,使用这个模板创建,具体方法为:

1)另存为带宏的模板

2)之后若要编辑新的word,点击"开发工具"------>"加载项",去掉文档模板框中的内容,点选共用模板里面的deepseek_word.dotm,点击打开即可编辑。

2、宏被禁用

只需要把宏启动即可,wps和office的宏启动方式有所不同,不过均在"文件"------>"选项"------>"信任中心"这里,wps的直接可以看到"宏安全性",将其调整问中或者低;而office的在"信任中心设置"里,在宏设置中启动所有宏即可。

3、office的生成失败

按照之前的步骤,一般不会出现问题,但是我在进行office的配置时发现报错。按照报错信息打印出来的结果,如下图所示,我个人推测是office在进行数据接收的时候有数据的长度限制,导致后面的数据直接截断,因此后面的数据处理时,格式错误使得内部报错。(不过网上也有说是因为单双引号导致的)

解决方案:

改成流式的输出,这样里面的vb代码需要变化

需要注意的是:

第九行:SendTxt = "{""model"": ""deepseek-r1-distill-qwen-7b"", ""messages"": [{""role"":""system"", ""content"":""我是一个文本生成助手""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": true}",这里的stream改为true。

之后对strArr = Split(response, "data:") ,按照data对流数据进行切分,然后再正则匹配,取出答案进行拼接。

vbscript 复制代码
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 = "http://localhost:1234/v1/chat/completions/"
    SendTxt = "{""model"": ""deepseek-r1-distill-qwen-7b"", ""messages"": [{""role"":""system"", ""content"":""我是一个文本生成助手""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": true}"

    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 DeepSeekR1()
    Dim api_key As String
    Dim inputText As String
    Dim response As String
    Dim lastRes As String
    Dim strArr() As String
    Dim regex As Object
    Dim matches As Object
    Dim originalSelection As Range
    
    lastRes = ""

    ' API Key
    api_key = "1234"
    If api_key = "" Then
        MsgBox "Please enter the API key.", vbExclamation
        Exit Sub
    End If

    ' 检查是否有选中文本
    If Selection.Type <> wdSelectionNormal Then
        MsgBox "Please select text.", vbExclamation
        Exit Sub
    End If

    ' 保存原始选区
    Set originalSelection = Selection.Range.Duplicate

    ' 处理特殊字符
    inputText = Selection.Text
    inputText = Replace(inputText, "\", "\\")
    inputText = Replace(inputText, vbCrLf, " ")
    inputText = Replace(inputText, vbCr, " ")
    inputText = Replace(inputText, vbLf, " ")
    inputText = Replace(inputText, """", "\""") ' 转义双引号

    ' 发送 API 请求
    response = CallDeepSeekAPI(api_key, inputText)

    strArr = Split(response, "data:") ' 按照流数据进行切割
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = """content"":""(.*?)""" ' 匹配 JSON 的 "content" 字段
    End With
    
    For Each strItem In strArr ' 循环处理
        ' 处理 API 响应
        If Left(strItem, 5) <> "Error" Then
            ' 解析 JSON
            Set matches = regex.Execute(strItem)
            If matches.Count > 0 Then
                ' 提取 API 响应的文本内容
                strItem = matches(0).SubMatches(0)
                ' 处理转义字符
                strItem = Replace(strItem, vbCrLf, "")
                strItem = Replace(strItem, "\n", vbCrLf)
                strItem = Replace(strItem, "\\", "\") ' 处理 JSON 里的反斜杠
                strItem = Replace(strItem, "&", "") ' 过滤 `&`,防止意外符号
                If Len(strItem) <> 0 Then
                    lastRes = lastRes & strItem
                End If
            End If
        Else
            MsgBox response, vbCritical
        End If
    Next
    ' 让光标移动到文档末尾,防止覆盖已有内容
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.TypeParagraph
    Selection.TypeText Text:=lastRes

    ' 将光标移回原来选中文本的末尾
    originalSelection.Select
End Sub
相关推荐
过河卒_zh156676615 小时前
网信发布2025年“人工智能+政务”规范应用案例拟入选名单公示
人工智能·大模型·aigc·政务·算法备案
Chukai12315 小时前
第1章:了解大模型与RAG
大模型·rag
大模型教程.16 小时前
收藏级教程:ReAct模式详解,让大模型从回答问题到解决问题
前端·人工智能·机器学习·前端框架·大模型·产品经理·react
程序猿追19 小时前
在昇腾NPU上实战部署LongCat-Video:从环境配置到长视频生成的完整指南
python·大模型·华为云·音视频
KAI智习1 天前
大模型榜单周报(2025/12/20)
人工智能·大模型
咩图1 天前
使用ollama完成私有大模型搭建
大模型·ollama·私有化
我很哇塞耶1 天前
BOSS直聘3B超越Qwen3-32B,更多训练数据刷新小模型极限
人工智能·ai·大模型
骚戴2 天前
n1n:从替代LiteLLM Proxy自建网关到企业级统一架构的进阶之路
人工智能·python·大模型·llm·gateway·api
喜欢吃豆2 天前
下一代 AI 销售陪练系统的架构蓝图与核心技术挑战深度研究报告
人工智能·架构·大模型·多模态·ai销售陪练
不会吉他的肌肉男不是好的挨踢男2 天前
SearXNG AI 的免费搜索引擎api 调用
搜索引擎·ai·大模型·serxng