2 ๋ถ„ ์†Œ์š”

Chat GPT API

Chat GPT API

์ผ๋ฐ˜์ ์ธ api ๋Š” key ๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•„์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

https://platform.openai.com/docs/overview ์—์„œ key ๋ฐœ๊ธ‰ ๊ฐ€๋Šฅํ•จ.

key๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์œผ๋ฉด ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋‹ฌ์ด ๋‚˜์˜ค๋Š”๋ฐ, ๋ฐ˜๋“œ์‹œ ๋ณต์‚ฌํ•˜๊ณ  ์–ด๋”˜๊ฐ€์— ์ €์žฅํ•ด๋†”์•ผ ํ•จ. ๊ทธ๋Ÿฌ์ง€ ์•Š๊ณ  ๋ชจ๋‹ฌ์„ ๋„๋ฉด ๋‹ค์‹œ๋Š” key๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†์Œ

API ์‚ฌ์šฉํ•˜๊ธฐ (Python ๊ธฐ์ค€)

๋จผ์ € openai ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ api๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌผ๋ก  ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ์ง„ํ–‰ํ•˜๋Š” ํŽธ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰ ๋ฐฉ๋ฒ•

๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰ํ•˜๊ธฐ[macOs]

python -m venv venv
source venv/bin/activate

๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰ํ•˜๊ธฐ[windows]

python -m venv venv
cd venv/Scripts
./activate
pip install openai

์˜ˆ์‹œ ์ฝ”๋“œ (OpenAI ๋ฌธ์„œ์—์„œ ๊ฐ€์ ธ์˜จ ์ฝ”๋“œ)

import openai

openai.api_key = "YOUR_API_KEY"

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

output_text = response["choices"][0]["message"]["content"]
print(output_text)
  • openai.api_key : ์—ฌ๊ธฐ์— ์•„๊นŒ ๋ฐœ๊ธ‰๋ฐ›์€ api key๋ฅผ ๋ถ™์—ฌ๋„ฃ์–ด์ค€๋‹ค.
  • response
    • model : ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์€ ๋งŽ์ง€๋งŒ, 3.5 ํ„ฐ๋ณด๊ฐ€ ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ
    • messages : role ๋ถ€๋ถ„์— ๋ณด๋ฉด system, user, assistance ๊ฐ€ ์žˆ๋‹ค.
      • ์‹œ์Šคํ…œ : gpt์—๊ฒŒ ์–ด๋–ป๊ฒŒ ํ–‰๋™์„ ํ• ์ง€ ์ง€์ •ํ•ด๋†“๋Š” ๊ธฐ๋Šฅ (gpt์˜ act as ___ ์™€ ์œ ์‚ฌํ•œ ๋ช…๋ น)
      • ์‚ฌ์šฉ์ž : ์‚ฌ์šฉ์ž ์งˆ๋ฌธ, ์ด์ „ ๋Œ€ํ™”๋ฅผ ์ €์žฅํ•˜๊ณ  ์—ฐ์†์„ฑ ์œ ์ง€ โ†’ ์ด์–ด์ง€๋Š” ๋‹ต๋ณ€์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Œ
      • ๋ณด์กฐ์ž : ์งˆ๋ฌธ์„ ํ•˜๊ธฐ๋ณด๋‹จ, ์ด ์ „ ๋Œ€ํ™”์˜ ๋‚ด์šฉ์„ ์ €์žฅ ํ•˜๊ณ , ์—ฐ์†์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์˜ต์…˜ โ†’ ์ด์–ด์ง€๋Š” ๋‹ต๋ณ€์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Œ

์ค‘์š”ํ•œ ์ž…๋ ฅ ๊ฐ’

  • temperature
    • 0 ~ ๋ฌดํ•œ๋Œ€์˜ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง, ๊ธฐ๋ณธ ๊ฐ’์€ 0.7
    • ๊ฐ’์ด ๋†’์„ ์ˆ˜๋ก ๋ชจ๋ธ์ด ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์žฅ์ด ๋‹ค์–‘ํ•ด์ง โ†’ ๋‹ค์–‘ํ•œ ์„ ํƒ์ง€๋ฅผ ํ†ตํ•œ ๋‹ต๋ณ€
    • ๊ฐ’์ด ๋‚ฎ์„ ์ˆ˜๋ก ์ผ๊ด€์„ฑ ์žˆ๋Š” ๋ฌธ์žฅ์ด ์ƒ์„ฑ๋จ โ†’ ๊ฐ€๋Šฅํ•œ ์„ ํƒ์ง€ ์ค‘, ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ๋‹ต๋ณ€
    • ์ผ๋ฐ˜์ ์œผ๋กœ 0.5 ~ 1.0 ์‚ฌ์ด์˜ ๊ฐ’์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•จ
    • ์ •๋ณด๊ฐ€ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ : ๋‚ฎ์€ ๊ฐ’
    • ์ฐฝ์˜์„ฑ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ : ๋†’์€ ๊ฐ’
  • max_tokens
    • ์ƒ์„ฑ๋˜๋Š” ํ…์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด ์ง€์ •
    • ๊ธฐ๋ณธ ๊ฐ’์€ 256
    • ์ตœ๋Œ€ ๊ฐ’์€ 2048

๋‚ด๊ฐ€ ์‚ฌ์šฉํ•  ์ฝ”๋“œ

import openai

 # API KEY ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”.
openai.api_key = ""

# ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›์„ message ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
messages = []

while True:
  # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  content = input("input content : ") 
  print("input content : ", content)

  # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ messages์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
  messages.append({"role": "user", "content": f"{content}"})

  completion = openai.ChatCompletion .create(
    model="gpt-3.5-turbo",
    messages=messages
  )

  # gpt๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  # strip() : ์‘๋‹ต์ด ์˜ฌ ๋•Œ, ๋นˆ ๊ณต๊ฐ„ ์ œ๊ฑฐ
  assistance_content = completion.choices[0].message['content'].strip()

  # assistance์— ์ด์ „ ๋Œ€ํ™” ๋‹ต๋ณ€์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  messages.append({"role": "assistance", "content": f"{assistance_content}"})

  print(f'ChatGPT : {assistance_content}')
  • messages : ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ณผ ์‘๋‹ต์— ๊ด€ํ•œ ๋ฆฌ์ŠคํŠธ
  • completion : ์‚ฌ์šฉํ•  ๋ชจ๋ธ์„ ์ •ํ•˜๊ณ , messages ํ• ๋‹นํ•ด์„œ gpt์—๊ฒŒ ์ „์†ก
  • assistance_content : completion.choices[0].message['content']๋กœ ๋ฐ›์•„์˜จ ์‘๋‹ต์„ ์ €์žฅ.
    • strip() : ๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  ์‚ฌ์šฉ.
    • ์‘๋‹ต์ด ์˜ฌ ๋•Œ, ์• ๋งคํ•œ ๊ณต๋ฐฑ์ด ์ƒ๊ฒจ์„œ ์ง€์šฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•จ.
    • ์ด์ „ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์˜ ๋‹ต๋ณ€์„ ์ €์žฅํ•˜๋ฉด, gpt๊ฐ€ ์ด๊ฑธ ๊ธฐ์–ตํ•ด ๋‹ค์Œ ์งˆ๋ฌธ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Œ
  • messages.append({โ€œroleโ€: โ€œassistanceโ€, โ€œcontentโ€: f {assistance_content}โ€}) : assiatance_content๋ฅผ ์ถ”๊ฐ€ํ•จ -> ๋ฐฉ๊ธˆ ๋ฐ›์€ ๋‹ต๋ณ€์„ ๋‹ค์Œ ์งˆ๋ฌธ์— ์œ ํšจํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ

์ฐธ๊ณ ์ž๋ฃŒ

OpenAI API ๊ณต์‹ ๋ฌธ์„œ

์ฑ—GPT API ์‚ดํŽด๋ณด๊ธฐ

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ