
如何獲取免費的ChatGPT API密鑰 – Apidog
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Access API key securely
api_key = os.getenv("OPENAI_API_KEY")
Sora API 的使用須遵守以下規定:
查看OpenAI 文檔以獲取最新信息,因為這些細節可能會隨著 API 從預覽版到普遍可用性而發展。
為了有效地與 Sora API 交互,您需要:
# Install required packages
pip install openai requests python-dotenv
# Basic imports for working with the API
import openai
import json
import time
使用 Sora API 需要了解其請求結構、參數和響應格式。
所有對 Sora API 的請求都需要使用您的 API 密鑰進行身份驗證:
# Configure OpenAI with your API key
openai.api_key = os.getenv("OPENAI_API_KEY")
# Basic request to generate a video
response = openai.Sora.create(
prompt=prompt,
duration_seconds=duration
)
results.append({
"variation": i+1,
"prompt": prompt,
"success": True,
"url": response.data[0].url
})
except Exception as e:
results.append({
"variation": i+1,
"prompt": prompt,
"success": False,
"error": str(e)
})
time.sleep(2) # Prevent rate limiting
# Analyze results to identify patterns
successful = [r for r in results if r["success"]]
failed = [r for r in results if not r["success"]]
if len(successful) > 0:
print("Successful variations found. Review them to understand what works.")
return successful
else:
print("All variations failed. Consider more significant prompt restructuring.")
return failed
實施系統的評估方法有助于不斷改進您的 Sora API 實現。
有用的評估指標包括:
為了進行系統評估,請考慮實施評分系統:
def evaluate_generation(prompt, video_url, criteria=None):
"""Basic evaluation framework for generations"""
if criteria is None:
criteria = {
"visual_quality": "Rate the overall visual quality from 1-10",
"prompt_adherence": "Rate how well the video matches the prompt from 1-10",
"consistency": "Rate the physical and temporal consistency from 1-10",
"narrative": "Rate the narrative coherence from 1-10"
}
print(f"Evaluating video generated from prompt: {prompt[:50]}...")
print(f"Video URL: {video_url}")
results = {}
for criterion, description in criteria.items():
score = input(f"{description}: ")
results[criterion] = int(score)
# Calculate overall score
overall = sum(results.values()) / len(results)
results["overall"] = overall
print(f"Overall score: {overall:.1f}/10")
return results
有效的反饋方法包括:
在您的應用程序中實現一個簡單的反饋系統:
def collect_user_feedback(video_id, user_id):
"""Collect and store user feedback on generated videos"""
questions = [
{"id": "quality", "text": "How would you rate the visual quality?", "type": "scale", "range": [1, 5]},
{"id": "realism", "text": "How realistic did the video appear?", "type": "scale", "range": [1, 5]},
{"id": "usefulness", "text": "How useful was this video for your needs?", "type": "scale", "range": [1, 5]},
{"id": "improvements", "text": "What could be improved about this video?", "type": "text"}
]
# In a real application, this would render a form and collect responses
# For this example, we'll simulate responses
responses = {
"video_id": video_id,
"user_id": user_id,
"timestamp": time.time(),
"ratings": {
"quality": 4,
"realism": 3,
"usefulness": 4
},
"comments": "The lighting was great but motion could be smoother."
}
# In a real application, store this in a database
store_feedback(responses)
# Analyze feedback trends
analyze_feedback_trends(video_id)
return responses
為了不斷提高您的成果:
實施持續改進流程:
def iterative_prompt_improvement(original_prompt, iterations=3):
"""Iteratively improve prompts based on results"""
current_prompt = original_prompt
results = []
for i in range(iterations):
print(f"Iteration {i+1} with prompt: {current_prompt[:50]}...")
# Generate video with current prompt
response = openai.Sora.create(
prompt=current_prompt,
duration_seconds=10
)
# Collect evaluation (in a real system, this could be user feedback)
evaluation = evaluate_generation(current_prompt, response.data[0].url)
results.append({
"iteration": i+1,
"prompt": current_prompt,
"score": evaluation["overall"],
"url": response.data[0].url
})
# If score is high enough, stop iterations
if evaluation["overall"] >= 8:
print("Reached satisfactory quality. Stopping iterations.")
break
# Use feedback to improve the prompt
if evaluation["prompt_adherence"] < 7:
current_prompt = add_specificity(current_prompt)
if evaluation["consistency"] < 7:
current_prompt = enhance_physical_descriptions(current_prompt)
if evaluation["narrative"] < 7:
current_prompt = improve_narrative_flow(current_prompt)
print(f"Revised prompt: {current_prompt[:50]}...")
time.sleep(2) # Prevent rate limiting
# Return the best result
best_result = max(results, key=lambda x: x["score"])
print(f"Best result was iteration {best_result['iteration']} with score {best_result['score']}/10")
return best_result
隨著 Sora API 的發展,適應性設計將確保您的實施保持有效。
構建彈性實施方案:
版本感知的實現方法:
class SoraClient:
def __init__(self, api_key=None):
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
self.api_version = self._detect_api_version()
def _detect_api_version(self):
"""Detect the current Sora API version"""
try:
# Make a minimal API call to check version
metadata = openai.Sora.get_info()
return metadata.version
except:
# Fall back to default version if detection fails
return "v1"
def generate_video(self, prompt, duration, **kwargs):
"""Version-aware video generation"""
if self._supports_feature("high_resolution") and kwargs.get("high_res"):
resolution = "1080p"
else:
resolution = "720p"
if self._supports_feature("extended_duration") and duration > 60:
# Handle with segmentation for older API versions
return self._generate_segmented(prompt, duration, **kwargs)
# Standard generation with version-appropriate parameters
params = self._prepare_parameters(prompt, duration, **kwargs)
return openai.Sora.create(**params)
def _supports_feature(self, feature_name):
"""Check if current API version supports a specific feature"""
feature_map = {
"high_resolution": ["v1.2", "v2.0"],
"extended_duration": ["v2.0"],
"style_transfer": ["v1.5", "v2.0"]
}
if feature_name in feature_map:
return self.api_version in feature_map[feature_name]
return False
def _prepare_parameters(self, prompt, duration, **kwargs):
"""Prepare version-appropriate parameters"""
# Base parameters supported across versions
params = {
"prompt": prompt,
"duration_seconds": min(duration, 60) # Enforce limits for older versions
}
# Add version-specific parameters
if self.api_version >= "v1.5" and "style" in kwargs:
params["style_preset"] = kwargs["style"]
# Add other parameters based on version capability
return params
對于預期需求增加的應用程序:
可擴展隊列實現:
import asyncio
import aiohttp
import time
from fastapi import FastAPI, BackgroundTasks
from pydantic import BaseModel
app = FastAPI()
class VideoRequest(BaseModel):
prompt: str
duration: int
callback_url: str
user_id: str
# Simple in-memory queue for demonstration
request_queue = asyncio.Queue()
processing_semaphore = asyncio.Semaphore(5) # Limit concurrent processing
@app.post("/generate")
async def enqueue_generation(request: VideoRequest, background_tasks: BackgroundTasks):
# Add to queue
await request_queue.put(request)
# Start processing in background if not already running
background_tasks.add_task(process_queue)
return {"status": "queued", "queue_position": request_queue.qsize()}
async def process_queue():
while not request_queue.empty():
async with processing_semaphore:
request = await request_queue.get()
try:
# Generate video
response = await generate_video_async(request.prompt, request.duration)
# Notify via callback
await send_callback(request.callback_url, {
"user_id": request.user_id,
"status": "completed",
"video_url": response.data[0].url
})
except Exception as e:
# Handle failures
await send_callback(request.callback_url, {
"user_id": request.user_id,
"status": "failed",
"error": str(e)
})
finally:
request_queue.task_done()
async def generate_video_async(prompt, duration):
"""Asynchronous video generation"""
# In a real implementation, use the OpenAI async client
return openai.Sora.create(
prompt=prompt,
duration_seconds=duration
)
async def send_callback(url, data):
"""Send callback to notify of completion"""
async with aiohttp.ClientSession() as session:
await session.post(url, json=data)
無論您是將 Sora 集成到應用程序的開發人員、希望擴展工具包的內容創建者,還是尋求轉變視覺內容制作的組織,本指南中涵蓋的原則和技術都為成功實施和優化 OpenAI Sora API 提供了路線圖。
prompt="A calm lake reflecting the sunrise, with mountains in the background and birds flying across the sky.",
duration_seconds=10
)
video_url = response.data[0].url
### Essential Parameters Explained
The Sora API accepts several key parameters that control the generation process:
- **prompt** (required): The text description of the video you want to generate. This is the most important parameter and should be detailed and specific.
- **duration_seconds**: Specifies the desired length of the video (typically 1-60 seconds).
- **output_format**: The file format for the generated video (e.g., "mp4", "webm").
- **resolution**: The dimensions of the output video (e.g., "1080p", "720p").
- **style_preset**: Optional parameter to influence the visual style (e.g., "cinematic", "animation", "documentary").
- **negative_prompt**: Descriptions of what you want to avoid in the generated video.
### Understanding Response Formats
The API returns a structured response containing:
```json
{
"id": "gen-2xJ7LjGi8M5UgRq2XCTg8Zp2",
"created": 1709548934,
"status": "completed",
"data": [
{
"url": "https://cdn.openai.sora.generation/videos/gen-2xJ7LjGi8M5UgRq2XCTg8Zp2.mp4",
"metadata": {
"duration_ms": 10000,
"resolution": "1080p",
"format": "mp4"
}
}
]
}
關鍵要素包括:
使用 Sora API 時,強大的錯誤處理至關重要:
try:
response = openai.Sora.create(
prompt="A serene mountain landscape with flowing rivers and dense forests.",
duration_seconds=15
)
video_url = response.data[0].url
except openai.error.RateLimitError:
# Handle rate limiting
print("Rate limit exceeded. Implementing exponential backoff...")
time.sleep(30)
except openai.error.InvalidRequestError as e:
# Handle invalid requests (e.g., problematic prompts)
print(f"Invalid request: {str(e)}")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {str(e)}")
建議采用指數退避實現智能重試邏輯來處理速率限制和瞬態錯誤。
提示符的質量會顯著影響 Sora 的輸出。學習如何編寫有效的提示符或許是使用 API 最重要的技能。
有效的 Sora 提示通常遵循以下原則:
基本提示:
一只紅狐貍在雪林中奔跑。
改進的提示:
一只尾巴濃密的紅狐貍在茂密的冬日森林中奔跑。白雪皚皚的松樹環繞著小路。清晨的陽光透過枝葉,在雪地上留下斑駁的光芒。狐貍快速地從左到右移動,偶爾回頭望向鏡頭。隨著狐貍的經過,廣角鏡頭逐漸過渡到特寫。
改進的提示提供了更多關于場景、燈光、運動方向和攝影工作的背景信息,從而產生更具體、更可控的輸出。
對于動態視頻,有效地傳達動作至關重要:
風格指導有助于設定視覺基調:
東京夜晚熙熙攘攘的街道,以霓虹黑色電影風格拍攝。濃重的陰影與鮮艷的霓虹燈形成鮮明對比,倒映在雨水濕滑的街道上。慢動作鏡頭捕捉撐傘行人穿過十字路口的場景。變形鏡頭拍攝的過往車輛前燈產生的眩光。
這個提示不僅描述了內容,還具體引用了電影風格并提供了有關視覺處理的細節。
一旦您熟悉了基本的視頻生成,您就可以探索更復雜的方法來擴展 Sora 的功能。
對于較長的敘述或復雜的序列,您可以將多個代鏈接在一起:
def generate_story_sequence(scene_descriptions, durations):
video_urls = []
for i, (description, duration) in enumerate(zip(scene_descriptions, durations)):
print(f"Generating scene {i+1}: {description[:50]}...")
response = openai.Sora.create(
prompt=description,
duration_seconds=duration
)
video_urls.append(response.data[0].url)
time.sleep(2) # Avoid rate limiting
return video_urls
# Example usage
scene_descriptions = [
"A seed sprouting from soil, close-up timelapse with morning light.",
"The sprout growing into a small plant, developing its first leaves.",
"The plant maturing and developing flower buds, still in timelapse.",
"The flower blooming in vibrant colors, attracting a hummingbird."
]
durations = [8, 12, 10, 15]
video_sequence = generate_story_sequence(scene_descriptions, durations)
然后可以使用 MoviePy 或 ffmpeg 等視頻編輯庫連接這些視頻。
為了保持場景間的一致性:
# First generation
initial_response = openai.Sora.create(
prompt="A young woman in a red dress walks along a beach at sunset, seen from behind.",
duration_seconds=10
)
# Continuation with reference to maintain character consistency
continuation_response = openai.Sora.create(
prompt="The same woman in the red dress now turns to face the ocean, the golden sunset light illuminating her face as she smiles.",
duration_seconds=12
)
您可以嘗試將特定的視覺樣式應用到您的世代中:
styles = [
"in the style of a watercolor painting",
"filmed as classic film noir with high contrast black and white",
"rendered as a vibrant anime scene",
"captured as a vintage 8mm home movie"
]
base_prompt = "A sailboat on a calm lake with mountains in the background"
for style in styles:
styled_prompt = f"{base_prompt}, {style}"
print(f"Generating: {styled_prompt}")
response = openai.Sora.create(
prompt=styled_prompt,
duration_seconds=8
)
# Process response
對于更復雜的工作流程,請將 Sora 與其他 OpenAI 服務結合使用:
from openai import OpenAI
client = OpenAI()
# Use GPT to enhance a basic prompt
basic_idea = "Dog in a park"
gpt_response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a video description expert. Expand the basic video idea into a detailed, visually rich prompt for a video generation AI."},
{"role": "user", "content": f"Basic idea: {basic_idea}"}
]
)
enhanced_prompt = gpt_response.choices[0].message.content
# Use the enhanced prompt with Sora
sora_response = openai.Sora.create(
prompt=enhanced_prompt,
duration_seconds=15
)
OpenAI Sora API 代表了 AI 生成視頻領域的重大進步,它提供了前所未有的能力,可以將文本描述轉化為高質量、連貫的視覺內容。正如我們在本指南中所探討的,要有效地實現 Sora,需要理解其技術層面以及成功生成視頻的創意原則。
對于希望利用 Sora 的開發人員和內容創建者來說,關鍵要點包括:
隨著技術的不斷發展,保持適應性將是最大限度發揮其潛力的關鍵。通過兼顧技術卓越性和創意品質,您可以充分利用這一突破性工具的全部功能,創作出引人入勝的視覺內容,而這在幾年前還是不可能實現或成本高昂的。
未來幾年,AI 視頻生成能力將迎來顯著提升,分辨率將更高、時長將更長、控制將更精準,創意可能性也將進一步拓展。現在就打下堅實的知識基礎和最佳實踐,您將能夠充分利用這些新興技術。
文章轉載自:How to Use OpenAI’s Sora API: A Comprehensive Guide