karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.04k stars 113 forks source link

ChatGPT Error: HTTP/2 400 #144

Closed Enrico68 closed 6 months ago

Enrico68 commented 7 months ago

Hello,

My program was running smoothly until today when I encountered the following error. Here's the backtrace:

HTTP/2 400 
date: Thu, 07 Dec 2023 08:51:29 GMT
content-type: application/json; charset=utf-8
content-length: 443
vary: Origin
x-request-id: 00cd5ec1499a7626fd63f55bf811e895
strict-transport-security: max-age=15724800; includeSubDomains
cf-cache-status: DYNAMIC
set-cookie: __cf_bm=cBPfBmxIb8M7CpCjxIFF4DqoZZv3xd1FbvG4CtmE0kU-1701939089-0-AS93XD+aCRzmyOAzDK2Y5zV8EuWsNBtutfMer/iAvBSOJzjvhx9ujIAvQP8C8C8fSvRIu37xcoEz33X5LJqK7LY=; path=/; expires=Thu, 07-Dec-23 09:21:29 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
set-cookie: _cfuvid=wJ3exPA1zv2NrH69EvYcgSE4eHBG_ICSAkJzWP_lrRo-1701939089218-0-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
server: cloudflare
cf-ray: 831b86eabc190e2a-MXP
alt-svc: h3=:443; ma=86400

{
    "error": {
        "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
(8eb16c09838c9d1924d100e80a71220c . 777)

Is this error related to OpenAI or the package itself? Could be that the response indicates a failure to parse the JSON body of the request, suggesting a potential issue with how the HTTP library is used?

Enrico

karthink commented 7 months ago

For a request that fails, could you run the following (using M-x eval-expression or equivalent):

(gptel-curl--get-args (gptel--create-prompt) "abcdefgh")

(You would run this instead of gptel-send)

And paste the result here? We can look at the HTTP payload that is sent to ChatGPT.

Important: This data will contain your API key -- please remove/obfuscate it before pasting.

Enrico68 commented 7 months ago

This is the response:

("--location" "--silent" "--compressed" "--disable" "-XPOST" "-w(abcdefgh . %{size_header})" "-m300" "-D-" "-d{\"model\":\"gpt-3.5-turbo\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a large language model living in Emacs and a helpful assistant. Respond concisely.\"},{\"role\":\"user\",\"content\":\"(gptel-curl--get-args (gptel--create-prompt) \\\"abcdefgh\\\")\"}],\"stream\":true,\"temperature\":1.0}" "-HContent-Type: application/json" "-HAuthorization: Bearer ------------------
Max-Jacobi commented 6 months ago

I got the same error. For me it was because I retrieved the api key from gnu pass which appended a newline to the key. So the error about the JSON formatting was quite misleading in my case.

karthink commented 6 months ago

This is the response:

("--location" "--silent" "--compressed" "--disable" "-XPOST" "-w(abcdefgh . %{size_header})" "-m300" "-D-" "-d{\"model\":\"gpt-3.5-turbo\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a large language model living in Emacs and a helpful assistant. Respond concisely.\"},{\"role\":\"user\",\"content\":\"(gptel-curl--get-args (gptel--create-prompt) \\\"abcdefgh\\\")\"}],\"stream\":true,\"temperature\":1.0}" "-HContent-Type: application/json" "-HAuthorization: Bearer ------------------

Why is the content being sent to ChatGPT the elisp command I suggested to run?

Enrico68 commented 6 months ago

The problem was related to the API key. Initially, I encountered an error while attempting to retrieve the key from the authpass file. Due to my own oversight, I didn't thoroughly search for a solution. However, setting the API key directly as a variable resolved the issue entirely. If you concur with this approach, I will proceed to close the issue.