#Give me code pls
1 messages · Page 1 of 1 (latest)
Certainly! Here’s a simple example of how to use the OpenAI API in Python to generate text with GPT-5. This code will send a prompt to the model and print the response:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5",
input="Write a one-sentence bedtime story about a unicorn."
)
print(response.output_text)
Make sure you have installed the OpenAI SDK with:
pip install openai
And set your API key as an environment variable named OPENAI_API_KEY before running the script.
If you want a JavaScript example, here’s how you can do it with Node.js:
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
input: "Write a one-sentence bedtime story about a unicorn."
});
console.log(response.output_text);
Install the SDK with:
npm install openai
These examples are directly from the official OpenAI documentation and will get you started quickly Developer quickstart.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@narrow egret Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
Certainly! Here is example code for generating a video using the Sora 2 model via the OpenAI API in Python, as provided in the official documentation:
from openai import OpenAI
openai = OpenAI()
video = openai.videos.create(
model="sora-2",
prompt="A video of a cool cat on a motorcycle in the night",
)
print("Video generation started:", video)
You can monitor the progress and download the final MP4 once the job is completed. Here’s a more complete example that polls for status and downloads the video:
from openai import OpenAI
import sys
import time
openai = OpenAI()
video = openai.videos.create(
model="sora-2",
prompt="A video of a cool cat on a motorcycle in the night",
)
print("Video generation started:", video)
progress = getattr(video, "progress", 0)
bar_length = 30
while video.status in ("in_progress", "queued"):
# Refresh status
video = openai.videos.retrieve(video.id)
progress = getattr(video, "progress", 0)
filled_length = int((progress / 100) * bar_length)
bar = "=" * filled_length + "-" * (bar_length - filled_length)
status_text = "Queued" if video.status == "queued" else "Processing"
sys.stdout.write(f"\r{status_text}: [{bar}] {progress:.1f}%")
sys.stdout.flush()
time.sleep(2)
# Move to next line after progress loop
sys.stdout.write("\n")
if video.status == "failed":
message = getattr(
getattr(video, "error", None), "message", "Video generation failed"
)
print(message)
exit(1)
print("Video generation completed:", video)
print("Downloading video content...")
content = openai.videos.download_content(video.id, variant="video")
content.write_to_file("video.mp4")
print("Wrote video.mp4")
Requirements:
- Install the OpenAI Python SDK:
pip install openai - Set your
OPENAI_API_KEYas an environment variable.
This code is directly based on the official OpenAI documentation for Sora 2 video generation Video generation with Sora Retrieve results.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@narrow egret Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Hi kapa