콘텐츠로 이동

간단한 ChatGPT API

필요한 모듈 설치하기

!pip install openai

가장 기본적인 ChatGPT 호출 방식

from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "서울의 위도를 알려줘."}
    ],    
)

response = completion.choices[0].message;
print(response.content)

설명

  • ChatGPT에 프롬프트를 보낼 때에는 messages 이름으로 role, content에 담겨져서 전송됩니다.
  • 위의 예제는 답변 생성이 모두 완료된 후에 결과를 받는 가장 기본적인 코드입니다.
  • ChatGPT로 부터 받을 때에도 content 이름으로 전송됩니다.