之前跟大家分享了,如何調(diào)用Deepseek的API制作ExcelAI函數(shù),但是不少粉絲都反饋體驗(yàn)不好,主要還是反饋的結(jié)果太慢,數(shù)據(jù)量一大,就能不行了。
今天解決的方法來了,就是調(diào)用豆包的API,結(jié)果基本都是秒出,做到了跟常規(guī)函數(shù)幾乎一樣的速度,真的太爽了
一、找到豆包
想要調(diào)用豆包的API就需要通過火山引擎調(diào)用,這個(gè)就不再贅述了,之前都發(fā)過視頻了的,大家可以搜一下,注冊(cè)就是手機(jī)號(hào)注冊(cè),然后實(shí)名認(rèn)證就可以調(diào)用API了
來到首頁(yè)后,我們需要在右側(cè)點(diǎn)擊【模型廣場(chǎng)】有的找到【Doubao-1.5-lite-32k】然后點(diǎn)擊【查看詳情】
為什么要選擇【Doubao-1.5-lite-32k】這個(gè)模型呢,因?yàn)檫@個(gè)是輕量化的模型,主打低延遲,速度更快,我們使用ExcelAI函數(shù)的體驗(yàn)也會(huì)更好

二、調(diào)用API
點(diǎn)擊【查看詳情】后會(huì)來到一個(gè)新的窗口,我們需要在當(dāng)前的窗口中找到【推理】之后就會(huì)在右側(cè)看到一個(gè)窗口,如下截圖,我們獲取三處關(guān)鍵的數(shù)據(jù)
1.API KEY】這個(gè)需要自己創(chuàng)建下
2.【url它就是API】的地址,已經(jīng)在下圖標(biāo)注,記得全部復(fù)制
3.【Model】它就是模型的ID
獲取上方的三個(gè)關(guān)鍵數(shù)據(jù)后,就能做API的調(diào)用了

三、更改代碼
當(dāng)前的代碼我們需要修改3處,也正好對(duì)應(yīng)我們上一步獲取的三處,大家記得一定要全部替換下才能正確調(diào)用API,修改后使用這個(gè)AI函數(shù)了,下面是它的參數(shù)
=ExcelAI(需要處理的單元格,”你的需求”)
1.【你的API替換為】豆包的API KEY
2.【模型的URL地址】替換為豆包的url
3.【模型的ID】替換為豆包的模型ID
Function ExcelAI(TargetCell As Range, Question As String) As Variant
On Error GoTo ErrorHandler
Const API_KEY As String ="你的API" ' 需替換有效密鑰
Const API_URL As String ="模型的URL地址"
' 構(gòu)建安全請(qǐng)求
Dim safeInput As String
safeInput = BuildSafeInput(TargetCell.Text, Question)
' 發(fā)送API請(qǐng)求
Dim response As String
response = PostRequest(API_KEY, API_URL, safeInput)
' 解析響應(yīng)內(nèi)容
If Left(response, 5) ="Error"Then
ExcelAI = response
Else
ExcelAI = ParseContent(response)
End If
Exit Function
ErrorHandler:
ExcelAI ="Runtime Error: "& Err.Description
End Function
' 構(gòu)建安全輸入內(nèi)容
Private Function BuildSafeInput(Context As String, Question As String) As String
Dim sysMsg As String
If Len(Context) > 0 Then
sysMsg ="{""role"":""system"",""content"":""上下文:"& EscapeJSON(Context) &"""},"
End If
BuildSafeInput ="{""model"":""模型的ID"",""messages"":["& _
sysMsg &"{""role"":""user"",""content"":"""& EscapeJSON(Question) &"""}]}"
End Function
' 發(fā)送POST請(qǐng)求
Private Function PostRequest(apiKey As String, url As String, payload As String) As String
Dim http As Object
Set http = CreateObject("MSXML2.XMLHTTP")
On Error Resume Next
With http
.Open"POST", url, False
.setRequestHeader"Content-Type","application/json"
.setRequestHeader"Authorization","Bearer "& apiKey
.send payload
If Err.Number <> 0 Then
PostRequest ="Error: HTTP Request Failed"
Exit Function
End If
' 增加10秒超時(shí)控制
Dim startTime As Double
startTime = Timer
Do While .readyState < 4 And Timer - startTime < 10
DoEvents
Loop
End With
If http.Status = 200 Then
PostRequest = http.responseText
Else
PostRequest ="Error "& http.Status &": "& http.statusText
End If
End Function
' JSON特殊字符轉(zhuǎn)義
Private Function EscapeJSON(str As String) As String
str = Replace(str,"\", "\\")
str = Replace(str, """", "\""")
str = Replace(str, vbCr, "\r")
str = Replace(str, vbLf, "\n")
str = Replace(str, vbTab, "\t")
EscapeJSON = str
End Function
' 智能解析響應(yīng)內(nèi)容
Private Function ParseContent(json As String) As String
Dim regex As Object, matches As Object
Set regex = CreateObject("VBScript.RegExp")
' 增強(qiáng)版正則表達(dá)式
With regex
.Pattern = """content"":\s*""((?:\\""|[\s\S])*?)"""
.Global = False
.MultiLine = True
.IgnoreCase = True
End With
Set matches = regex.Execute(json)
If matches.Count > 0 Then
Dim rawText As String
rawText = matches(0).SubMatches(0)
' 反轉(zhuǎn)義處理
rawText = Replace(rawText, "\""", """")
rawText = Replace(rawText, "\\", "\")
rawText = Replace(rawText, "\n", vbCrLf)
rawText = Replace(rawText, "\r", vbCr)
rawText = Replace(rawText, "\t", vbTab)
ParseContent = rawText
Else
' 錯(cuò)誤信息提取
Dim errMatch As Object
regex.Pattern = """message"":\s*""(.*?)"""
Set errMatch = regex.Execute(json)
If errMatch.Count > 0 Then
ParseContent = "API Error:" & errMatch(0).SubMatches(0)
Else
ParseContent = "Invalid Response"
End If
End If
End Function
以上就是今天分享的全部?jī)?nèi)容,大家可以試一下~
熱門跟貼