Openai-python
The official Python library for the OpenAI API
Install
Sync example
import os
from openai import OpenAI
client = OpenAI(
# This is the default and can be omitted
api_key=os.environ.get("OPENAI_API_KEY"),
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-3.5-turbo",
)
Async example
import os
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
# This is the default and can be omitted
api_key=os.environ.get("OPENAI_API_KEY"),
)
async def main() -> None:
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-3.5-turbo",
)
asyncio.run(main())
Streaming Responses
We provide support for streaming responses using Server Side Events (SSE).
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
The async client uses the exact same interface.
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def main():
stream = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
asyncio.run(main())
파인튜닝 방법
- 꿈 많은 사람의 이야기 :: OpenAI GPT Fine-Tuning(파인튜닝) 방법 정리 - 나만의 GPT 모델 만들기
- OpenAI ChatGPT API를 활용해 추천 시스템 구현하기(feat. HuggingFace)
Endpoints
Audio
Chat
Create chat completion
지정된 채팅 대화에 대한 모델 응답을 생성합니다.
The chat completion object
제공된 입력을 기반으로 모델이 반환한 채팅 완료 응답을 나타냅니다.
The chat completion chunk object
제공된 입력을 기반으로 모델이 반환한 채팅 완료 응답의 스트리밍된 청크를 나타냅니다.