demo.py
#!/usr/bin/env -S poetry run python
from openai import OpenAI
# gets API Key from environment variable OPENAI_API_KEY
client = OpenAI()
# Non-streaming:
print("----- standard request -----")
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say this is a test",
},
],
)
print(completion.choices[0].message.content)
# Streaming:
print("----- streaming request -----")
stream = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "How do I output all files in a directory using Python?",
},
],
stream=True,
)
for chunk in stream:
if not chunk.choices:
continue
print(chunk.choices[0].delta.content, end="")
print()