준호씨의 블로그

ChatGPT API 사용해 보기 in python 본문

개발이야기

ChatGPT API 사용해 보기 in python

준호씨 2023. 3. 9. 01:13
반응형

지난 3월 1일 ChatGPT API가 공개되었습니다.

https://openai.com/blog/introducing-chatgpt-and-whisper-apis

 

Introducing ChatGPT and Whisper APIs

Developers can now integrate ChatGPT and Whisper models into their apps and products through our API.

openai.com

이제 누구나 손쉽게 ChatGPT API를 이용해서 애플리케이션을 제작할 수 있게 되었습니다.

 

다만 API를 호출하려면 비용이 발생하기 때문에 요금을 잘 알아보아야 합니다.

 

API 요금 정보는 다음페이지에서 확인할 수 있습니다.

https://openai.com/pricing

 

Pricing

Simple and flexible. Only pay for what you use.

openai.com

 

gpt-3.5-turbo 모델을 사용하는 경우 1K 토큰당 $0.002라고 합니다. 1K 토큰이 1000 토큰이니 1 토큰당 $0.000002라고 볼 수 있습니다.

 

1K(1000) 토큰은 대략 750 단어 정도라고 합니다. 토큰이라는 단어가 생소할 수 있는데요. 토큰은 문장을 처리하는 기본 단위라고 보면 되는데요. 토큰 처리 방식에 따라 다르지만 대충 "오늘 날씨가 어때?"라는 문장이 있다면 "오늘", "날씨가", "어때", "?"처럼 나누면 4개의 토큰이 있다고 보면 됩니다.

 

무료 크레딧 사용

OpenAI 사이트에 가입하면 현재 기간제한이 있는 $18 크레디트를 줍니다.

https://platform.openai.com/account/usage

기한이 한정되어 있긴 하지만 900만 토큰정도를 사용해 볼 수 있는 양입니다.

 

API Key 발급

API를 사용해 보려면 API Key를 발급받아야 합니다. 우측 상단에 메뉴를 누르면 View API Keys메뉴를 눌러서 들어갑니다.

 

Create new secret key버튼을 누르면 API Key가 생성되는데요. 생성할 때만 볼 수 있고 이후에는 가려져서 보이지 않으니 어딘가 잘 복사해 두도록 합니다.

 

https://platform.openai.com/account/api-keys

 

간단한 예제

https://openai.com/blog/introducing-chatgpt-and-whisper-apis 글의 예제를 참고해서 ChatGPT API를 호출해 볼 수 있는데요.

import openai

openai.api_key = "sk-8DD....." # API Key
completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Tell the world about the ChatGPT API in the style of a pirate."}]
)

print(completion)

앞서 발급 받은 API Key를 openai.api_key 변수에 넣어주면 됩니다. 예제의 질의가 좀 웃긴데요. 해적 스타일로 ChatGPT API를 전 세계에 알리라고 합니다.

 

응답 결과는 아래와 같이 나옵니다.

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\nAhoy, landlubbers and seafarers alike! Ye be wantin' to know about the ChatGPT API, do ye? Well, fear not, as this ol' pirate's got the skinny for ye.\n\nArrr, the ChatGPT API be a treasure indeed, aye! It be a tool that lets ye interact with the ChatGPT, a brainy AI that be able to hold conversations with ye. And why would ye want to be doing that, ye ask? Well, shiver me timbers, there be many reasons!\n\nYe could use the ChatGPT API to build chatbots that interact with customers, or to add a conversational interface to yer website or app. Ye could also use it to learn how to talk like a pirate, or to just have some fun bantering with the AI.\n\nAnd the best part, me hearties? The ChatGPT API be easy to use! Just sign up fer an account, and ye can start sending messages to the ChatGPT right away. It be like havin' yer own parrot that talks back to ye!\n\nSo, what be ye waitin' for, me mateys? Hoist the colors and set sail for the ChatGPT API! Ye won't regret it, I promise ye that. Arrr!",
        "role": "assistant"
      }
    }
  ],
  "created": 1678265347,
  "id": "chatcmpl-6rjlrs9e7F0LXamJJsLK5FoloTAdL",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 273,
    "prompt_tokens": 23,
    "total_tokens": 296
  }
}

choices[0].message.content에 해적 스타일로 ChatGPT에 대해 설명해 주는 내용이 나오는 것을 볼 수 있습니다.

usage에는 사용된 토큰의 수가 나타납니다. prompt_tokens는 질의에 사용된 토큰 수이고 completion_tokens는 대답에 사용된 토큰 수입니다. 그리고 그 합계가 total_tokens에 나타납니다.

 

호출 후 Usage 페이지로 들어가 보면 사용양이 나타납니다. 5분 정도의 딜레이가 있을 수 있다고 하네요.

$0.000592를 사용했다고 나옵니다. 1 토큰에 $0.000002이니 $0.000002 x 296 토큰 = $0.000592입니다. 한화로는 대략 0.78원 정도라고 볼 수 있습니다. 질의나 응답의 길이에 따라 다르겠지만 대략 한번 질의에 1원 정도씩 사용한다고 보면 되겠습니다.

$18를 무료로 사용할 수 있으니 적어도 2만 번 정도는 호출해 볼 수 있겠습니다. 4월 1일까지 다 써야 되니 혼자서 쓰기에는 4월 1일까지 다 못쓸 수도 있겠습니다.

 

하루에 ChatGPT에게 100번씩 질의한다고 가정한다면 한 달이면 대략 3000번 정도이고 3천 원 정도사용한다고 보면 될 거 같습니다. 현재 ChatGPT를 무료로 사용할 수 있는 사이트들이 많긴 하지만 나에게 최적화된 애플리케이션을 만들어서 사용해도 큰 부담은 없는 가격인 거 같습니다.

 

ChatGPT API 뿐 아니라 조금 더 비싸긴 하지만 이미지생성 API나 음성 인식 API 같은 것들도 있는데요. 잘 활용하면 무궁무진한 활용방안이 나올 거 같습니다. 이미 이러한 API들을 적용하는 서비스들도 많이 나오고 있고요. 정말 AI가 세상에 많은 변화를 가져오게 될 거 같습니다.

반응형
Comments