It is my beginning trail of ChatGPT (Generative pre-trained transformer) from OpenAI. It is possible to chat with ChatGPT there: https://chat.openai.com.
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: https://platform.openai.com/account/api-keys. 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:
export OPENAI_API_KEY=sk-FAKESecretKeyGqOYDpaT3BlbkFJBMADdIKLR5Ecu0iRx6HW |
The question to ChatGPT is “What are 10 biggest asteroids in Solar system?” and HTTP request looks like:
curl https://api.openai.com/v1/completions -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:
{ "id":"cmpl-7QzlGBNqcZy1f5jJHAq2mkJyErW1U", "object":"text_completion", "created": 1686668294, "model":"text-davinci-003", "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