ChatGPT API

I signed up for some kind of ChatGPT API service. OpenAI offers API code in a number of languages like Go, Python, Javascript... not C. I was looking at invoking Python scripts from C. It is possible through a library, or by fork + exec a shell script. But, populating some JSON templates and contacting ChatGPT through libcurl looks like the easiest thing to do.

Here is an example from the commandline:

curl -v https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" \
-H "Authorization: Bearer <arcane authorization here>
-d '{
  "model": "gpt-5-nano",
  "messages": [{"role": "user", "content": "In a few words, answer this question: does mario love the princess"}]
}'

The trouble is, the answer comes back: "Yes, he loves Princess Peach." (!)

Oh? Do we know that?

Now to embedding it in Brainhat. libcurl is available from https://curl.se/libcurl/ with little license restriction. There are non-blocking calls, which means I can ask ChatGPT a question and go away. It takes a long time to answer, typically.