References and Traces
References Summary
delphyne.core.refs
References to nodes, values, spaces and space elements.
References are serializable, immutable values that can be used to
identify nodes, spaces and values in a tree (possibly deeply nested).
References are useful for tooling and for representing serializable
traces (Trace
). Also, references are attached to success nodes and
query answers (Tracked
) so as to allow caching and enforce the
locality invariant (see Tree
).
Local references identify a node, space or space element relative to a given tree node. Global references are expressed relative to a single, global origin.
In addition, three kinds of references can be distinguished:
- Full references: the default kind of references produced by
reify
. Query answers are stored as strings and elements of spaces induced by strategies are denoted by sequences of value references. - Id-based references: shorter references, where query answers and
success values are identified by unique identifiers. This concise
format is used for exporting traces (see
Trace
). - Hint-based references: query answers and success values are
identified by sequences of hints. This format is used in the
demonstration language (e.g. argument of test instruction
go compare(['', 'foo bar'])
) and when visualizing traces resulting from demonstrations.
Query Answers
Answer
dataclass
An answer to a query.
It can serve as a space element reference if the space in question is a query and the proposed answer correctly parses.
Attributes:
Name | Type | Description |
---|---|---|
mode |
AnswerMode
|
The answer mode (see |
content |
str | Structured
|
The answer content, which can be a raw string or a
structured answer (see |
tool_calls |
tuple[ToolCall, ...]
|
An optional sequence of tool calls. |
justification |
str | None
|
Additional explanations for the answers, which are not passed to the parser but can be appended at the end of the answer in examples. In particular, this is useful when defining queries for which the oracle is not asked to produce a justification for its answer, but justifications can still be provided in examples for the sake of few-shot prompting. |
Source code in src/delphyne/core/refs.py
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 |
|
AnswerMode
AnswerMode: str | None
A name for an answer mode, which can be a string or None
(the latter
is typically used for naming default modes).
Queries are allowed to define multiple answer modes, each mode being
possibly associated with different settings and with a different parser.
An Answer
value features the mode that must be used to parse it.
Structured
dataclass
Wrapper for structured LLM answers.
Many LLM APIs allow producing JSON answers (sometimes following a given schema) instead of plain text.
Source code in src/delphyne/core/refs.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
ToolCall
dataclass
A tool call, usually produced by an LLM oracle.
Tool calls can be attached to LLM answers (see Answer
).
Source code in src/delphyne/core/refs.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
References
NodeRef
A node reference is either a path or a node identifier.
Only one of these forms may be allowed depending on the context (e.g. in
the id-based references used for exporting traces, only node identifiers
are used, while in the full references attached to trees by reify
,
only paths are used).
NodePath
NodePath: tuple[ValueRef, ...]
Encodes a sequence of actions leading to a node with respect to a given root.
NodeId
dataclass
Global identifier of a node within a trace.
Source code in src/delphyne/core/refs.py
186 187 188 189 190 191 192 |
|
ValueRef
ValueRef: Assembly[AtomicValueRef]
A reference to a local value, which is obtained by combining elements of (possibly multiple) local spaces.
AtomicValueRef
AtomicValueRef: IndexedRef | SpaceElementRef
An atomic value reference is a space element reference that is indexed
zero or a finite number of times: space_elt_ref[i1][i2]...[in]
.
IndexedRef
dataclass
Indexing an atomic value reference.
Source code in src/delphyne/core/refs.py
148 149 150 151 152 153 154 155 |
|
SpaceElementRef
dataclass
A reference to an element of a local space.
When the space
field is None
, the primary field is considered
instead (if it exists).
Source code in src/delphyne/core/refs.py
274 275 276 277 278 279 280 281 282 283 284 |
|
SpaceRef
dataclass
A reference to a specific local space.
The arg
argument should be ()
for nonparametric spaces and a
n-uple for spaces parametric in n arguments. This differs from
Orakell where all parametric spaces have one argument.
Source code in src/delphyne/core/refs.py
206 207 208 209 210 211 212 213 214 215 216 217 |
|
SpaceName
dataclass
A name identifying a parametric space.
This name can feature integer indices. For example, subs[0]
denotes the first subgoal of a Join
node.
Source code in src/delphyne/core/refs.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
AnswerId
dataclass
The identifier to an Answer
object stored within a trace.
Source code in src/delphyne/core/refs.py
232 233 234 235 236 237 238 |
|
HintsRef
dataclass
References a local space element via a sequence of hints.
Source code in src/delphyne/core/refs.py
265 266 267 268 269 270 271 |
|
Hint
dataclass
A hint for selecting a query answer.
A hint can be associated to a qualifier, which is the name of an imported demonstration defining the hint.
Source code in src/delphyne/core/refs.py
253 254 255 256 257 258 259 260 261 262 |
|
HintValue
HintValue: str
A string that hints at a query answer.
GlobalNodePath
Path to a node from the global origin, as a sequence of (space to enter, path to follow) instruction pairs.
NodeOrigin
NodeOrigin: ChildOf | NestedTreeOf
Origin of a tree.
A tree is either the child of another tree or the root of a nested tree.
Traces can be exported as mappings from node identifiers to node origin
information featuring id-based references (see Trace
).
ChildOf
dataclass
The tree of interest is the child of another one.
Source code in src/delphyne/core/refs.py
314 315 316 317 318 319 320 321 |
|
NestedTreeOf
dataclass
The tree of interest is the root of a tree that induces a given space.
Source code in src/delphyne/core/refs.py
324 325 326 327 328 329 330 331 332 |
|
Tracked Values
Tracked
dataclass
Bases: Generic[T]
A tracked value, which pairs a value with a reference.
Attributes:
Name | Type | Description |
---|---|---|
value |
T
|
The value being tracked. |
ref |
AtomicValueRef
|
A local reference to the value, relative to the node
reference by the |
node |
GlobalNodePath
|
A global reference to the node to which the space that the
value originates from is attached. In particular, this field
is useful to check the locality invariant at runtime (e.g.,
when passing a tracked value to |
type_annot |
TypeAnnot[T] | NoTypeInfo
|
An optional type annotation for the |
Tracked sequences (or pairs) can be indexed using __getitem__
,
resulting in tracked values with IndexedRef
references. Since
__getitem__
is defined, tracked values are also iterable.
Source code in src/delphyne/core/refs.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
|
Value
Value: ExtAssembly[Tracked[Any]]
An assembly of local, tracked values.
Values can serve as actions or space parameters.
ExtAssembly
ExtAssembly: T | None | Sequence[ExtAssembly[T]]
Generalizing Assembly
to allow arbitrary sequences (and not just
tuples). The distinction is important because ValueRef
needs to be
hashable and so cannot contain lists, while Value
can contain lists.
check_local_value
check_local_value(val: Value, node: GlobalNodePath)
Raise a LocalityError
exception if a given value is not a local
value relative to a given node.
Source code in src/delphyne/core/refs.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
|
LocalityError
dataclass
Bases: Exception
Exception raised when the locality invariant is violated.
See Tree
and check_local_value
.
Source code in src/delphyne/core/refs.py
404 405 406 407 408 409 410 411 412 413 414 |
|
Traces
Trace
A collection of reachable nodes and spaces, which is encoded in a concise way by introducing unique identifiers for answers and nodes.
Traces are mutable. Methods are provided to convert full references into id-based references, creating fresh identifiers for new nodes and queries on the fly. Backward conversion methods are also provided for converting id-based references back into full references (assuming id-based references are valid, without which these methods fail with assertion errors).
Attributes:
Name | Type | Description |
---|---|---|
nodes |
dict[NodeId, NodeOrigin]
|
a mapping from node identifiers to their origin. |
node_ids |
dict[NodeOrigin, NodeId]
|
reverse map of |
answers |
dict[AnswerId, tuple[QueryOrigin, Answer]]
|
a mapping from answer identifiers to actual answers, along with origin information on the associated query. |
answer_ids |
dict[QueryOrigin, dict[Answer, AnswerId]]
|
reverse map of |
Source code in src/delphyne/core/traces.py
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
__init__
__init__()
Create an empty trace.
Source code in src/delphyne/core/traces.py
128 129 130 131 132 133 134 135 136 137 138 139 |
|
fresh_or_cached_node_id
fresh_or_cached_node_id(origin: NodeOrigin) -> NodeId
Obtain the identifier of a node described by its origin. Create a new identifier on the fly if it does not exist yet.
Source code in src/delphyne/core/traces.py
141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
fresh_or_cached_answer_id
fresh_or_cached_answer_id(answer: Answer, origin: QueryOrigin) -> AnswerId
Obtain the identifier of an answer, given its content and the origin of the query that it corresponds to. Create a new, fresh identifier on the fly if it does not exist yet.
Source code in src/delphyne/core/traces.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
register_query
register_query(origin: QueryOrigin) -> None
Ensure that a query appears in the trace, even if not answers are associated with it yet.
This is particularly useful for the demonstration interpreter. Indeed, when a test gets stuck on an unanswered query, it is desirable for this query to be part of the returned trace so that the user can visualize it.
Source code in src/delphyne/core/traces.py
174 175 176 177 178 179 180 181 182 183 184 185 |
|
export
export() -> ExportableTrace
Export a trace into a lightweight, serializable format.
Source code in src/delphyne/core/traces.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
check_consistency
check_consistency() -> None
Perform a sanity check on the trace.
Each node identifier is expanded into a full reference and then converted back to an identifier, which must be equal to the original one.
Source code in src/delphyne/core/traces.py
202 203 204 205 206 207 208 209 210 211 212 |
|
convert_location
convert_location(location: Location) -> ShortLocation
Convert a full location into an id-based one.
Source code in src/delphyne/core/traces.py
216 217 218 219 220 221 222 223 224 |
|
convert_query_origin
convert_query_origin(ref: GlobalSpacePath) -> QueryOrigin
Convert a full, global space reference denoting a quey origin into an id-based reference.
Source code in src/delphyne/core/traces.py
226 227 228 229 230 231 232 233 234 235 |
|
convert_answer_ref
convert_answer_ref(ref: tuple[GlobalSpacePath, Answer]) -> AnswerId
Convert a full answer reference into an answer id.
Source code in src/delphyne/core/traces.py
237 238 239 240 241 242 243 244 245 246 247 |
|
convert_global_node_path
convert_global_node_path(path: GlobalNodePath) -> NodeId
Convert a full, global node reference into an id-based one.
Source code in src/delphyne/core/traces.py
249 250 251 252 253 254 255 256 257 258 259 260 |
|
convert_global_space_path
convert_global_space_path(path: GlobalSpacePath) -> SpaceRef
Convert a full global space reference into an id-based one.
Source code in src/delphyne/core/traces.py
262 263 264 265 266 267 268 269 270 |
|
expand_space_ref
Convert a local id-based space reference into a full one, relative to a given node.
Source code in src/delphyne/core/traces.py
350 351 352 353 354 355 356 357 358 |
|
expand_value_ref
Convert a local id-based value reference into a full one, relative to a given node.
Source code in src/delphyne/core/traces.py
360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
expand_node_id
expand_node_id(id: NodeId) -> GlobalNodePath
Convert a node identifier into a full, global node reference.
Source code in src/delphyne/core/traces.py
374 375 376 377 378 379 380 381 382 |
|
QueryOrigin
dataclass
A global, id-based reference to the space induced by a query.
Source code in src/delphyne/core/traces.py
96 97 98 99 100 101 102 103 |
|
ExportableTrace
dataclass
A lightweight trace format that can be easily exported to JSON/YAML.
Attributes:
Name | Type | Description |
---|---|---|
nodes |
dict[int, NodeOriginStr]
|
a mapping from node ids to serialized origin information. |
queries |
list[ExportableQueryInfo]
|
a list of encountered queries with associated answers. |
Source code in src/delphyne/core/traces.py
77 78 79 80 81 82 83 84 85 86 87 88 |
|
ExportableQueryInfo
dataclass
Information about a query encountered in an exportable trace.
Attributes:
Name | Type | Description |
---|---|---|
node |
int
|
Identifier of the node that the query is attached to. |
space |
str
|
Local, id-based reference of the space that the query
belongs to. Serialized using |
answers |
dict[int, Answer]
|
Mapping from answer identifiers to actual answers.
Answer identifiers are unique across a whole exportable
trace (and not only across an |
Source code in src/delphyne/core/traces.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
NodeOriginStr
NodeOriginStr: str
A concise, serialized representation for NodeOrigin
.
Location
dataclass
A full, global reference to either a node or a space.
This is useful in particular for attaching location information to logging messages.
Source code in src/delphyne/core/traces.py
19 20 21 22 23 24 25 26 27 28 29 |
|
ShortLocation
dataclass
An id-based, global reference to either a node or a space.
This is the id-based counterpart of Location
. Policies typically
log messages with Location
values attached (since trees feature
full references), which are then converted into ShortLocation
in
the final exportable log.
Source code in src/delphyne/core/traces.py
32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Tracers
Tracer
A mutable trace along with a mutable list of log messages.
Both components are protected by a lock to ensure thread-safety (some policies spawn multiple concurrent threads).
Attributes:
Name | Type | Description |
---|---|---|
trace |
A mutable trace. |
|
messages |
list[LogMessage]
|
A mutable list of log messages. |
lock |
A reentrant lock protecting access to the trace and log. The lock is publicly exposed so that threads can log several successive messages without other threads interleaving new messages in between (TODO: there are cleaner ways to achieve this). |
Source code in src/delphyne/core/traces.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
trace_node
trace_node(node: GlobalNodePath) -> None
Ensure that a node at a given reference is present in the trace.
See tracer_hook
for registering a hook that automatically
calls this method on all encountered nodes.
Source code in src/delphyne/core/traces.py
541 542 543 544 545 546 547 548 549 |
|
trace_query
trace_query(ref: GlobalSpacePath) -> None
Ensure that a query at a given reference is present in the trace, even if no answer is provided for it.
Source code in src/delphyne/core/traces.py
551 552 553 554 555 556 557 |
|
trace_answer
trace_answer(space: GlobalSpacePath, answer: Answer) -> None
Ensure that a given query answer is present in the trace, even it is is not used to reach a node.
Source code in src/delphyne/core/traces.py
559 560 561 562 563 564 565 566 567 |
|
log
log(
message: str,
metadata: dict[str, Any] | None = None,
location: Location | None = None,
)
Log a message, with optional metadata and location information. The metadata must be a dictionary of JSON values.
Source code in src/delphyne/core/traces.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
|
export_log
export_log() -> Iterable[ExportableLogMessage]
Export the log into an easily serializable format.
Source code in src/delphyne/core/traces.py
585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
export_trace
export_trace() -> ExportableTrace
Export the trace into an easily serializable format.
Source code in src/delphyne/core/traces.py
599 600 601 602 603 604 |
|
LogMessage
dataclass
A log message.
Attributes:
Name | Type | Description |
---|---|---|
message |
str
|
The message to log. |
metadata |
dict[str, Any] | None
|
Optional metadata associated with the message, as a dictionary mapping string keys to JSON values. |
location |
ShortLocation | None
|
An optional location in the strategy tree where the message was logged, if applicable. |
Source code in src/delphyne/core/traces.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
ExportableLogMessage
dataclass
An exportable log message, as a dataclass whose fields are JSON
values (as opposed to LogMessage
) and is thus easier to export.
Source code in src/delphyne/core/traces.py
503 504 505 506 507 508 509 510 511 512 513 |
|
tracer_hook
Standard hook to be passed to TreeMonitor
to automatically log
visited nodes into a trace.
Source code in src/delphyne/core/traces.py
607 608 609 610 611 612 |
|