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

免费deepseek的API获取教程及将API接入word或WPS中

免费deepseek的API获取教程:

1 https://cloud.siliconflow.cn/中注册时填写邀请码:GAejkK6X即可获取2000 万 Tokens;
2 按照图中步骤进行操作
在这里插入图片描述

将API接入word或WPS中

1 打开一个word,文件-选项-自定义功能区-勾选开发工具-左侧的信任中心-信任中心设置-宏设置-启用所有宏-确定
在这里插入图片描述
2
2 开发工具-VB-插入-模块
在这里插入图片描述
在这里插入图片描述

3 在调出的窗口中输入如下代码

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}"

    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 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 Object

    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 regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)"""
        End With
        Set matches = regex.Execute(response)
        If matches.Count > 0 Then
            response = matches(0).SubMatches(0)
            response = Replace(Replace(response, """", Chr(34)), """", Chr(34))

            ' 取消选中原始文本
            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

4 文件-选项-自定义功能区-选中开发工具-新建组(命名为deepseek)
在这里插入图片描述
5 中间框中依次选中“宏”-deepseek模型-(选中刚创建的deepseek组)添加
6 文件-另存为带宏的模板-放入C:\Users<YourUsername>\AppData\Roaming\Microsoft\Word\STARTUP中,完成

7上述代码为v3模型,r1模型代码如下:

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"", ""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 = 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 Object

    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 regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)"""
        End With
        Set matches = regex.Execute(response)
        If matches.Count > 0 Then
            response = matches(0).SubMatches(0)
            response = Replace(Replace(response, """", Chr(34)), """", Chr(34))

            ' 取消选中原始文本
            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

完整版见链接:https://rvq2yh94tm.feishu.cn/docx/HXLDdLmZcoE7hGxS1RqcomEanSd

相关文章:

  • 前沿科技一览当今创新技术趋势
  • Unity Shader Graph 2D - Procedural程序化图形转动的环状六边形
  • Webpack和Vite插件的开发与使用
  • el-input输入框样式修改
  • 功能测试-黑盒测试
  • [0696].第11节:Kafka-Eagle监控
  • python学opencv|读取图像(六十二)使用cv2.morphologyEx()形态学函数实现图像梯度处理
  • 11、《Web开发性能优化:静态资源处理与缓存控制深度解析》
  • ipfs安装及其访问webui
  • JavaScript 中toLocaleString()的基本用法
  • 03:Spring之Web
  • this.globalThis || (this.globalThis = this)
  • Android中获取so文件来源于哪个库
  • SQL Server STUFF 函数的用法及应用场景
  • MATLAB图像处理:几何变换详解(裁剪、旋转、缩放)
  • C++ 设计模式-抽象工厂
  • Vision Transformer:打破CNN垄断,全局注意力机制重塑计算机视觉范式
  • 网络安全防范课后参考答案
  • Python爬虫实战:股票分时数据抓取与存储 (1)
  • 设计模式-模版方法
  • 泽连斯基启程前往土耳其
  • 德国总理默茨发表首份政府声明:将提升国防能力,全力发展经济
  • 深圳拟出让3宗居住用地,共计用地面积6.77公顷
  • 七部门:进一步增强资本市场对于科技创新企业的支持力度
  • A股午后拉升,沪指收复3400点:大金融发力,两市成交超1.3万亿元
  • 深圳中院回应“退休夫妻月入1.2万负债1.2亿”:其自述因经营不善负债