1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#!/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()
# Response headers:
print("----- custom response headers test -----")
response = client.chat.completions.with_raw_response.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
)
completion = response.parse()
print(response.request_id)
print(completion.choices[0].message.content)
|