
REST API命名規范的終極指南:清晰度和一致性的最佳實踐
官方公告顯示,它能在 720p@24fps 的實時流里,根據 一句話或一張圖 生成可交互的 3D 世界,并支持「Promptable World Events」——你喊一聲“下暴雨”,場景就真會烏云壓頂。
如果你:
那么今天這 5 分鐘,將是你 2025 年 ROI 最高的一次技術投資。
秒 | 動作 | 終端命令 |
---|---|---|
0-30 | 申請 API Key | 打開 Google AI Studio |
30-90 | 裝 SDK | pip install genie3-api 或 npm i genie3-api |
90-150 | 跑通 Hello World | genie3 quickstart --prompt "一座漂浮的圖書館" |
150-210 | 集成到 Python | 見 03.1 |
210-270 | 集成到 Node | 見 03.2 |
270-300 | 集成到 Unity | 見 03.3 |
GENIE3_API_KEY
寫進環境變量:
export GENIE3_API_KEY="AIz...your_key...c"
curl -H "x-goog-api-key:$GENIE3_API_KEY" \
https://genie3.googleapis.com/v1/quota
返回示例:
{"daily_frames":10000,"remaining":10000}
import os, requests, json
url = "https://genie3.googleapis.com/v1/generate"
payload = {
"prompt": "一座漂浮的圖書館",
"duration": 5,
"resolution": "720p",
"events": [{"type":"weather","value":"sunset"}]
}
headers = {"x-goog-api-key": os.getenv("GENIE3_API_KEY")}
r = requests.post(url, json=payload, headers=headers, stream=True)
with open("scene.mp4","wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
print("? 5 秒 3D 世界已保存到 scene.mp4")
運行效果:終端打印 ? 后,雙擊 scene.mp4
即可看到漂浮圖書館的日落鏡頭。
npm i genie3-api
import { Genie3Client } from "genie3-api";
const client = new Genie3Client({ apiKey: process.env.GENIE3_API_KEY });
const stream = await client.generateStream({
prompt: "賽博朋克夜市",
duration: 10,
events: [{ type: "add_object", value: "飛行出租車" }]
});
stream.pipeTo(require("fs").createWriteStream("cyberpunk.mp4"));
瀏覽器實時預覽:
< video autoplay muted loop src="cyberpunk.mp4" > < /video >
Assets/Plugins
目錄下新建腳本 Genie3Player.cs
: // Assets/Plugins/Genie3Player.cs
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Genie3Player : MonoBehaviour
{
public string prompt = "low-poly maze";
IEnumerator Start()
{
string apiKey = System.Environment.GetEnvironmentVariable("GENIE3_API_KEY");
string json = JsonUtility.ToJson(new { prompt, duration = 3, resolution = "480p" });
byte[] body = System.Text.Encoding.UTF8.GetBytes(json);
var req = new UnityWebRequest("https://genie3.googleapis.com/v1/generate", "POST");
req.uploadHandler = new UploadHandlerRaw(body);
req.downloadHandler = new DownloadHandlerFile($"{Application.persistentDataPath}/unity_scene.mp4");
req.SetRequestHeader("x-goog-api-key", apiKey);
yield return req.SendWebRequest();
Debug.Log("Unity 場景生成完成");
}
}
unity_scene.mp4
。Genie 3 最炸裂的是 事件指令,支持 3 大類 20+ 子事件:
類別 | 示例指令 | JSON 片段 |
---|---|---|
天氣 | "暴雪" |
{"type":"weather","value":"blizzard"} |
時間 | "黎明→黃昏" |
{"type":"time","value":"dusk","transition":3} |
物體 | "在屋頂放一只貓" |
{"type":"add_object","value":"cat","position":[10,5,0]} |
把事件塞進 事件隊列 即可實現劇情腳本:
"events": [
{"type":"weather","value":"rain","start":1},
{"type":"add_object","value":"lightning","start":3},
{"type":"camera_shake","intensity":0.4,"start":3.5}
]
分辨率 | 幀率 | 單價 | 免費額度可玩時長 |
---|---|---|---|
720p | 24 fps | $0.0006/幀 | 10000÷24÷60 ≈ 6.9 分鐘 |
480p | 15 fps | $0.0002/幀 | 10000÷15÷60 ≈ 11.1 分鐘 |
省錢三板斧:
cache_key = md5(prompt + json.dumps(events))
--segment
分段生成,只重錄需要修改的片段。(單文件或少文件,復制即可跑)
場景 | 技術棧 | 運行方式 | 亮點 | 核心代碼位置 |
---|---|---|---|---|
3D 英語課堂 | Python + Streamlit | streamlit run edu.py |
輸入單詞→即時 3D 場景 | 下方代碼塊 ① |
直播虛擬背景 | Node + OBS WebSocket | node obs-bg.js |
彈幕實時觸發天氣切換 | 下方代碼塊 ② |
具身智能訓練 | Unity + ML-Agents | 拖入腳本 → Play | 一鍵生成 1000 個隨機迷宮 | 下方代碼塊 ③ |
# 保存為 edu.py,終端:streamlit run edu.py
import streamlit as st, os, requests, json, time
API_KEY = os.getenv("GENIE3_API_KEY")
URL = "https://genie3.googleapis.com/v1/generate"
st.title("?? 3D 英語課堂")
word = st.text_input("輸入單詞:", "library")
if st.button("生成場景"):
payload = {"prompt": f"A vivid 3D scene of a {word}", "duration": 5, "resolution": "480p"}
headers = {"x-goog-api-key": API_KEY}
r = requests.post(URL, json=payload, headers=headers, stream=True)
mp4_path = f"{word}.mp4"
with st.spinner("生成中…"):
with open(mp4_path, "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
st.video(mp4_path)
// 保存為 obs-bg.js,終端:node obs-bg.js
import { WebSocket } from "ws";
import "node-fetch"; // 或 import fetch from 'node-fetch';
import fs from "fs";
const OBS = "ws://localhost:4455";
const API_KEY = process.env.GENIE3_API_KEY;
const ws = new WebSocket(OBS);
ws.on("open", () = > console.log("OBS 已連接"));
ws.on("message", async (msg) = > {
const data = JSON.parse(msg.toString());
if (data.d?.eventData?.chatMessage) {
const text = data.d.eventData.chatMessage;
const weather = text.includes("下雪") ? "snow" : text.includes("下雨") ? "rain" : null;
if (!weather) return;
const res = await fetch("https://genie3.googleapis.com/v1/generate", {
method: "POST",
headers: { "x-goog-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ prompt: "賽博朋克夜市", duration: 10, events: [{ type: "weather", value: weather }] })
});
const dest = fs.createWriteStream("bg.mp4");
res.body.pipe(dest);
dest.on("finish", () = > {
ws.send(JSON.stringify({ op: 6, d: { sourceName: "Genie3", localFile: "./bg.mp4" } }));
});
}
});
// Assets/Scripts/Genie3Maze.cs
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.IO;
public class Genie3Maze : MonoBehaviour
{
public string prompt = "random 3D maze";
public int mazeCount = 1000;
IEnumerator Start()
{
string apiKey = System.Environment.GetEnvironmentVariable("GENIE3_API_KEY");
for (int i = 0; i < mazeCount; i++)
{
string json = JsonUtility.ToJson(new { prompt = prompt + " " + i, duration = 3, resolution = "480p" });
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
var req = new UnityWebRequest("https://genie3.googleapis.com/v1/generate", "POST");
req.uploadHandler = new UploadHandlerRaw(bodyRaw);
req.downloadHandler = new DownloadHandlerFile(Path.Combine(Application.persistentDataPath, $"maze_{i}.mp4"));
req.SetRequestHeader("x-goog-api-key", apiKey);
yield return req.SendWebRequest();
Debug.Log($"Maze {i} ready.");
}
}
}
錯誤碼 | 場景 | 解決 |
---|---|---|
429 | 幀率超配額 | sleep(1) 退避或升級付費檔 |
400 | Prompt 含敏感詞 | 先用官方 moderation API 過濾 |
504 | 高并發 | 降級 --quality low 或異步隊列 |
"Genie 3 reveal easter egg"
會生成隱藏彩蛋場景 ??。300 秒前,你只有一個空文件夾;
300 秒后,你擁有了一個可以“說話就會變”的 3D 宇宙。
現在就打開終端,復制下面這條命令,讓 Genie 3 成為你 2025 年的生產力外掛:
echo "export GENIE3_API_KEY=YOUR_KEY" > > ~/.zshrc && source ~/.zshrc
python -c "import requests,os;r=requests.post('https://genie3.googleapis.com/v1/generate',json={'prompt':'Hello Genie3','duration':3,'resolution':'480p'},headers={'x-goog-api-key':os.getenv('GENIE3_API_KEY')},stream=True);open('hello.mp4','wb').write(r.content)"