Search code examples
rjsoncurlopenai-apigpt-3

Access OpenAI (json) API from R


I want to access the OpenAI API with the following curl command from R:

curl https://api.openai.com/v1/engines/davinci/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"prompt": "This is a test", "max_tokens": 5}'

I think the curl package (on CRAN) will be the best option(?). I have never used this package so can anyone help me getting started with this simple call?


Solution

  • I created an R package named "openapi" (https://github.com/zhanghao-njmu/openapi), which supports all OpenAI APIs and can generate streaming returns (currently, other packages do not have good solutions), chatGPT app, and various RStudio add-ins. Welcome to use it.