@yuka-mori

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ExcelVBAからChat GPT APIを呼び出す際に発生するエラーの解消方法

解決したいこと

発生したエラーを解消するために、もう1つヘッダ情報をセットしたいがその方法がわからないため、教えていただきたいです。

参考にしたもの

以下のページで原因はわかったものの、解決方法がわかりませんでした。
原因は、'Content-Type: application/json'をもう1つヘッダ情報を追加してセットしなければならないということでした。
https://stackoverflow.com/questions/75567331/openai-gpt-3-api-error-you-must-provide-a-model-parameter

発生している問題・エラー

}

 "message": "you must provide a model parameter",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }

現在のコード

Public Sub HelloChatGPT()
    Dim apikey As String
    apikey = "sk----"
    
    Dim messages(0) As New Dictionary
    messages(0).Add "role", "user"
    messages(0).Add "content", " こんにちは、ChatGPT!"

    Dim data As New Dictionary
    data.Add "messages", messages
    data.Add "model", "gpt-3.5-turbo"
End Sub


Function HTTPGet2(sUrl As String, sRequestHeader As String) As String

    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long

    sCmd = "curl -X POST -H '" & sRequestHeader & "' " & sUrl
    sResult = execShell(sCmd, lExitCode)

    ' ToDo check lExitCode

    HTTPGet2 = sResult

End Function

Sub test2()
    Dim result As String
    Const apikey = "sk--------"
    result = HTTPGet2("https://api.openai.com/v1/chat/completions", "Authorization: Bearer  sk-------")
    Debug.Print result
End Sub

よろしくお願いいたします。

0 likes

1Answer

client.send JsonConverter.ConvertToJson(data)で渡したかったデータは、
↓これですね。dataでつながります。

Public Sub HelloChatGPT()
    Dim apikey As String
    apikey = "sk----"
    
    Dim messages(0) As New Dictionary
    messages(0).Add "role", "user"
    messages(0).Add "content", " こんにちは、ChatGPT!"

    Dim data As New Dictionary
    data.Add "messages", messages
    data.Add "model", "gpt-3.5-turbo"
End Sub

これをjsonで書くと↓こうなります。

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {"role": "user", "content": "こんにちは、ChatGPT!"}
    ]
}

まずは、以下のコードを追加してください。

Function HelloChatGPT_json() As String
    HelloChatGPT_json = "{ ""model"": ""gpt-3.5-turbo"", ""messages"": [ {""role"": ""user"", ""content"": ""こんにちは、ChatGPT!""} ] }"
End Function

Function HTTPGet3(sUrl As String, sRequestHeader As String, sBody As String) As String

    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long

    sCmd = "curl -X POST -H 'Content-Type: application/json' -H '" & sRequestHeader & "' -d '" & sBody & "' " & sUrl
    Debug.Print "cmd →", sCmd
    sResult = execShell(sCmd, lExitCode)

    ' ToDo check lExitCode

    HTTPGet3 = sResult

End Function

次のtest3で実行します。

test3
Sub test3()
    Dim result As String
    Const apiKey = "sk-XXXXXX"
    Dim json_body As String: json_body = HelloChatGPT_json()
    result = HTTPGet3("https://api.openai.com/v1/chat/completions", "Authorization: Bearer " & apiKey, json_body)
    Debug.Print "response →", result
End Sub

自分のapiKeyで実行すると、下記エラーが返ります。課金ユーザしか使えないAPIですかね?

{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details.",
        "type": "insufficient_quota",
        "param": null,
        "code": "insufficient_quota"
    }
}
0Like

Comments

  1. @yuka-mori

    Questioner

    課金したら成功しました!本当に本当にありがとうございます!!

    cmd → curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer sk-----' -d '{ "model": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "Hello!ChatGPT!"} ] }' https://api.openai.com/v1/chat/completions
    response → {
    "id": "chatcmpl-81lmAzyMK1iYe3VdRHgmKjHSXA7hJ",
    "object": "chat.completion",
    "created": 1695432670,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
    {
    "index": 0,
    "message": {
    "role": "assistant",
    "content": "Hello! How can I assist you today?"
    },
    "finish_reason": "stop"
    }
    ],
    "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 9,
    "total_tokens": 22
    }
    }

  2. よかったです✌️

    下記サイトによると、
    『Chat Completions APIはステートレスなAPIであるため、連続したやり取りについての応答を生成する場合は今までの応答を全部messagesに含めて送る必要があります。』
    とありました。

    ご参考まで。

  3. @yuka-mori

    Questioner

    知らなかったです!大変助かります:relaxed:

Your answer might help someone💌