如何在WPS上接入DeepSeek-R1
大家好!今天带来的是一份超详细的教程,教你如何将DeepSeek-R1大模型接入WPS。如果你经常使用WPS处理文档,而又想借助AI提升效率,那么这篇文章一定能帮到你!整个过程简单易懂,小白也能轻松上手。
为什么要将DeepSeek-R1接入WPS?
将DeepSeek-R1接入WPS后,你可以直接在文档中调用AI处理文字内容,比如生成文本、优化表达、甚至是进行推理分析。这不仅能大幅提升文档处理效率,还能让你的工作变得更加智能化。
接下来,我们将一步步教你如何实现这一功能。
第一步:获取DeepSeek-R1的API Key
要使用DeepSeek-R1大模型,首先需要获取它的API Key。
由于官网服务器资源紧张,已暂停 API 服务充值,所以我们需要其他的方法来获取,具体请看这篇:【获取DeepSeek API】
获取到API Key后,记得妥善保存,因为后续配置中需要用到它。
【注意:不管是哪个方案,对应平台上一定要有余额】
第二步:配置WPS开发工具
要在WPS中使用DeepSeek-R1,我们需要启用开发工具,并进行一些简单的设置。
1. 启用开发工具
打开WPS,新建一个文档。
点击顶部菜单栏的“文件” → “选项” → “自定义功能区”。
在右侧的功能区列表中,勾选“工具”选项。
点击“确定”保存设置。
2. 配置信任中心
- 在WPS中,点击“文件” → “选项” → “信任中心”。
- 选择“信任中心设置” → “宏安全性”。
- 将安全性设置为“低”,以便运行VBA宏。
3. 添加模块
点击顶部菜单栏的“工具” → “开发工具” → “切换到VB环境”,然后重启。
重启后,回到刚才的步骤,再点击VB编辑器。
在弹出的VB编辑器窗口中,点击“插入” → “模块”。
将下方的代码复制到编辑区中,并替换为你的API Key:
以下是几个完整代码示例,记得替换为你的API Key:
Deepseek-R1代码(官方apikey)
textFunction 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.deepseek.com/chat/completions" SendTxt = "{""model"": ""deepseek-reasoner"", ""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 reasoningRegex As Object Dim contentRegex As Object Dim matches As Object Dim reasoningMatches As Object Dim originalSelection As Object Dim reasoningContent As String Dim finalContent As String 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 reasoningRegex = CreateObject("VBScript.RegExp") With reasoningRegex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """reasoning_content"":""(.*?)""" End With Set contentRegex = CreateObject("VBScript.RegExp") With contentRegex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """content"":""(.*?)""" End With ' 提取推理内容 Set reasoningMatches = reasoningRegex.Execute(response) If reasoningMatches.Count > 0 Then reasoningContent = reasoningMatches(0).SubMatches(0) reasoningContent = Replace(reasoningContent, "\n\n", vbNewLine) reasoningContent = Replace(reasoningContent, "\n", vbNewLine) reasoningContent = Replace(Replace(reasoningContent, """", Chr(34)), """", Chr(34)) End If ' 提取最终回答 Set matches = contentRegex.Execute(response) If matches.Count > 0 Then finalContent = matches(0).SubMatches(0) finalContent = Replace(finalContent, "\n\n", vbNewLine) finalContent = Replace(finalContent, "\n", vbNewLine) finalContent = Replace(Replace(finalContent, """", Chr(34)), """", Chr(34)) ' 取消选中原始文本 Selection.Collapse Direction:=wdCollapseEnd ' 插入推理过程(如果存在) If Len(reasoningContent) > 0 Then Selection.TypeParagraph Selection.TypeText "推理过程:" Selection.TypeParagraph Selection.TypeText reasoningContent Selection.TypeParagraph Selection.TypeText "最终回答:" Selection.TypeParagraph End If ' 插入最终回答 Selection.TypeText finalContent ' 将光标移回原来选中文本的末尾 originalSelection.Select Else MsgBox "Failed to parse API response.", vbExclamation End If Else MsgBox response, vbCritical End If End Sub
DeepSeek-V3代码(官方apikey)
textFunction 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.deepseek.com/chat/completions" SendTxt = "{""model"": ""deepseek-chat"", ""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
DeepSeek-R1代码(硅基流动)
textFunction 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 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. 自定义功能区
回到WPS主界面,点击“文件” → “选项” → “自定义功能区”。
在右侧功能区列表中,点击“新建组”,将其命名为“DeepSeek”。
在左侧命令列表中,选择“宏”,找到刚刚添加的宏(如“DeepSeekV3”)。
点击“添加”,然后右键重命名为“生成”。
点击“确定”保存设置。
到这里,WPS的开发工具就配置完成了!接下来,我们来看如何使用DeepSeek-R1大模型。
第三步:使用DeepSeek-R1生成内容
打开WPS文档,选中需要处理的文字。
点击刚刚添加的“生成”按钮。
稍等片刻,DeepSeek-R1会返回处理结果,并直接插入到文档中。
是不是很简单?通过这种方式,你可以快速调用DeepSeek-R1处理文档内容,无需切换到其他工具。
第四步:创建WPS模板
为了方便日后使用,我们可以将当前配置保存为一个WPS模板文件。
点击“文件” → “另存为”。
在保存类型中选择“Microsoft Word 带宏的模板文件 (*.dotm)”。
将文件保存到WPS的模板文件夹中(通常是以下路径):
C:\Users\用户名\AppData\Roaming\kingsoft\wps\startup
下次需要使用时,只需打开模板文件即可。
最后
通过以上步骤,我们成功将DeepSeek-R1大模型接入了WPS。无论是生成文本、优化表达,还是进行复杂的推理分析,这个工具都能让你的文档处理效率大幅提升。
我的感觉是,这种结合AI的方式真的很实用,特别是对需要频繁处理文档的朋友来说,绝对是一个神器!赶紧试试吧!