File size: 1,243 Bytes
6835659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Any, Dict

from src.planner.semantic_plan import SemanticPlan, Scene
from src.llm.llm_router import get_llm


SYSTEM_PROMPT = """
You are a semantic planner.

Convert the user prompt into a STRICT JSON object matching this schema:

{
  "scene": {
    "setting": "urban | natural | indoor",
    "time": "day | night | sunset",
    "weather": "clear | rain | fog | wind"
  },
  "visual_elements": [string, ...],
  "audio_elements": [string, ...],
  "mood": "calm | tense | futuristic | melancholic",
  "motion": "static | slow | dynamic"
}

Rules:
- Output JSON ONLY
- No explanations
- No extra keys
- Use best semantic judgment
"""


def generate_semantic_plan(prompt: str) -> SemanticPlan:
    llm = get_llm()
    data: Dict[str, Any] = llm.generate_json(
        f"{SYSTEM_PROMPT}\n\nUser prompt: {prompt}"
    )

    try:
        scene = Scene(**data["scene"])
        plan = SemanticPlan(
            scene=scene,
            visual_elements=data["visual_elements"],
            audio_elements=data["audio_elements"],
            mood=data["mood"],
            motion=data["motion"],
        )
    except Exception as e:
        raise ValueError(f"Planner output does not match schema:\n{data}") from e

    return plan