之前我曾利用Word的VBA宏接入ChatGPT 3.5的API。然而,由于OpenAI对中国大陆地区的限制,这一方案难以持续使用。随后出现的大型语言模型要么难以调用,要么收费昂贵。如今,随着DeepSeek的开源,结合Word这一办公常用场景,我决定将DeepSeek模型接入Word,实现文本生成和文本润色两大功能。
Vba code 文本生成模块代码 通过VBA调用英伟达的API(具体获取方式之前已提到多次不再赘述),采用非流式生成方式。用户只需选中文字并点击模块名称,稍等片刻即可生成结果。该模块基于推理文本模型,并保留了模型的思考过程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Sub DeepSeek() Dim selectedText As String Dim apiKey As String Dim response As Object , re As String Dim midString As String Dim ans As String If Selection.Type = wdSelectionNormal Then selectedText = Selection.Text selectedText = Replace(selectedText, ChrW$(13 ), "" ) apiKey = "your_api_key_here" URL = "https://integrate.api.nvidia.com/v1/chat/completions" Set response = CreateObject("MSXML2.XMLHTTP" ) response.Open "POST" , URL, False response.setRequestHeader "Content-Type" , "application/json" response.setRequestHeader "Authorization" , "Bearer " + apiKey response.Send "{""model"":""deepseek-ai/deepseek-r1"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & """}], ""temperature"":0.7}" re = response.responseText midString = Mid (re, InStr(re, """content"":""" ) + 11 ) ans = Split(midString, """" )(0 ) ans = Replace(ans, "\n" , "" ) Selection.Text = selectedText & vbNewLine & ans Else Exit Sub End If End Sub
文本润色模块 文本润色模块内置了提示词,大家可根据需要修改提示词,实现翻译、润色、续写等功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 If Selection.Type = wdSelectionNormal Then selectedText = Selection.Text selectedText = Replace(selectedText, ChrW$(13 ), "" ) apiKey = "your_api_key_here" URL = "https://integrate.api.nvidia.com/v1/chat/completions" polishPrompt = "请润色以上文字,要求语句通顺,条理清晰,专业而合理。" Set response = CreateObject("MSXML2.XMLHTTP" ) response.Open "POST" , URL, False response.setRequestHeader "Content-Type" , "application/json" response.setRequestHeader "Authorization" , "Bearer " + apiKey response.Send "{""model"":""deepseek-ai/deepseek-r1"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & """}, {""role"":""assistant"", ""content"":""" & polishPrompt & """}], ""temperature"":0.7}" re = response.responseText midString = Mid (re, InStr(re, """content"":""" ) + 11 ) ans = Split(midString, """" )(0 ) ans = Replace(ans, "\n" , "" ) Selection.Text = selectedText & vbNewLine & ans Else Exit Sub End If End Sub
宏的使用 宏的使用有很多方法,快捷键、放在功能区都行,这里以自定义功能区简单介绍。
添加宏
选项-信任中心-宏设置-启用所有宏-信任 vba
开启开发工具, 选项-自定义选项卡-勾选开发工具。
Word 工具栏点击“开发工具”选项卡。
点击“Visual Basic”。
右键模板下的“模块”,选择“插入模块”。
复制上述代码到新建的模块中。
自定义功能区
点击“文件”->“选项”->“自定义功能区”。
选择“宏”,在右侧自定义功能区新建选项卡和组。
将宏添加至组中,重命名并更换图标。
效果预览
小结 最近几天,我一直在探索DeepSeek的应用,深感其高性能和低成本对工作生活的普惠意义。对于个人和小企业而言,人工智能的应用应紧密结合自身场景,无需考虑在本地搭建模型。尽管DeepSeek模型开源,但在本地运行全功能模型成本较高。随着各大厂商的跟进,生成式模型可能成为基础设施,如同GitHub一样普及。我们需要做的是提前跑通整个流程,以便在新模型出现时能迅速应用。目前,DeepSeek虽强大,但运行速度仍有提升空间,尤其是RI推理模型。让我们保持热情,持续关注其发展。
BY 纯个人经验,如有帮助,请收藏点赞,如需转载,请注明出处。 微信公众号:环境猫 er CSDN : 细节处有神明 个人博客: https://maoyu92.github.io/
GPT3.5 与word的结合,提前享受office copilot_wordgpt-CSDN博客