当前位置: 首页 > article >正文

Word接入DeepSeek(API的作用)

1.打开”Word”,点击“文件”。

2.点击“选项”。

3.点击“信任中心”——“信任中心设置”。

4. 勾选”启用所有宏“,点击”确定“。

 5.点击“自定义功能区”,勾选上“开发工具”,点击“确定”。

6.返回“文件——开发工具“下的”Visual Basic"。

7.点击“插入”,选择“模块”。 

8.打开硅基流动官网(SiliconFlow, Accelerate AGI to Benefit Humanity) 

9.选择“API密钥”。 

10.点击右上角:“新建API密钥”。 

11.随便输入一个名字,新建密钥。

12.复制刚建立的API密钥。

13.替换以下代码中:api_key="" 中的内容。

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-V3"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
    '想用R1模型,就把上面的model的deepseek-ai/DeepSeek-V3换成deepseek-ai/DeepSeek-R1

    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 Range

    ' API Key
    api_key = "sk-esykfihseaarfitflbmcxmhxlevppfiublbnlpmlaeywdnaf"
    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

14.粘贴以上代码到“模块1”中。 

15.再次回到选项,点击自定义功能区。

16.在“开发工具”下,点击新建组。“命令”下拉选择“宏”。

17.把我们刚才写的模块,拖拽到右侧。

18.点击“确定”。 

19.这样我们就可以在Word里使用"DeepSeek"。选中指令(如:五行有哪5个?),点击上侧“DeepSeekV3”模块。

20.等待一段时间,就出答案了。 

注:在Word里反应比较慢。有时候问复杂问题还会报:Error: 504-{"code":50501,"message":"Model service timeoutPlease try again later.","data":null}错误(服务器繁忙,请稍后再试)。

大家可以去DeepSeek官网(DeepSeek | 深度求索),申请自己的API后,充钱。

21.申请API的意义

有道、WPS等软件(这些大公司也付费了或企业间合作)已经免费集成了DeepSeek,在这些软件里面我们可以直接使用。

但如果我们开发自己的软件、可视化平台、物联网等,需要引用DeepSeek,就需要自己的API。

 

 

 

 

 


http://www.kler.cn/a/553177.html

相关文章:

  • 使用 Certbot 自动获取和更新 Let‘s Encrypt SSL 证书
  • 数据湖与数据仓库:初学者的指南
  • MongoDB:listDatabases failed : not master and slaveOk=false
  • 理解计算机系统_虚拟内存(二)缓存
  • 【核心算法篇十六】《DeepSeek强化学习:MuZero算法核心解析》
  • Spring AI 接入 DeepSeek:开启智能应用的新篇章
  • “深入浅出”系列之C++:(8)libevent 库
  • 如何开发一个大模型应用?
  • Node events模块
  • ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
  • 长文档处理痛点:GPT-4 Turbo引文提取优化策略与替代方案讨论
  • Linux 的基本命令
  • 使用 Ansys HFSS 对植入式医疗设备进行无线电力传输和 SAR 仿真
  • 美团将为全职及稳定兼职骑手缴纳社保,未来将覆盖所有骑手
  • Python基于Flask的豆瓣Top250电影数据可视化分析与评分预测系统(附源码,技术说明)
  • springboot使用XxlJob定时任务框架
  • php文件包含
  • JAVA 集成 ElasticSearch
  • 【STM32H743IIT6】正点原子阿波罗TFTLCD移植
  • Python高级语法之selenium