๋ณธ๋ฌธ

[์‹ค์Šต] ๋ฌธ์„œ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œ๊ฐํ™” ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ

โœ…  ์‹ค์Šต ์ค€๋น„๋ฌผ: apikey

https://server-engineer.tistory.com/983

 

[์‹ค์Šต] OpenAI apikey ๋ฐœ๊ธ‰ ๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ

1. ํšŒ์›๊ฐ€์ž…ํ•˜๊ธฐ ๋จผ์ € OpenAI ํ”Œ๋žซํผ์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. (๋ฐ”๋กœ๊ฐ€๊ธฐ) ํ™ˆํŽ˜์ด์ง€์—์„œ ํšŒ์›๊ฐ€์ž… ์ ˆ์ฐจ๋ฅผ ์™„๋ฃŒํ•ด์ฃผ์„ธ์š”. 2. API ํ‚ค ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ ๋กœ๊ทธ์ธ ํ›„, ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ์žˆ๋Š” 'View API Keys'๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ๋ง

server-engineer.tistory.com


โœ…  ์‹ค์Šต ์ž๋ฃŒ
https://github.com/i-am-shuan/learn-openai/tree/main/python-chatgpt/assistants_api

 

โœ…  ์„œ๋น„์Šค ์„ค๋ช…

์ด ํ”„๋กœ๋žจ์€ OpenAI์˜ GPT-4์™€ ์—ฐ๋™ํ•˜์—ฌ CSV ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ƒ์œ„ 10๊ฐœ ๊ธฐ์—…์˜ ๋งค์ถœ์„ ์‹œ๊ฐํ™”ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž๋™ํ™”ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ์‹œ๊ฐํ™” ์ž‘์—…์„ ์ž๋™ํ™”ํ•˜๋ฉฐ, ํŠนํžˆ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์™€ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ์— ๋Œ€์‘ํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

  • ์ฝ”๋“œ ํ•ด์„๊ธฐ(Code Interpreter)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Assistants API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒŒ๋“œ๋ฐ•์Šค ์‹คํ–‰ ํ™˜๊ฒฝ์—์„œ Python ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด ๋„๊ตฌ๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ์™€ ํฌ๋งท์˜ ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ์™€ ๊ทธ๋ž˜ํ”„ ์ด๋ฏธ์ง€๊ฐ€ ํฌํ•จ๋œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


1. ๋™์ž‘ ๊ณผ์ •

 

  • 1๋‹จ๊ณ„: ํ™˜๊ฒฝ ์„ค์ •๊ณผ ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
    • ๋จผ์ €, OPENAI_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ API ํ‚ค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” OpenAI ์„œ๋น„์Šค์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ธ์ฆ ํ‚ค์ž…๋‹ˆ๋‹ค.
      OpenAI() ํด๋ผ์ด์–ธํŠธ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ OpenAI API์™€์˜ ์—ฐ๊ฒฐ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
  • 2๋‹จ๊ณ„: ์ฐธ์กฐ ํŒŒ์ผ ์—…๋กœ๋“œ
    • client.files.create ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 'sample/fortune_1000_revenue_2022.csv' ํŒŒ์ผ์„ OpenAI ์„œ๋ฒ„์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ ๋ถ„์„ ๋Œ€์ƒ์ธ ํฌ์ถ˜ 1000 ๊ธฐ์—…์˜ 2022๋…„ ๋งค์ถœ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์—…๋กœ๋“œ๋œ ํŒŒ์ผ์˜ ID๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด ID๋Š” ํ›„์† ๋‹จ๊ณ„์—์„œ ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • 3๋‹จ๊ณ„: ์–ด์‹œ์Šคํ„ดํŠธ ์ƒ์„ฑ
    • client.beta.assistants.create ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ์–ด์‹œ์Šคํ„ดํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ์–ด์‹œ์Šคํ„ดํŠธ๋Š” CSV ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ์— ์ตœ์ ์œผ๋กœ ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•œ ์ง€์‹œ์‚ฌํ•ญ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
      ์ƒ์„ฑ๋œ ์–ด์‹œ์Šคํ„ดํŠธ๋Š” ์ฝ”๋“œ ํ•ด์„(code_interpreter) ๋ฐ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰(retrieval) ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—…๋กœ๋“œํ•œ ํŒŒ์ผ ID๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • 4๋‹จ๊ณ„: ์Šค๋ ˆ๋“œ ์ƒ์„ฑ ๋ฐ ๋ฉ”์‹œ์ง€ ์ „์†ก
    • client.beta.threads.create ๋ฉ”์„œ๋“œ๋กœ ์ƒˆ๋กœ์šด ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
      ์ƒ์„ฑ๋œ ์Šค๋ ˆ๋“œ์— ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜์—ฌ, ์—…๋กœ๋“œ๋œ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•œ ์ƒ์œ„ 10๊ฐœ ๊ธฐ์—…์˜ ๋งค์ถœ ์‹œ๊ฐํ™”๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
  • 5๋‹จ๊ณ„: ์‹คํ–‰ ๋ช…๋ น ๋ฐ ์ƒํƒœ ํ™•์ธ
    • client.beta.threads.runs.create ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, ์–ด์‹œ์Šคํ„ดํŠธ์—๊ฒŒ ํŠน์ • ์ง€์‹œ์‚ฌํ•ญ์„ ์‹คํ–‰ํ•˜๋„๋ก ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
    • ์‹คํ–‰ ์ƒํƒœ๋ฅผ ์ฃผ๊ธฐ์ ์œผ๋กœ ํ™•์ธํ•˜์—ฌ, ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ๊ฐ€ 'completed'๊ฐ€ ๋˜๋ฉด, ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • 6๋‹จ๊ณ„: ๊ฒฐ๊ณผ ๋ฉ”์‹œ์ง€ ๊ฒ€์ƒ‰
    • client.beta.threads.messages.list ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šค๋ ˆ๋“œ์—์„œ ์ƒ์„ฑ๋œ ๋ฉ”์‹œ์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์–ด์‹œ์Šคํ„ดํŠธ๊ฐ€ ์ˆ˜ํ–‰ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ์‹œ๊ฐํ™” ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 7๋‹จ๊ณ„: ์‹œ๊ฐํ™” ํŒŒ์ผ ์ €์žฅ
    • ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ๋Š” ์–ด์‹œ์Šคํ„ดํŠธ๊ฐ€ ์ƒ์„ฑํ•œ ๋งค์ถœ ์‹œ๊ฐํ™” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋กœ์ปฌ ์‹œ์Šคํ…œ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. client.files.content ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•˜๊ณ , ์ด๋ฅผ ๋ฐ”์ดํŠธ ํ˜•์‹์œผ๋กœ ๋กœ์ปฌ ํŒŒ์ผ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

2. ์‹คํ–‰ ๊ฒฐ๊ณผ

 

 

* p.s. OpenAI Assistants API: https://platform.openai.com/docs/assistants/overview

๊ณต์œ 

๋Œ“๊ธ€