|
先上代码,VBA连接Open API
- Public Function OpenAI(prompt As String) As String
- Dim url As String, apiKey As String
- Dim response As Object, json As String
- apiKey = "替换成你自己的key"
- url = "https://api.openai.com/v1/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"":""text-davinci-003"",""prompt"":""" & prompt & """,""max_tokens"":10}"
- json = response.responseText
- OpenAI = Split(Mid(json, InStr(json, """text"":""") + 8), """")(0)
- OpenAI = Replace(OpenAI, "\n", "")
- End Function
复制代码- Sub test()
- Dim t As String
- t = OpenAI("say hello to me")
- Debug.Print t
- End Sub
复制代码 说明一下:帖子发布时,还没测试成功。不挂梯子,报错:指定资源下载失败。挂梯子,到send这一句一直转圈圈
注册及申请key地址:https://platform.openai.com/account/api-keys。
亲测,国内163邮箱无法注册,我用的是谷歌gmail账号登录的。需要绑定手机号,而且必须是国外的
API注册成功后有赠送的18美金。用完需充值,非免费。所以代码中我把自己的apiKey删除了
|
|