๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

[API] ChatGPT Python์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ - Part.2

by nowgeun 2023. 3. 8.
728x90

ํšŒ์‚ฌ ์—…๋ฌด์— ์น˜์—ฌ ์ •๋ง ์˜ค๋žœ๋งŒ์— ํฌ์ŠคํŒ…์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž...

 

๋ช‡ ๋‹ฌ ์ „ ์ฏค์— ChatGPT ๊ด€๋ จ ํฌ์ŠคํŒ…์„ ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹œ์—๋Š” API๊ฐ€ ๊ณต๊ฐœ๋˜์ง€ ์•Š์•„ GPT 3.0 ๋ฒ„์ „์ธ InstructGPT๋กœ ๋Œ€์‹  ์‚ฌ์šฉ์„ ํ–ˆ์—ˆ๊ณ , ๋‹ค๋ฅธ ๋ถ„๋“ค๋„ Reverse Engineering์„ ํ•˜๊ฑฐ๋‚˜ ๋ธŒ๋ผ์šฐ์ € ์„ธ์…˜์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ์„ ํ•ด์™”๋˜ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด์ œ ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค.

 

ChatGPT์˜ API ๋ฒ„์ „์ด 3์›” 1์ผ์— ๊ณต๊ฐœ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

Introducing ChatGPT and Whisper APIs

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

openai.com

์ด์ œ ๋ˆ„๊ตฌ๋‚˜ ์†์‰ฝ๊ฒŒ ๋‹จ ์„ธ ์ค„์˜ ์ฝ”๋“œ ๋งŒ์œผ๋กœ ChatGPT๋ฅผ Python์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(TMI: ChatGPT ์˜†์— ์žˆ๋Š” Whisper์€ ์Œ์„ฑ์„ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” Speech-to-text (STT) ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค)

 

๊ตฌํ˜„ํ•˜๊ธฐ์— ์•ž์„œ ๋‘ ๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

1. OpenAI API Key

2. openai ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ (๋ฒ„์ „ 0.27.0 ์ด์ƒ)  

    ใ„ด ์ด๋ฏธ ์„ค์น˜๋ฅผ ํ•˜์…จ๋‹ค๋ฉด prompt ์—์„œ pip install --upgrade openai  ๋กœ ๋ฒ„์ „ ์—…๊ธ€์„ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š” ๋ฐฉ๋ฒ•๊ณผ ์„ค์น˜ ๋ฐฉ๋ฒ•์€ ์ œ ์ด์ „ ํฌ์ŠคํŒ…์— ์ƒ์„ธํžˆ ์ ์–ด๋†“์•˜์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค ๐Ÿ‘‡๐Ÿ‘‡

 

[API] ChatGPT Python์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ - Part.1 (์ˆ˜์ •)

[์ˆ˜์ • 2023.02.04] ๋ ˆ๋”ง์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธ€์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์š”์•ฝ์„ ํ•˜์ž๋ฉด ChatGPT์™€ text-davinci-003 ๋ชจ๋ธ์€ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. text-davinci-003์€ InstructGPT์˜ ๋ชจ๋ธ๋กœ ์ง€์‹œ์‚ฌํ•ญ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์ตœ์ ํ™”๊ฐ€ ๋œ

jakely.tistory.com

 

Python์œผ๋กœ ๊ตฌํ˜„

์ฝ”๋“œ๋Š” ์•„๋ž˜๊ฐ€ ๋์ž…๋‹ˆ๋‹ค. 

 

import openai

YOUR_API_KEY = '๋ฐœ๊ธ‰๋ฐ›์€ Openai API ํ‚ค'

openai.api_key = YOUR_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)

 

openai.ChatCompletion.create() ๋ฉ”์„œ๋“œ๋Š” role (์—ญํ• ), ๊ทธ๋ฆฌ๊ณ  content (์งˆ๋ฌธ)์ด ๋“ค์–ด๊ฐ„ dictionary๋ฅผ list์˜ ํ˜•ํƒœ๋กœ input์„ ๋ฐ›๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

- content ๋ถ€๋ถ„์— ์›ํ•˜๋Š” ์งˆ๋ฌธ์„ ์ง‘์–ด๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

- role์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ์€ “system”, “user”, “assistant”  ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์€ json ํ˜•ํƒœ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\nAhoy, me hearties! Have ye heard about the ChatGPT API, the greatest treasure in all the seven seas of coding? This be the tool fer all ye swashbucklers and landlubbers who be wantin' to add a bit o' chat functionality to yer apps and websites.\n\nWith the ChatGPT API, ye can integrate text and voice chat, as well as language translation capabilities, into yer projects. Whether ye be needin' to connect with yer crew on a ship, or reach out to customers from far-off lands, this be the solution for yer communication needs.\n\nAnd this API be so easy to use, even a scallywag with little coding knowledge can set it up in no time! The ChatGPT API be a true treasure trove for any savvy developer, and it be free to boot. So what be ye waitin' for, me mateys? Hoist the Jolly Roger and set sail to the ChatGPT API today!",
        "role": "assistant"
      }
    }
  ],
  "created": 1678274175,
  "id": "chatcmpl-6rm4Fd3aCEgglRqghwLk84SRJnv6S",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 206,
    "prompt_tokens": 23,
    "total_tokens": 229
  }
}

 

.py ํŒŒ์ผ๋กœ ๊ตฌํ˜„

์‚ฌ์šฉ ํŽธ์˜์„ฑ์„ ์œ„ํ•ด์„œ .py ํŒŒ์ผ๋กœ๋„ ๊ตฌํ˜„์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค:

ํ•œ๊ธ€๋กœ ์งˆ๋ฌธํ•˜๋ฉด ์ธ์ฝ”๋”ฉ์œผ๋กœ ๊นจ์ ธ์„œ ๋‚˜์˜ค๋Š” ํ˜„์ƒ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, utf-8๋กœ ๋‹ค์‹œ ์ธ์ฝ”๋”ฉ์„ ํ•œ ๋’ค ๋””์ฝ”๋”ฉ์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

 

import openai
import argparse

YOUR_API_KEY = '๋ฐœ๊ธ‰๋ฐ›์€ Openai API ํ‚ค'

def chatGPT(prompt, API_KEY=YOUR_API_KEY):
    
    # set api key
    openai.api_key = API_KEY

    # Call the chat GPT API
    completion = openai.ChatCompletion.create(
      model="gpt-3.5-turbo", 
      messages=[{"role": "user", "content": prompt}]
    )
    # resolve encoding issue 
    return completion['choices'][0]['message'].encode('utf-8').decode().strip()

def main():
    # ์ง€๋ฌธ ์ž…๋ ฅ ๋ž€
    while True:
        prompt = input("Insert a prompt: ")
        if prompt.lower() == 'exit':
            break
        print(chatGPT(prompt))
        print('='*60)
        print('='*60)
        print('\n')

if __name__ == '__main__':
    main()

์‹คํ–‰ ์˜์ƒ

๋ชจ๋“  ์—…๊ณ„์—์„œ ChatGPT๋ฅผ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ• ์ง€ ๊ณ ๋ฏผ ์ค‘์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ์—๋Š” ๋‹ต๋ณ€์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฆฌ์Šคํฌ๋ฅผ ํ’€์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์„œ๋น„์Šค๋‚˜ ์‹ ๊ทœ ์„œ๋น„์Šค๋ฅผ ๊ธฐํšํ•˜๋Š” ๋ฐ์—๋Š” ์–ด๋Š์ •๋„ ์–ด๋ ค์›€์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•ž์œผ๋กœ ChatGPT๊ฐ€ ์„ธ์ƒ์— ์–ด๋–ค ๋ณ€ํ™”๋ฅผ ๊ฐ€์ ธ์˜ฌ์ง€๋Š” ๋‘๊ณ ๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์‹ ๊ธฐ์ˆ ์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ๋„ ๋ฌด์ฒ™ ์–ด๋ ต์ง€๋งŒ, ํ˜„์กดํ•˜๋Š” ๊ธฐ์ˆ ์„ ์ ์žฌ์ ์†Œ์— ์ž˜ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ ๋˜ํ•œ ์–ด๋ ต๋‹ค๋Š” ๊ฒƒ์„ ์š”์ฆ˜ ๋Š๋ผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.  

 

์ œ ํฌ์ŠคํŒ…์ด ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋„์›€์ด ๋˜์—ˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๐Ÿ˜

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€