Skip to content

Reverse-engineer any video, shot by shot: filmed, AI-generated or coded, and how to remake it. Breakdowns in the open VIDEO.md format. 逆向一切视频,逐个镜头看是拍的、AI 生成的还是代码做的,再告诉你怎么复刻;拆解结果按 VIDEO.md 格式输出。

fix: scope VLM cache to model and request configuration

zuotongbin.tobin77d973e70086
diff --git a/rea_video/vlm.py b/rea_video/vlm.py
index f0e184a..8d13e30 100644
--- a/rea_video/vlm.py
+++ b/rea_video/vlm.py
@@ -135,7 +135,11 @@ class VLMClient:
return data["choices"][0]["message"]["content"]
def ask_json(self, prompt: str, media: list[tuple[Path, str]], max_tokens: int = 4096) -> dict:
- h = hashlib.sha1(prompt.encode())
+ # Use the configured order, not self.models: successful fallbacks reorder
+ # the latter during a run. Never include the API key in the cache identity.
+ request = {"prompt": prompt, "base_url": self.cfg.base_url.rstrip("/"),
+ "models": self.cfg.models, "max_tokens": max_tokens}
+ h = hashlib.sha1(json.dumps(request, sort_keys=True, ensure_ascii=False).encode())
for p, mime in media:
h.update(mime.encode())
h.update(hashlib.sha1(p.read_bytes()).digest())
diff --git a/tests/test_vlm_cache.py b/tests/test_vlm_cache.py
new file mode 100644
index 0000000..335c82c
--- /dev/null
+++ b/tests/test_vlm_cache.py
@@ -0,0 +1,61 @@
+"""Cached answers must belong to the requested model configuration."""
+import json
+
+import pytest
+
+from rea_video.vlm import VLMClient, VLMConfig
+
+
[email protected]("change", ["model", "model_order", "endpoint", "max_tokens"])
+def test_changed_request_does_not_reuse_cached_answer(tmp_path, monkeypatch, change):
+ calls = []
+
+ def post(self, model, content, max_tokens):
+ calls.append((self.cfg.base_url, model, max_tokens))
+ return json.dumps({"answer": len(calls)})
+
+ monkeypatch.setattr(VLMClient, "_post", post)
+ media = tmp_path / "frame.jpg"
+ media.write_bytes(b"same media")
+ cache = tmp_path / "cache"
+ original = VLMConfig("https://first.example/v1", "test", ["first", "fallback"])
+ first = VLMClient(original, cache).ask_json("describe", [(media, "image/jpeg")])
+ assert first["answer"] == 1
+
+ cfg = VLMConfig(original.base_url, original.api_key, list(original.models))
+ max_tokens = 4096
+ if change == "model":
+ cfg.models = ["second"]
+ elif change == "model_order":
+ cfg.models.reverse()
+ elif change == "endpoint":
+ cfg.base_url = "https://second.example/v1"
+ else:
+ max_tokens = 8192
+ client = VLMClient(cfg, cache)
+ result = client.ask_json("describe", [(media, "image/jpeg")], max_tokens=max_tokens)
+ assert result["answer"] == 2
+ assert len(calls) == 2
+ # The identical request still hits the cache, including from a new client.
+ assert VLMClient(cfg, cache).ask_json("describe", [(media, "image/jpeg")], max_tokens=max_tokens) == result
+ assert len(calls) == 2
+
+
+def test_fallback_reordering_preserves_cache_hit(tmp_path, monkeypatch):
+ calls = []
+
+ def post(self, model, content, max_tokens):
+ calls.append(model)
+ if model == "down":
+ raise ValueError("unavailable")
+ return '{"answer": "ok"}'
+
+ monkeypatch.setattr(VLMClient, "_post", post)
+ monkeypatch.setattr("rea_video.vlm.time.sleep", lambda seconds: None)
+ cfg = VLMConfig("https://first.example/v1", "test", ["down", "working"])
+ client = VLMClient(cfg, tmp_path / "cache")
+ result = client.ask_json("describe", [])
+ assert client.models == ["working", "down"]
+ assert calls == ["down", "down", "working"]
+ assert client.ask_json("describe", []) == result
+ assert len(calls) == 3