日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

回答網(wǎng)友提問:Word 里如何調(diào)用 DeepSeek API?

 汪子熙 2025-04-20 發(fā)布于上海

筆者運(yùn)營的知識星球里有朋友提問,大意是公司不能訪問外網(wǎng),想在 Microsoft Word 里訪問公司本地部署的 DeepSeek API,問具體應(yīng)該如何操作?

筆者蛇年春節(jié)時(shí)曾經(jīng)嘗試過自己部署 DeepSeek. 筆者使用的筆記本配置:Intel(R) Core(TM) i7 2.4G + 64G 內(nèi)存 + 2TB SSD

所以筆者沒打算在本地部署哪怕是參數(shù)最小的 DeepSeek 版本,而是死皮賴臉的找騰訊云社區(qū)的運(yùn)營小姐姐,要了 100 元代金券,然后在騰訊 HAI 上創(chuàng)建了一臺 Windows Server 實(shí)例,在上面部署了 DeepSeek:

后來這 100 元被我霍霍完之后,我也不好意思再去要代金券了,如今我又轉(zhuǎn)戰(zhàn) DeepSeek 官方的 API 了。

不過在 Word 里調(diào)用 DeepSeek 官方 API 還是本地 API,步驟都基本一致,無非是 API url 修改一下就行了。本文給出的示例代碼里,調(diào)用的是官網(wǎng) API endpoint. 

實(shí)現(xiàn)思路就是在 Word 里創(chuàng)建一個(gè)宏,把調(diào)用 DeepSeek API 的 Visual Basic 代碼,寫在這個(gè)宏里。

我們首先需要把 Word 里創(chuàng)建宏的入口放出來,讓它顯示在 Word 的工具欄里。

在 Word 的 File->Options 選項(xiàng)里,選擇 Customize Ribbon,將 Developer 標(biāo)簽從左側(cè)移到右側(cè),然后在 Developer 標(biāo)簽下新建一個(gè) Group,隨便起個(gè)名稱。

一會兒我們要把創(chuàng)建好的宏,放到這個(gè) Group 里去。

接下來我們就能在工具欄里看到 Developer 了,點(diǎn)擊 Visual Basic,新建一個(gè) Module:

下一步是給這個(gè) Module 填充 Visual Basic 代碼。

代碼從哪來?肯定不用自己寫。再次祭出 Trae(筆者現(xiàn)在是越來越懶了):

發(fā)出指令:

我要在 Word 里使用 Visual Basic, 調(diào)用 DeepSeek V3 API. API 輸入,是用戶在 Word 里選中的一段文本。

Trae 很快給我返回了下面的 Visual Basic 代碼,除了將代碼里的 API key place holder 去掉之外,其他的原封不動(dòng)。

把 Trae 寫好的 Visual Basic,粘貼到新建的 Module 中。

Function CallDeepSeekAPI(api_key As String, inputText 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./chat/completions"    SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are word writting 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 WithIf status_code = 200 Then    CallDeepSeekAPI = response    Else      CallDeepSeekAPI = "Error: " & status_code & " - " & response End If    Set Http = NothingEnd FunctionSub 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 = "<此處粘貼你自己的 DeepSeek API Key>"    If api_key = "" Then       MsgBox "Please enter the API key."      Exit Sub    ElseIf Selection.Type <> wdSelectionNormal Then       MsgBox "請?jiān)?Word 里選中一段文字"     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))        response = Replace(response, "\n", vbCrLf)               response = Replace(response, "\n", vbCrLf)        response = Replace(response, "*", "")        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 IfEnd Sub

把上面的代碼保存到 Module 之后,在 Word Customize Ribbon 里,把宏配置到自定義組里,隨便維護(hù)一個(gè)圖標(biāo)即可。

上面的 Visual Basic 定義了名叫 DeepSeekV3 的 subroutine,同理我們可以讓 Trae 再寫一個(gè)調(diào)用開啟了 DeepThink(R1) 模式的 DeepSeek API,步驟類似,這里不再贅述。 

最后測試一下,在 Word 里隨便寫一句話,選中之后,點(diǎn)擊 Developer 的 DeepSeekV3, 這樣我們剛才給這個(gè)選項(xiàng)分配的 Visual Basic 實(shí)現(xiàn)的同名 subroutine 就會執(zhí)行:

API 調(diào)用的 response,自動(dòng)填充到了 Word 里。

筆者對于 Visual Basic 的語法是一竅不通,但這并不會妨礙我使用它編寫代碼來調(diào)用 DeepSeek API,這就是 AI 的力量。

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多