我是一个word二次开发人员,局域网内已部署deepseek-R1的70b版大模型,api地址为10.78.3.176:8080/v1/chat/completions,我需要在二次开发插件时,调用大模型,对word中选中的文字进行分析,使用VSTO VB.net代码如何实现?
时间: 2025-03-02 19:07:29 浏览: 89
要在Word VSTO插件中使用VB.NET代码调用部署在局域网内的DeepSeek大模型API,并对选中的文本进行分析,你可以按照以下步骤操作:
### 步骤概述
1. **获取用户选择的文本**:从当前打开的文档中获取用户选中的内容。
2. **构建HTTP请求**:构造一个POST请求发送到指定的API地址(`http://10.78.3.176:8080/v1/chat/completions`),并包含要分析的文本数据。
3. **处理响应结果**:接收来自服务器返回的数据,并将其显示给用户或进一步处理。
### 实现示例
```vb.net
Imports System.Net.Http
Imports Newtonsoft.Json.Linq
Public Class ThisAddIn
Private Async Sub AnalyzeSelectedText()
' 获取活动文档和选区
Dim doc As Word.Document = Globals.ThisAddIn.Application.ActiveDocument
Dim selection As String = DirectCast(Globals.ThisAddIn.Application.Selection.Range.Text, Object)
If Not String.IsNullOrEmpty(selection) Then
Try
Using client As New HttpClient()
' 设置API URL
Dim apiUrl As String = "http://10.78.3.176:8080/v1/chat/completions"
' 准备JSON格式的消息体
Dim requestBody As JObject = New JObject(
New JProperty("messages", New JArray(New JObject(
New JProperty("role", "user"),
New JProperty("content", selection)
)))
)
' 发送Post请求
Dim responseMessage As HttpResponseMessage = Await client.PostAsync(apiUrl, New StringContent(requestBody.ToString(), Encoding.UTF8, "application/json"))
If responseMessage.IsSuccessStatusCode Then
Dim jsonResponseString As String = Await responseMessage.Content.ReadAsStringAsync()
' 解析返回的结果 (假设我们只需要第一个完成的内容部分)
Dim jsonResponseObject As JObject = JObject.Parse(jsonResponseString)
Dim completionResult As String = CType(jsonResponseObject.SelectToken("choices[0].message.content"), String)
MessageBox.Show($"Analysis Result:\n{completionResult}")
Else
Throw New Exception($"Error calling API with status code {responseMessage.StatusCode}: {Await responseMessage.Content.ReadAsStringAsync()}")
End If
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("No text selected.")
End If
End Sub
End Class
```
请注意,在实际项目中你需要添加错误处理机制、日志记录功能等来保证系统的稳定性和安全性。同时考虑到网络延迟等因素影响用户体验的问题也应纳入考量范围之内。
此外还要安装Newtonsoft.Json NuGet包以便于解析Json字符串;如果你的应用程序运行环境不允许直接访问外部网络,则需确保代理设置正确无误。
阅读全文
相关推荐

















