Word接入DeepSeek

原理是利用Word的VBA宏,写代码接入API。

注册硅基流动

https://account.siliconflow.cn/login  邀请码:ZHPkGiJV

注册这个是为了有一个api调用的api_key,有一些免费的额度可以使用。大概就是这个公司提供token,我们使用这个公司的模型调用deepseek的r1。

新建API_KEY

这个是为了获取一个密钥,sk-开头的一串字符串。例如sk-ncoublxkoyxsntfnnywfpxxxxxxxxxx,在下面的代码块里,把刚刚获取的密钥,替换成你真实的密钥

可以在cmd命令里去尝试:

复制代码
curl --request POST \
  --url https://api.siliconflow.cn/v1/chat/completions \
  --header 'Authorization: Bearer 刚刚获取的密钥' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
  "frequency_penalty": 0.5,
  "max_tokens": 512,
  "n": 1,
  "response_format": {
    "type": "text"
  },
  "temperature": 0.7,
  "top_k": 50,
  "top_p": 0.7,
  "messages": [
    {
      "role": "user",
      "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?"
    }
  ]
}'
复制代码

如果正确返回,那么则调动流程都ok了。

如何呼出Word的VBA编辑

alt+f11 或者开发工具->VB编辑器

WPS这边VB编辑器是要开会员的,不过也能破解-。-

插件安装参考:https://www.bilibili.com/video/BV1KGeAewE6C/?spm_id_from=333.337.search-card.all.click

VBA代码插入

在下面的代码里面替换咱刚刚获取的api_key,创建了一个新的宏

复制代码
Sub DeepSeekPolish()
    Dim selectedText As String
    Dim apiKey As String
    Dim response As Object, re As String
    Dim midString As String
    Dim ans As String
    Dim polishPrompt As String
    Dim URL As String
    Dim jsonResponse As Object
    
    ' 检查是否有正常选中的文本
    If Selection.Type = wdSelectionNormal Then
        ' 获取选中文本并去除不需要的字符
        selectedText = Selection.Text
        selectedText = Replace(selectedText, ChrW$(13), "")
        
        ' 定义API密钥和请求URL
        apiKey = "your_api_keys"
        URL = "https://api.siliconflow.cn/v1/chat/completions"
        
        ' 设置润色提示词
        polishPrompt = "请润色以上文字,要求语句通顺,条理清晰,专业而合理。"
        
        ' 创建HTTP请求对象并设置参数
        Set response = CreateObject("MSXML2.XMLHTTP")
        response.Open "POST", URL, False
        
        ' 添加必要的头部信息
        response.setRequestHeader "Content-Type", "application/json"
        response.setRequestHeader "Authorization", "Bearer " + apiKey
        
        ' 构建请求体
        Dim requestBody As String
        requestBody = "{""model"":""deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & " " & polishPrompt & """}], ""temperature"":0.7}"
        
        ' 发送请求
        response.Send requestBody
        
        ' 检查请求是否成功
        If response.Status = 200 Then
            ' 处理响应数据
            re = response.responseText
            
            midString = Mid(re, InStr(re, """content"":""") + 11)
            ans = Split(midString, """")(0)
            ans = Replace(ans, "\n", "")
            
            ' 将原选中文本与润色后的文本一起插入文档中
            Selection.Text = selectedText & vbNewLine & ans
        Else
            MsgBox "API请求失败,状态码:" & response.Status & ",响应:" & response.responseText
        End If
    Else
        MsgBox "请先选中一段文本。"
    End If
End Sub
复制代码

 

自定义word功能

 文件->选项->自定义功能区->新建组AI->新建选项卡deepseek->选中宏命令(刚刚创建的新宏)

使用

在工具栏里能找到刚刚的宏,调用deepseek即可

 

posted @   王鹏鑫  阅读(2842)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示