Layout gpt
LayoutGPT Pydantic AI agent package.
BoxFormat ¶
Bases: StrEnum
Supported bounding-box coordinate formats.
Source code in lib/laygen/src/laygen/common/bbox.py
21 22 23 24 25 26 | |
ConditionType ¶
Bases: StrEnum
Canonical condition names used by layout generation interfaces.
Source code in lib/laygen/src/laygen/common/conditions.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
LayoutGPTAgent ¶
Bases: BaseLayoutAgent[RawLayoutResponse]
High-level LayoutGPT runner that ports released prompt and parse strategy.
Source code in models/layout-gpt/src/layout_gpt/agent.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
__init__ ¶
__init__(
*,
model: ModelLike = None,
config: LayoutGPTConfig,
token_counter: TokenCounter = default_token_counter,
) -> None
Initialize the runner with provider and prompt configuration.
Source code in models/layout-gpt/src/layout_gpt/agent.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
build_prompt ¶
build_prompt(
prompt: str,
*,
train_examples: Sequence[LayoutExample],
seed: int | None = None,
generator: Generator | None = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
) -> tuple[str | list[ChatMessage], list[LayoutExample]]
Select exemplars and serialize the prompt sent to the model.
Source code in models/layout-gpt/src/layout_gpt/agent.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
run_sync ¶
run_sync(
prompt: str,
*,
train_examples: Sequence[LayoutExample],
model: ModelLike = None,
seed: int | None = None,
generator: Generator | None = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
model_settings: ModelSettings | None = None,
) -> LayoutGPTOutput
Run LayoutGPT and parse the response into typed layout output.
Source code in models/layout-gpt/src/layout_gpt/agent.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
__call__ ¶
__call__(
*,
prompt: str,
train_examples: Sequence[LayoutExample],
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: str
| ConditionType = ConditionType.text,
labels: Int[Tensor, "batch elements"]
| list[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| list[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| list[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: str | BoxFormat = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: str | OutputType = OutputType.dataclass,
return_intermediates: bool = False,
model: ModelLike = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
model_settings: ModelSettings | None = None,
) -> LayoutGenerationOutput | LayoutGPTOutputDict
Generate a layout through the common public generation surface.
Source code in models/layout-gpt/src/layout_gpt/agent.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
) -> None
Persist prompt and parser configuration without provider state.
Source code in models/layout-gpt/src/layout_gpt/agent.py
227 228 229 230 231 232 233 234 235 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
*,
model: ModelLike = None,
token_counter: TokenCounter = default_token_counter,
) -> "LayoutGPTAgent"
Load saved LayoutGPT prompt and parser configuration.
Source code in models/layout-gpt/src/layout_gpt/agent.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
ICLType ¶
Bases: StrEnum
Supported in-context exemplar selection modes.
Source code in models/layout-gpt/src/layout_gpt/enums.py
16 17 18 19 20 | |
LayoutGPTSetting ¶
Bases: StrEnum
Supported NSR-1K LayoutGPT settings.
Source code in models/layout-gpt/src/layout_gpt/enums.py
9 10 11 12 13 | |
OutputType ¶
Bases: StrEnum
Public return type names.
Source code in models/layout-gpt/src/layout_gpt/enums.py
23 24 25 26 27 | |
LayoutGPTOutput ¶
Bases: BaseModel
Pydantic representation of a parsed LayoutGPT response.
Source code in models/layout-gpt/src/layout_gpt/schema.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
to_layout_generation_output ¶
to_layout_generation_output() -> LayoutGenerationOutput
Convert to the shared public layout output schema.
Source code in models/layout-gpt/src/layout_gpt/schema.py
85 86 87 88 89 90 91 92 93 94 95 96 97 | |
LayoutItem2D ¶
Bases: BaseModel
A parsed 2D CSS layout item.
Source code in models/layout-gpt/src/layout_gpt/schema.py
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 | |
build_agent ¶
build_agent(model: ModelLike = None) -> Agent[None]
Build a Pydantic AI agent with provider selected by argument or env.
Source code in models/layout-gpt/src/layout_gpt/agent.py
49 50 51 52 53 54 55 56 | |
agent ¶
Provider-agnostic Pydantic AI wrapper for LayoutGPT.
LayoutGPTAgent ¶
Bases: BaseLayoutAgent[RawLayoutResponse]
High-level LayoutGPT runner that ports released prompt and parse strategy.
Source code in models/layout-gpt/src/layout_gpt/agent.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
__init__ ¶
__init__(
*,
model: ModelLike = None,
config: LayoutGPTConfig,
token_counter: TokenCounter = default_token_counter,
) -> None
Initialize the runner with provider and prompt configuration.
Source code in models/layout-gpt/src/layout_gpt/agent.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
build_prompt ¶
build_prompt(
prompt: str,
*,
train_examples: Sequence[LayoutExample],
seed: int | None = None,
generator: Generator | None = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
) -> tuple[str | list[ChatMessage], list[LayoutExample]]
Select exemplars and serialize the prompt sent to the model.
Source code in models/layout-gpt/src/layout_gpt/agent.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
run_sync ¶
run_sync(
prompt: str,
*,
train_examples: Sequence[LayoutExample],
model: ModelLike = None,
seed: int | None = None,
generator: Generator | None = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
model_settings: ModelSettings | None = None,
) -> LayoutGPTOutput
Run LayoutGPT and parse the response into typed layout output.
Source code in models/layout-gpt/src/layout_gpt/agent.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
__call__ ¶
__call__(
*,
prompt: str,
train_examples: Sequence[LayoutExample],
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: str
| ConditionType = ConditionType.text,
labels: Int[Tensor, "batch elements"]
| list[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| list[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| list[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: str | BoxFormat = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: str | OutputType = OutputType.dataclass,
return_intermediates: bool = False,
model: ModelLike = None,
query_embedding: EmbeddingProvider | None = None,
example_embeddings: Sequence[Sequence[float]]
| None = None,
model_settings: ModelSettings | None = None,
) -> LayoutGenerationOutput | LayoutGPTOutputDict
Generate a layout through the common public generation surface.
Source code in models/layout-gpt/src/layout_gpt/agent.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
) -> None
Persist prompt and parser configuration without provider state.
Source code in models/layout-gpt/src/layout_gpt/agent.py
227 228 229 230 231 232 233 234 235 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
*,
model: ModelLike = None,
token_counter: TokenCounter = default_token_counter,
) -> "LayoutGPTAgent"
Load saved LayoutGPT prompt and parser configuration.
Source code in models/layout-gpt/src/layout_gpt/agent.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
build_agent ¶
build_agent(model: ModelLike = None) -> Agent[None]
Build a Pydantic AI agent with provider selected by argument or env.
Source code in models/layout-gpt/src/layout_gpt/agent.py
49 50 51 52 53 54 55 56 | |
enums ¶
Closed string modes that are specific to LayoutGPT.
LayoutGPTSetting ¶
Bases: StrEnum
Supported NSR-1K LayoutGPT settings.
Source code in models/layout-gpt/src/layout_gpt/enums.py
9 10 11 12 13 | |
ICLType ¶
Bases: StrEnum
Supported in-context exemplar selection modes.
Source code in models/layout-gpt/src/layout_gpt/enums.py
16 17 18 19 20 | |
OutputType ¶
Bases: StrEnum
Public return type names.
Source code in models/layout-gpt/src/layout_gpt/enums.py
23 24 25 26 27 | |
coerce_enum ¶
coerce_enum(
value: str | EnumT, enum_type: type[EnumT]
) -> EnumT
Convert a string or enum value into the requested StrEnum.
Source code in models/layout-gpt/src/layout_gpt/enums.py
30 31 32 33 34 | |
exemplars ¶
LayoutGPT exemplar loading, selection, and serialization.
LayoutExample
dataclass
¶
One NSR-1K 2D LayoutGPT exemplar.
Source code in models/layout-gpt/src/layout_gpt/exemplars.py
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
from_vendor_record
classmethod
¶
from_vendor_record(
record: VendorRecord, *, setting: str | LayoutGPTSetting
) -> LayoutExample
Build an exemplar from the original NSR-1K JSON record.
Source code in models/layout-gpt/src/layout_gpt/exemplars.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
load_nsr_examples ¶
load_nsr_examples(
path: str | Path, *, setting: str | LayoutGPTSetting
) -> list[LayoutExample]
Load LayoutGPT NSR-1K examples from a reference-style JSON file.
Source code in models/layout-gpt/src/layout_gpt/exemplars.py
68 69 70 71 72 73 74 75 | |
select_fixed_random ¶
select_fixed_random(
examples: Sequence[LayoutExample],
*,
k: int,
seed: int = DEFAULT_FIXED_RANDOM_SEED,
) -> list[LayoutExample]
Select exemplars with the reference fixed random.seed(42) strategy.
Source code in models/layout-gpt/src/layout_gpt/exemplars.py
78 79 80 81 82 83 84 85 86 87 88 | |
select_k_similar ¶
select_k_similar(
examples: Sequence[LayoutExample],
*,
query: str,
k: int,
query_embedding: EmbeddingProvider,
example_embeddings: Sequence[Sequence[float]],
) -> list[LayoutExample]
Select top-k exemplars by CLIP-style cosine similarity.
The original code computes softmax(100 * query @ train.T) and then
topk. Softmax preserves ranking, so this implementation keeps the same
order without requiring torch or CLIP in the core package.
Source code in models/layout-gpt/src/layout_gpt/exemplars.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
parser ¶
Parse LayoutGPT CSS-like LLM output into typed layout items.
CSSProperty ¶
Bases: StrEnum
CSS declaration keys emitted by the reference LayoutGPT prompts.
Source code in models/layout-gpt/src/layout_gpt/parser.py
13 14 15 16 17 18 19 20 21 22 | |
parse_layout_line ¶
parse_layout_line(
line: str,
*,
canvas_size: int = DEFAULT_CANVAS_SIZE,
no_integer: bool = False,
) -> LayoutItem2D | None
Parse one 2D CSS line using the reference clamp/reject behavior.
Source code in models/layout-gpt/src/layout_gpt/parser.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
parse_layout_text ¶
parse_layout_text(
text: str, *, canvas_size: int = DEFAULT_CANVAS_SIZE
) -> list[LayoutItem2D]
Parse a multi-line 2D LayoutGPT response.
Source code in models/layout-gpt/src/layout_gpt/parser.py
85 86 87 88 89 90 91 92 93 94 | |
parse_3d_layout_line ¶
parse_3d_layout_line(
line: str, *, unit: str = DEFAULT_3D_UNIT
) -> LayoutItem3D | None
Parse one 3D CSS line from the reference scene-layout script.
Source code in models/layout-gpt/src/layout_gpt/parser.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
parse_3d_layout_text ¶
parse_3d_layout_text(
text: str, *, unit: str = DEFAULT_3D_UNIT
) -> list[LayoutItem3D]
Parse a multi-line 3D LayoutGPT response.
Source code in models/layout-gpt/src/layout_gpt/parser.py
120 121 122 123 124 125 126 127 128 | |
prompts ¶
Prompt serialization ported from the original LayoutGPT scripts.
default_token_counter ¶
default_token_counter(text: str) -> int
Small dependency-free fallback token counter.
Source code in models/layout-gpt/src/layout_gpt/prompts.py
13 14 15 | |
system_prompt_2d ¶
system_prompt_2d(*, canvas_size: int) -> str
Return the reference 2D instruction prompt.
Source code in models/layout-gpt/src/layout_gpt/prompts.py
18 19 20 21 22 23 24 25 26 27 28 29 30 | |
create_exemplar_prompt ¶
create_exemplar_prompt(
example: LayoutExample,
*,
canvas_size: int,
is_chat: bool = False,
) -> str
Serialize one exemplar using reference CSS property order.
Source code in models/layout-gpt/src/layout_gpt/prompts.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
form_prompt_for_chatgpt ¶
form_prompt_for_chatgpt(
text_input: str,
*,
exemplars: Sequence[LayoutExample],
canvas_size: int,
token_counter: TokenCounter = default_token_counter,
input_length_limit: int = DEFAULT_INPUT_LENGTH_LIMIT,
) -> list[ChatMessage]
Build chat messages with reference exemplar ordering and token truncation.
Source code in models/layout-gpt/src/layout_gpt/prompts.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
form_prompt_for_gpt3 ¶
form_prompt_for_gpt3(
text_input: str,
*,
exemplars: Sequence[LayoutExample],
canvas_size: int,
token_counter: TokenCounter = default_token_counter,
input_length_limit: int = DEFAULT_INPUT_LENGTH_LIMIT,
) -> str
Build completion prompt with reference exemplar ordering and truncation.
Source code in models/layout-gpt/src/layout_gpt/prompts.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
schema ¶
Typed LayoutGPT request and response schemas.
LayoutItem2D ¶
Bases: BaseModel
A parsed 2D CSS layout item.
Source code in models/layout-gpt/src/layout_gpt/schema.py
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 | |
LayoutItem3D ¶
Bases: BaseModel
A parsed 3D CSS layout item.
Source code in models/layout-gpt/src/layout_gpt/schema.py
51 52 53 54 55 56 57 58 59 60 61 62 63 | |
RawLayoutResponse ¶
Bases: BaseModel
Structured model response before CSS parsing.
Source code in models/layout-gpt/src/layout_gpt/schema.py
66 67 68 69 | |
LayoutGPTOutput ¶
Bases: BaseModel
Pydantic representation of a parsed LayoutGPT response.
Source code in models/layout-gpt/src/layout_gpt/schema.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
to_layout_generation_output ¶
to_layout_generation_output() -> LayoutGenerationOutput
Convert to the shared public layout output schema.
Source code in models/layout-gpt/src/layout_gpt/schema.py
85 86 87 88 89 90 91 92 93 94 95 96 97 | |
LayoutGPTConfig ¶
Bases: BaseModel
Runtime configuration for LayoutGPT prompt and provider behavior.
Source code in models/layout-gpt/src/layout_gpt/schema.py
100 101 102 103 104 105 106 107 108 109 110 111 112 | |
types ¶
Structured dictionary types used by LayoutGPT.
ChatMessage ¶
Bases: TypedDict
Chat-style prompt message passed to Pydantic AI.
Source code in models/layout-gpt/src/layout_gpt/types.py
8 9 10 11 12 | |
LayoutGPTIntermediates ¶
Bases: TypedDict
LayoutGPT-specific intermediate values attached to shared outputs.
Source code in models/layout-gpt/src/layout_gpt/types.py
15 16 17 18 19 20 21 | |
LayoutGPTOutputDict ¶
Bases: TypedDict
Dictionary form returned when output_type='dict'.
Source code in models/layout-gpt/src/layout_gpt/types.py
24 25 26 27 28 29 30 31 32 33 34 | |