
REST API命名规范的终极指南:清晰度和一致性的最佳实践
2025 年 8 月 5 日凌晨,Google DeepMind 把 Genie 3 的 Early-Access 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)"