본문 바로가기
Develop

[OpenAI] 1.0.0 Migration 관련 Function 변경

by 너드나무 2024. 11. 5.
반응형
 

v1.0.0 Migration Guide · openai openai-python · Discussion #742

We have released a new major version of our SDK, and we recommend upgrading promptly. It's a total rewrite of the library, so many things have changed, but we've made upgrading easy with a code mig...

github.com


초기화, Initialization

  • openai.OpenAI
    • api_keys : OpenAI API Key 세팅 및 dafault OS 환경변수 지정
# old
import openai

openai.api_key = os.environ['OPENAI_API_KEY']

# new
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],  # this is also the default, it can be omitted
)

응답, Responses

  • openai.chat.completions
    • Dict 구조 탈피 및 pydantic 모델 변경
      • completion.choices[0].content 등
    • 기존 함수 제거
      • openai.Completion
# before
import json
import openai

completion = openai.Completion.create(model='curie')
print(completion['choices'][0]['text'])
print(completion.get('usage'))
print(json.dumps(completion, indent=2))

# after
from openai import OpenAI

client = OpenAI()

completion = client.completions.create(model='curie')
print(completion.choices[0].text)
print(dict(completion).get('usage'))
print(completion.model_dump_json(indent=2))
728x90
반응형