国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

word 宏關(guān)于chatGPT API接口異常返回

2023-04-04 21:02 作者:達到法定  | 我要投稿

代碼參考up主小衛(wèi)是David

使用效果,API請求異常后,返回報錯提醒




Sub chatGPT()

'

' chatGPT 宏

'

'


? ? Dim selectedText As String

? ? Dim apiKey As String

? ? Dim response As Object, re As String

? ? Dim midString As String

? ? Dim ans As String

? ??

? ? On Error GoTo ErrorHandler

? ??

? ? If Selection.Type = wdSelectionNormal Then

? ? ? ? selectedText = Selection.Text

? ? ? ? selectedText = Replace(selectedText, ChrW$(13), "")

? ? ? ??

? ? ? ? apiKey = "your_api_key"

? ? ? ? URL = "https://api.openai.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"":""gpt-3.5-turbo"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & """}],""temperature"":0.7}"

? ? ? ??

? ? ? ? If response.Status = 200 And Len(response.responseText) > 0 Then

? ? ? ? ? ? re = response.responseText

? ? ? ? ? ? midString = Mid(re, InStr(re, """content"":""") + 11)

? ? ? ? ? ? ans = Split(midString, """")(0)

? ? ? ? ? ? ans = Replace(ans, "\n", "")

? ? ? ? ? ? Selection.Text = selectedText & vbNewLine & ans

? ? ? ? Else

? ? ? ? ? ? MsgBox "請求失敗,響應(yīng)內(nèi)容為:" & vbNewLine & response.responseText, vbExclamation, "請求失敗"

? ? ? ? End If

? ? ? ??

? ? Else

? ? ? ? Exit Sub

? ? ? ??

? ? End If

? ??

? ? Exit Sub

? ??

ErrorHandler:

? ? MsgBox "發(fā)生錯誤:" & Err.Description, vbCritical, "運行時錯誤"

? ??

End Sub


word 宏關(guān)于chatGPT API接口異常返回的評論 (共 條)

分享到微博請遵守國家法律
尉犁县| 开封市| 大港区| 习水县| 饶阳县| 通许县| 清水县| 芜湖县| 广宗县| 西林县| 微博| 寿光市| 奇台县| 体育| 阳江市| 土默特左旗| 宜良县| 西城区| 旌德县| 西和县| 西充县| 麻城市| 沁水县| 安溪县| 明溪县| 武汉市| 霸州市| 汉川市| 平定县| 巴楚县| 金溪县| 泾川县| 乌兰察布市| 南陵县| 句容市| 嘉禾县| 怀仁县| 调兵山市| 侯马市| 汶上县| 柘城县|