Python/알면 쓸모있는 잡다한 코드
chat gpt api 이용 방법
joannekim0420
2023. 12. 6. 15:31
728x90
https://openai.com/blog/introducing-chatgpt-and-whisper-apis
Introducing ChatGPT and Whisper APIs
Developers can now integrate ChatGPT and Whisper models into their apps and products through our API.
openai.com
API 새로 만들기
※ API 키는 만들 때 밖에 알 수 없으므로 미리 복사 저장하기
환경에 openai 설치
pip install openai
파이썬으로 사용법
from openai import OpenAI
# 발급받은 API 키 설정
OPENAI_API_KEY = "~~"
# openai API 키 인증.
client = OpenAI(
api_key=OPENAI_API_KEY
)
# 모델 선택
MODEL = "gpt-3.5-turbo"
# 질문 작성하기
query = "chatgpt가 뭔지 알려줘"
# 메시지 설정하기
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
}, {
"role": "user",
"content": query
}
],
model=MODEL,
)
print(chat_completion)
#답변만 접근
print(chat_completion['choices'][0]['message']['content'])
참고
1.2 ChatGPT API 사용하기
OpenAI 에서 제공하는 ChatGPT API 사용법에 대해서 설명하겠습니다. [TOC] ## ChatGPT API 사용방법 1. OpenAI 회원가입을 한다. 2…
wikidocs.net
https://github.com/openai/openai-python
GitHub - openai/openai-python: The official Python library for the OpenAI API
The official Python library for the OpenAI API. Contribute to openai/openai-python development by creating an account on GitHub.
github.com