728x90

1. openai api key 발급

https://platform.openai.com/account/api-keys

 

OpenAI API

An API for accessing new AI models developed by OpenAI

platform.openai.com

2. 챗GPT API 관련 document

https://platform.openai.com/docs/guides/chat

 

OpenAI API

An API for accessing new AI models developed by OpenAI

platform.openai.com

https://platform.openai.com/docs/api-reference/chat

 

OpenAI API

An API for accessing new AI models developed by OpenAI

platform.openai.com

 

3. content 쪽에 질문할 내용 입력하면 아래와 같이 결과를 확인할 수 있고,

json 결과가 한글의 경우 유니코드로 리턴되어서 encode 하려다가, 그냥 print 찍으니 한글로 잘 나온다.

import openai

openai.api_key = "api_key 입력"

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "user", "content": "너는 누구야?"}
    ]
)

print(response.get("choices")[0].get("message").get("content"))

728x90

+ Recent posts