OpenAI API curl

1 개요[ | ]

OpenAPI API curl
export OPENAI_API_KEY=<오픈AI_API_키>
curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},{"role":"user","content":"Compose a poem that explains the concept of recursion in programming."}]}'

2 오류[ | ]

키가 입력되지 않은 경우
testuser@localhost:~$ export OPENAI_API_KEY=''
testuser@localhost:~$ curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},{"role":"user","content":"Compose a poem that explains the concept of recursion in programming."}]}'
{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
쿼타가 부족한 경우
testuser@localhost:~$ export OPENAI_API_KEY='sk-moqUsWYAuw9yzb1dCfh3E5GjlnIKpMrt70vxINSUFFICIENT'
testuser@localhost:~$ curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},{"role":"user","content":"Compose a poem that explains the concept of recursion in programming."}]}'
{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
        "type": "insufficient_quota",
        "param": null,
        "code": "insufficient_quota"
    }
}

3 성공[ | ]

testuser@localhost:~$ export OPENAI_API_KEY='sk-moqUsWYAuw9yzb1dCfh3E5GjlnIKpMrt70vxuOPRTVt24Xcd'
testuser@localhost:~$ curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},{"role":"user","content":"Compose a poem that explains the concept of recursion in programming."}]}'
{
  "id": "chatcmpl-8d7N0y4gZIm87vSxzfcP40jHqDp4i",
  "object": "chat.completion",
  "created": 1704333814,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "In a realm of code, where pixels dance and play,\nA concept quite bewitching holds its sway.\nRecursion, oh recursion, is its name,\nA mystic power, lighting up the coding game.\n\nLike a mirror reflecting its own reflection,\nRecursion finds beauty in self-connection.\nA function, bold and daring, takes the stage,\nCalling itself, creating a recursive cage.\n\nThink of a Russian doll, nested with delight,\nA universe expanding, hidden from sight.\nA function, it hums and sings its own song,\nUnraveling mysteries, righting all that's wrong.\n\nThrough the looking glass, down a rabbit hole,\nRecursion enchants us, capturing the soul.\nWith each recursive call, a tale unfolds,\nA problem divided, as the story beholds.\n\nSometimes the task at hand seems overwhelming,\nLike a mountain to climb, far too unwavering.\nBut recursion arrives, a knight in shining code,\nDividing the challenge into bite-sized mode.\n\nBase case, ah the base case, a guiding light,\nA beacon in the darkness, leading us right.\nFor recursion, like a phoenix, must rise from the ash,\nTo break the chains that constrain and clash.\n\nA dance of stack frames, a ballet of grace,\nRecursion weaves a pattern in time and space.\nFrom mathematical puzzles to fractal art,\nIn recursion's embrace, we find a world apart.\n\nBut heed my words, dear coder, for a cautionary tale,\nRecursion may entangle, if you let it prevail.\nWith no termination, it spirals without end,\nA wild whirlwind, chaos it does send.\n\nSo wield recursion wisely, as a master of the craft,\nFor with great power comes a need to abstract.\nLet it guide your code, intricately entwined,\nIn its recursive glory, greatness you'll find.\n\nOh, recursion, my muse, in thy loops I lose,\nCreating wonders, unraveling the clues.\nA symphony of logic, harmonizing with grace,\nRecursion, my friend, forever shall we embrace."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 39,
    "completion_tokens": 418,
    "total_tokens": 457
  },
  "system_fingerprint": null
}

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}