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
相关推荐
忧郁蓝调263 小时前
RAGFlow部署与使用(开源本地知识库管理系统,包括kibana配置)
人工智能·开源·大模型·github·知识库·rag·ragflow
强哥之神3 小时前
微软 AI 发布 LongRoPE2:近乎无损地将大型语言模型上下文窗口扩展至 128K 标记,保持 97% 短上下文准确性
人工智能·语言模型·自然语言处理·大模型·智能体·ai代理
张高兴7 小时前
张高兴的大模型开发实战:(二)使用 LangChain 构建本地知识库应用
python·langchain·大模型
韩曙亮11 小时前
【AI 大模型】RAG 检索增强生成 ⑤ ( 向量数据库 | 向量数据库 索引结构和搜索算法 | 常见 向量数据库 对比 | 安装并使用 向量数据库 chromadb 案例 )
数据库·人工智能·大模型·openai·向量数据库·ai大模型·chromadb
skywalk816312 小时前
使用 PaddleNLP 在 CPU(支持 AVX 指令)下跑通 llama2-7b或DeepSeek-r1:1.5b 模型(完成度80%)
人工智能·python·大模型·paddlenlp
ximigoo1 天前
一个敢问,一个敢答
ai·大模型
magic_ll1 天前
【大模型】Transformer、GPT1、GPT2、GPT3、BERT 的论文解析
大模型·transformer
HealthScience2 天前
RAG、Agent、微调等6种常见的大模型定制策略
python·深度学习·大模型
_Meilinger_2 天前
大模型微调|使用 LLaMA-Factory 微调 Llama3-8B-Chinese-Chat 完成知识问答任务
大模型·llm·微调·llama·大模型微调·llama-factory·unsloth
Nicolas8932 天前
【大模型实战】利用ms-swift微调框架对QwQ-32B推理模型进行微调
大模型·swift·大模型微调·lora微调·微调框架·推理模型微调·msswift