Policy Environments
Main Class
PolicyEnv
The global environment accessible to policies.
It can be used for:
- Fetching prompts, data, and examples.
- Caching LLM requests.
- Tracing nodes, query, answers, and logging information.
Attributes:
Name | Type | Description |
---|---|---|
cache |
The (optional) request cache. |
|
data_manager |
The data manager. |
|
templates |
The prompt templates manager. |
|
tracer |
The tracer, which can also be used for logging. |
|
examples |
The example database. |
|
log_long_computations |
see constructor. |
Source code in src/delphyne/stdlib/environments.py
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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
__init__
__init__(
*,
prompt_dirs: Sequence[Path] = (),
demonstration_files: Sequence[Path] = (),
data_dirs: Sequence[Path] = (),
cache: LLMCache | None = None,
override_answers: AnswerDatabase | None = None,
log_level: LogLevel = "info",
log_long_computations: tuple[LogLevel, float] | None = None,
do_not_match_identical_queries: bool = False,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_dirs
|
Sequence[Path]
|
A sequence of directories where Jinja prompt templates can be found. |
()
|
demonstration_files
|
Sequence[Path]
|
A sequence of paths to demonstration
files (with or without extension |
()
|
data_dirs
|
Sequence[Path]
|
A sequence of directories where data files can be found. |
()
|
cache
|
LLMCache | None
|
A request cache, or |
None
|
override_answers
|
AnswerDatabase | None
|
If provided, a database of answers that
must be used to override LLM calls whenever possible.
Individual prompting policies such as |
None
|
log_level
|
LogLevel
|
The minimum log level to record. Messages with a lower level will be ignored. |
'info'
|
log_long_computations
|
tuple[LogLevel, float] | None
|
if set, log computations taking more
than the given number of seconds at the given severity
level. This settings can be locally overriden by
|
None
|
do_not_match_identical_queries
|
bool
|
See |
False
|
Source code in src/delphyne/stdlib/environments.py
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 |
|
overriden_answer
overriden_answer(query: AbstractQuery[Any]) -> Answer | None
Attempt to fetch an answer from the override database and return it if it exists, while logging the event.
Source code in src/delphyne/stdlib/environments.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
log
log(
level: LogLevel,
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
LogLevel
|
The severity level of the message. |
required |
message
|
str
|
The message to log. |
required |
metadata
|
object | None
|
Additional metadata to log, as a dictionary of JSON values. |
None
|
loc
|
Tree[Any, Any, Any] | AttachedQuery[Any] | None
|
Tree or attached query that the message is about, if relevant. |
None
|
Source code in src/delphyne/stdlib/environments.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
|
trace
trace(
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message with "trace" severity level.
See log
method.
Source code in src/delphyne/stdlib/environments.py
454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
debug
debug(
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message with "debug" severity level.
See log
method.
Source code in src/delphyne/stdlib/environments.py
468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
info
info(
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message with "info" severity level.
See log
method.
Source code in src/delphyne/stdlib/environments.py
482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
warn
warn(
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message with "warn" severity level.
See log
method.
Source code in src/delphyne/stdlib/environments.py
496 497 498 499 500 501 502 503 504 505 506 507 508 |
|
error
error(
message: str,
metadata: object | None = None,
*,
loc: Tree[Any, Any, Any] | AttachedQuery[Any] | None = None,
) -> None
Log a message with "error" severity level.
See log
method.
Source code in src/delphyne/stdlib/environments.py
510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
InvalidDemoFile
dataclass
Bases: Exception
Exception raised when a demonstration file could not be parsed.
Source code in src/delphyne/stdlib/answer_loaders.py
118 119 120 121 122 123 124 125 |
|
Example Database
Example
dataclass
An example, usable for few-shot prompting.
Attributes:
Name | Type | Description |
---|---|---|
query |
SerializedQuery
|
The corresponding serialized query. |
answer |
Answer
|
The answer to the query. |
tags |
Sequence[str]
|
A sequence of tags associated with the example, which policies can use to select appropriate examples. |
Source code in src/delphyne/stdlib/environments.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
ExampleDatabase
dataclass
A simple example database.
Attributes:
Name | Type | Description |
---|---|---|
do_not_match_identical_queries |
bool
|
If set to |
Source code in src/delphyne/stdlib/environments.py
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 |
|
add_query_demonstration
add_query_demonstration(demo: QueryDemo)
Add all examples from a standalone query demonstration to the database.
Source code in src/delphyne/stdlib/environments.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
add_demonstration
add_demonstration(demo: Demo)
Add all examples from a demonstration to the database.
Source code in src/delphyne/stdlib/environments.py
154 155 156 157 158 159 160 161 162 163 |
|
examples
examples(query: SerializedQuery) -> Iterable[Example]
Obtain all potential examples that can be used for few-shot prompting with a given query.
Source code in src/delphyne/stdlib/environments.py
165 166 167 168 169 170 171 172 173 174 |
|
SerializedQuery
dataclass
A hashable representation of a query.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the query. |
args |
str
|
The serialized arguments of the query, as a canonical JSON string. Object keys are sorted so that equality is defined modulo key order. |
Source code in src/delphyne/core/answer_databases.py
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 |
|
Data and Templates Managers
DataManager
Utility class for loading and accessing external data.
Attributes:
Name | Type | Description |
---|---|---|
data |
A dictionary containing all loaded data files. Each file corresponds to a key in the dictionary (stripped of the extension). |
Source code in src/delphyne/stdlib/environments.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
__init__
__init__(data_dirs: Sequence[Path])
Find all files with extension *.data.yaml
in the data_dirs
,
parse them and save everything in a big dict. If two files have
the same name, raise an error.
Source code in src/delphyne/stdlib/environments.py
39 40 41 42 43 44 45 |
|
TemplatesManager
Bases: AbstractTemplatesManager
A class for managing Jinja prompt templates.
Templates are configured with the trim_blocks
and lstrip_blocks
options set to True
(no newlines are inserted after blocks and
indentation can be used within blocks without affecting the output).
The keep_trailing_newline
option is set to False
so trailing new
lines at the end of template files are ignored.
Templates are first searched in the provided prompt folders and then
in the standard library (delphyne.stdlib.templates
). For example,
to show standard formatting instructions, you can include the
following in your instance prompts:
{% include 'stdlib/format.jinja' %}
All templates automatically have access to the following global objects:
- A
yaml
filter for converting an object into a YAML string. - A
json
filter for converting an object into a JSON string. - A
fail
function that takes an error message as an argument and raises an exception on Python side. - A
data
dictionary containing all loaded data files.
Source code in src/delphyne/stdlib/environments.py
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 |
|
__init__
__init__(prompt_dirs: Sequence[Path], data_manager: DataManager)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_dirs
|
Sequence[Path]
|
A sequence of directories where Jinja prompt templates can be found. |
required |
data_manager
|
DataManager
|
A sequence of directories where data files can be found. |
required |
Source code in src/delphyne/stdlib/environments.py
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 |
|
TemplateFileMissing
dataclass
Bases: Exception
Exception raised when a template file is missing.
This exception should only be raised when a top-level template file
is missing. If an include
statement fails within a template, a
TemplateError
exception should be raised instead.
Source code in src/delphyne/core/queries.py
144 145 146 147 148 149 150 151 152 153 154 |
|
Tracer
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. |
Source code in src/delphyne/core/traces.py
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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
|
__init__
__init__(log_level: LogLevel = 'info')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_level
|
LogLevel
|
The minimum severity level of messages to log. |
'info'
|
Source code in src/delphyne/core/traces.py
586 587 588 589 590 591 592 593 594 595 596 597 |
|
global_node_id
global_node_id(node: GlobalNodePath) -> NodeId
Ensure that a node at a given reference is present in the trace and return the corresponding node identififier.
Source code in src/delphyne/core/traces.py
599 600 601 602 603 604 605 |
|
trace_node
trace_node(node: GlobalNodePath) -> None
Ensure that a node at a given reference is present in the trace.
Returns the associated node identifier.
See tracer_hook
for registering a hook that automatically
calls this method on all encountered nodes.
Source code in src/delphyne/core/traces.py
607 608 609 610 611 612 613 614 615 616 |
|
trace_query
trace_query(query: AttachedQuery[Any]) -> 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
618 619 620 621 622 623 624 625 |
|
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
627 628 629 630 631 632 633 634 635 |
|
log
log(
level: LogLevel,
message: str,
metadata: object | None = None,
location: Location | None = None,
)
Log a message, with optional metadata and location information. The metadata must be exportable to JSON using Pydantic.
Source code in src/delphyne/core/traces.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
|
export_log
export_log(*, remove_timing_info: bool = False) -> Iterable[ExportableLogMessage]
Export the log into an easily serializable format.
Source code in src/delphyne/core/traces.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
|
export_trace
export_trace() -> ExportableTrace
Export the trace into an easily serializable format.
Source code in src/delphyne/core/traces.py
688 689 690 691 692 693 |
|