主页 > 人工智能  > 

word$deepseep

word$deepseep

1、进入官网地址。

DeepSeek

2、进入DeepSeek的API文档

3、点击DeepSeek开放平台左侧的“API Keys”, 再点击“创建API Key”

4、在弹出的对话框中,输入自己的API Key名称,点击创建。

sk-0385cad5e19346a0a4ac8b7f0d7be428

5、打开Word文档。

6、Word找到文件。

7、文件-选项

8、在“word选项”对话框中,点击选择“自定义功能区”,然后在右侧选中“开发工具”,使“开发工具”前面的方框中出现“”,word的菜单栏中就会出现“开发工具”项目。

9、打开开发工具。

10、信任中心-信任中心设置。

11、信任中心-宏的设置-全部启用。

12、在“信任中心设置”对话框中,点击“宏设置”,在左侧选择“启用所有宏……”,然后选中“信任对VBA工程对象模型的访问”。

13、在word菜单栏,点击“开发工具”项,选择“Visual Basic”。

14、进入Visual Basic编辑界面,如下图,选择“Normal”下的“模块”。

Option Explicit Sub DeepSeekV3() Const api_Url As String = " api.deepseek /chat/completions" Const api_Key As String = "替换为自己申请的API Key" Dim inputText As String Dim response As String Dim originalSelection As Range On Error GoTo ErrorHandler ' 检查API密钥内容 If api_Key = "" Then MsgBox "请输入您的API Key。", vbExclamation Exit Sub End If ' 检查是否选中文本 If Selection.Type <> wdSelectionNormal Then MsgBox "请选中要分析的文本。", vbExclamation Exit Sub End If ' 保存原始选中的文本 Set originalSelection = Selection.Range.Duplicate ' 处理选中的文本 inputText = ReplaceSpecialCharacters(Selection.text) ' 调用Deepseek API response = CallDeepseekAPI(api_Key, api_Url, inputText) ' 处理API响应 If IsJsonResponse(response) Then response = FormatJson(response) InsertResponseToDocument response MsgBox "API分析内容已插入文档!", vbInformation Else MsgBox response, vbCritical End If Exit Sub ErrorHandler: MsgBox "发生错误:" & Err.Description, vbCritical End Sub ' 将API响应插入文档 Sub InsertResponseToDocument(response As String) With ActiveDocument.Content .InsertAfter vbCrLf & "分析内容:" & vbCrLf & response End With End Sub ' 调用DeepseekAPI函数 Function CallDeepseekAPI(api_Key As String, api_Url As String, inputText As String) As String Dim http As Object Dim requestBody As String Dim response As String Dim status_Code As Integer On Error GoTo ErrorHandler ' 构造请求体 requestBody = BuildRequestBody(inputText) ' 创建HTTP对象 Set http = CreateObject("MSXML2.XMLHTTP") ' 发送请求 With http .Open "POST", api_Url, False .setRequestHeader "Content-Type", "application/json" .setRequestHeader "Authorization", "Bearer " & api_Key .send requestBody status_Code = .Status response = .responseText End With ' 检查HTTP状态码 If status_Code = 200 Then CallDeepseekAPI = response Else CallDeepseekAPI = "API 请求失败,状态码:" & status_Code & "-" & response End If ExitFunction: Set http = Nothing Exit Function ErrorHandler: CallDeepseekAPI = "调用API时发生错误:" & Err.Description Resume ExitFunction End Function ' 构造API请求体 Function BuildRequestBody(inputText As String) As String BuildRequestBody = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}" End Function ' 检查是否为JSON响应 Function IsJsonResponse(response As String) As Boolean IsJsonResponse = (Left(response, 1) = "{") End Function ' 辅助函数:格式化 JSON 响应 Function FormatJson(json As String) As String Dim i As Long Dim indentLevel As Long Dim result As String Dim char As String indentLevel = 0 result = "" For i = 1 To Len(json) char = Mid(json, i, 1) Select Case char Case "{", "[" result = result & char & vbCrLf & Space((indentLevel + 1) * 4) indentLevel = indentLevel + 1 Case "}", "]" indentLevel = indentLevel - 1 result = result & vbCrLf & Space(indentLevel * 4) & char Case "," result = result & char & vbCrLf & Space(indentLevel * 4) Case ":" result = result & char & " " Case Else result = result & char End Select Next i FormatJson = result End Function ' 辅助函数:替换特殊字符 Function ReplaceSpecialCharacters(text As String) As String ReplaceSpecialCharacters = Replace(Replace(Replace(Replace(Replace(text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""") End Function

15、模块代码编辑完成后,进入“word选项”对话框,选择“开发工具”,在其上右击,选择“添加新组”。

16、选中新添加的组,在“下列位置选择命令”的下拉框中,选择“宏”。在出现的模块列表中,选择新编辑的模块,点击“添加”按钮

17、测试。

标签:

word$deepseep由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“word$deepseep