๐ถ ์๋ฌ
โ๏ธ ์์ ํ๊ฒฝ
- Window (PowerShell)
- VSCode
- Python 3.10
โ๏ธ ์์ ๋ด์ฉ (OpenAI API ํ ์คํธ)
1. API Key ์์ฑ
2. ํ๊ฒฝ๋ณ์ ์ค์
setx OPENAI_API_KEY "your_api_key_here"
3. OpenAI API ์ค์น
pip install openaiโ
4. ์์ค ์ฝ๋ ์์ฑ (text_example.py)
from openai import OpenAI client = OpenAI() completion = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant."}, { "role": "user", "content": "Write a haiku about recursion in programming." } ] ) print(completion.choices[0].message)โ
5. ์คํ
python text_example.py .โ
6. ์๋ฌ ๋ฐ์
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variableโ
๐ถ ์กฐ์น
โ๏ธ ๋ด์ฉ
2๋ฒ ์์ ์์ ํ๊ฒฝ๋ณ์๋ก ์ค์ ํ OPENAI_API_KEY ๊ฐ์ ์ฐพ์ง ๋ชปํด์ ๋ฐ์ํ๋ ์๋ฌ๋ค.
โ๏ธ ํด๊ฒฐ
๋ถ๋ช ํ ์ค์ ํด์คฌ๋๋ฐ, ์ ๋์ง ์๋๊ฐ!!
์ผ๋จ, setx๋ก ์ค์ ํ ํ์๋ ์๋ก์ด PowerShell ์ธ์ ์ ์์ํด์ผ ํ๊ฒฝ๋ณ์๊ฐ ์ ์ฉ๋๋ค๊ณ ํ๋ค.
๊ทธ๋์, ์๋ก์ด PowerShell ์ ์ด๊ณ ๋ค์ ๋ช ๋ น์ด๋ก ์ ์ฉ๋์๋์ง ํ์ธํ๋ค.
echo $env:OPENAI_API_KEYโ
๋์ ์ฌ์ ํ ๋์ค์ง ์๋ ๊ณ ๋ฌธ์ ๋ฐ์๋ค.
์ถ๊ฐ์ ์ธ ๋ด์ฉ์ผ๋ก๋ ์ ๋ฐ์ ์ผ๋ก ๋ฐ์๋์ง ์๊ฑฐ๋ ์ง์ฐ๋ ์ ์๋ค๊ณ ํ๋ค.
๊ทธ๋์, ๊ฒฐ๊ตญ ๋๋ ์ ์ผ ์ซ์ ์ฌ๋ถํ ์ผ๋ก ํด๊ฒฐ๋๋ค.๋