P.S0
dev.log
P.S0
전체 방문자
오늘
어제
  • 분류 전체보기 (42)
    • 프로그래밍(Programming) (28)
      • JavaScript (3)
      • Git (5)
      • React (4)
      • Vue (2)
      • Java (3)
      • 알고리즘 (7)
      • Python (1)
      • SQL (2)
    • TIL (7)
    • 프로젝트 (Project) (5)
      • BTP Project (3)
      • TRPG ASSISTANT (0)
      • OPEN_AI (2)
      • TISTORY (0)

블로그 메뉴

  • 홈
  • 💻Github
  • 📄 Velog
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Til
  • javascript

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
P.S0

dev.log

카테고리 없음

[Python] OpenAI>=1.0.0 버전 이후

2024. 7. 19. 20:59
728x90

1. 문제 상황

APIRemovedInV1: 
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API. You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

 

2. 문제 원인

과거의 자료를 보고 최근 openai 프로젝트를 진행했을 때 보기 쉬운 오류입니다.
패키지를 최근 높은 버전 (1.0.0)으로 설치했다면 발생하죠.

 

3. 해결 방법

 

오류 메시지에서 제공한 링크를 참고해 이전 코드에서 새로운 코드로 변경합니다.
https://github.com/openai/openai-python/discussions/742

보통 테스트 코드만 실행했는데도 오류가 떴을 것입니다.
그렇다면 해당 코드를 아래와같이 수정해줍니다.

 

- API Key import

# 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'],
)

 

- Responses

# 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))

 

이외에도 공식 링크를 참고하여 코드를 수정해주세요.

 

 


 

 

🔗모든 예시 코드를 사용해 api 호출하고 싶다면?

 

2024.07.15 - [프로젝트 (Project)/OPEN_AI] - [Python] OpenAI API 사용해 ChatGPT 사용해보기

728x90
    P.S0
    P.S0
    이전도 부지런함이다 그냥 살아보겠습니다.

    티스토리툴바