Ask ChatGPT to answer a question using curl command.

By | June 13, 2023

It is my beginning trail of ChatGPT (Generative pre-trained transformer) from OpenAI. It is possible to chat with ChatGPT there:
However the main goal of this post to ask ChatGPT by sending raw HTTP request. To get access to OpenAI API you need to generate security key using this link: The security key is sent as value of Authorization header of HTTP POST request to OpenAI server. This is example how to send question to ChatGTP using curl command from Linux terminal.
The fist step assign secret key to OPENAI_API_KEY environmental variable:


The question to ChatGPT is “What are 10 biggest asteroids in Solar system?” and HTTP request looks like:

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "What are 10 biggest asteroids in Solar system?",
"temperature": 1,
"max_tokens": 256

The used model to generate an answer is text-davinci-003, temperature is parameter which controls randomness, max_tokens parameter specifies the maximum number of tokens which is used in the request, more tokens look better but more expensive.

The raw JSON response from server is:

  "created": 1686668294,
  "choices": [
      "text":"\n\n1. Ceres\n2. Vesta\n3. Pallas\n4. Hygiea\n5. Interamnia\n6. Euphrosyne\n7. Davida\n8. Eunomia\n9. Flora\n10. Hebe",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 61,
    "total_tokens": 70

The response must be parsed to retrieve user friendly answer:
1. Ceres
2. Vesta
3. Pallas
4. Hygiea
5. Interamnia
6. Euphrosyne
7. Davida
8. Eunomia
9. Flora
10. Hebe

Leave a Reply

Your email address will not be published. Required fields are marked *