Prompts
patronus.prompts
clients
PromptNotFoundError
PromptNotFoundError(
name: str,
project: Optional[str] = None,
revision: Optional[int] = None,
label: Optional[str] = None,
)
Bases: Exception
Raised when a prompt could not be found.
Source code in src/patronus/prompts/clients.py
PromptProviderError
Bases: Exception
Base class for prompt provider errors.
PromptProviderConnectionError
Bases: PromptProviderError
Raised when there's a connectivity issue with the prompt provider.
PromptProviderAuthenticationError
Bases: PromptProviderError
Raised when there's an authentication issue with the prompt provider.
PromptProvider
Bases: ABC
get_prompt
abstractmethod
get_prompt(
name: str,
revision: Optional[int],
label: Optional[str],
project: str,
engine: TemplateEngine,
) -> Optional[LoadedPrompt]
Get prompts, returns None if prompt was not found
PromptClientMixin
PromptClient
Bases: PromptClientMixin
Source code in src/patronus/prompts/clients.py
get
get(
name: str,
revision: Optional[int] = None,
label: Optional[str] = None,
project: Union[str, Type[NOT_GIVEN]] = NOT_GIVEN,
disable_cache: bool = False,
provider: Union[
PromptProvider,
_DefaultProviders,
Sequence[Union[PromptProvider, _DefaultProviders]],
Type[NOT_GIVEN],
] = NOT_GIVEN,
engine: Union[
TemplateEngine,
DefaultTemplateEngines,
Type[NOT_GIVEN],
] = NOT_GIVEN,
) -> LoadedPrompt
Get the prompt. If neither revision nor label is specified then the prompt with latest revision is returned.
Project is loaded from the config by default. You can specify the project name of the prompt if you want to override the value from the config.
By default, once a prompt is retrieved it's cached. You can disable caching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the prompt to retrieve. |
required |
revision
|
Optional[int]
|
Optional specific revision number to retrieve. If not specified, the latest revision is used. |
None
|
label
|
Optional[str]
|
Optional label to filter by. If specified, only prompts with this label will be returned. |
None
|
project
|
Union[str, Type[NOT_GIVEN]]
|
Optional project name override. If not specified, the project name from config is used. |
NOT_GIVEN
|
disable_cache
|
bool
|
If True, bypasses the cache for both reading and writing. |
False
|
provider
|
Union[PromptProvider, _DefaultProviders, Sequence[Union[PromptProvider, _DefaultProviders]], Type[NOT_GIVEN]]
|
The provider(s) to use for retrieving prompts. Can be a string identifier ('local', 'api'), a PromptProvider instance, or a sequence of these. If not specified, defaults to config setting. |
NOT_GIVEN
|
engine
|
Union[TemplateEngine, DefaultTemplateEngines, Type[NOT_GIVEN]]
|
The template engine to use for rendering prompts. Can be a string identifier ('f-string', 'mustache', 'jinja2') or a TemplateEngine instance. If not specified, defaults to config setting. |
NOT_GIVEN
|
Returns:
Name | Type | Description |
---|---|---|
LoadedPrompt |
LoadedPrompt
|
The retrieved prompt object. |
Raises:
Type | Description |
---|---|
PromptNotFoundError
|
If the prompt could not be found with the specified parameters. |
ValueError
|
If the provided provider or engine is invalid. |
PromptProviderError
|
If there was an error communicating with the prompt provider. |
Source code in src/patronus/prompts/clients.py
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 |
|
push
push(
prompt: Prompt,
project: Union[str, Type[NOT_GIVEN]] = NOT_GIVEN,
engine: Union[
TemplateEngine,
DefaultTemplateEngines,
Type[NOT_GIVEN],
] = NOT_GIVEN,
) -> LoadedPrompt
Push a prompt to the API, creating a new revision only if needed.
If a prompt revision with the same normalized body and metadata already exists, the existing revision will be returned. If the metadata differs, a new revision will be created.
The engine parameter is only used to set property on output LoadedPrompt object. It is not persisted in any way and doesn't affect how the prompt is stored in Patronus AI Platform.
Note that when a new prompt definition is created, the description is used as provided. However, when creating a new revision for an existing prompt definition, the description parameter doesn't update the existing prompt definition's description.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
Prompt
|
The prompt to push |
required |
project
|
Union[str, Type[NOT_GIVEN]]
|
Optional project name override. If not specified, the project name from config is used. |
NOT_GIVEN
|
engine
|
Union[TemplateEngine, DefaultTemplateEngines, Type[NOT_GIVEN]]
|
The template engine to use for rendering the returned prompt. If not specified, defaults to config setting. |
NOT_GIVEN
|
Returns:
Name | Type | Description |
---|---|---|
LoadedPrompt |
LoadedPrompt
|
The created or existing prompt revision |
Raises:
Type | Description |
---|---|
PromptProviderError
|
If there was an error communicating with the prompt provider. |
Source code in src/patronus/prompts/clients.py
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 |
|
AsyncPromptClient
Bases: PromptClientMixin
Source code in src/patronus/prompts/clients.py
get
async
get(
name: str,
revision: Optional[int] = None,
label: Optional[str] = None,
project: Union[str, Type[NOT_GIVEN]] = NOT_GIVEN,
disable_cache: bool = False,
provider: Union[
PromptProvider,
_DefaultProviders,
Sequence[Union[PromptProvider, _DefaultProviders]],
Type[NOT_GIVEN],
] = NOT_GIVEN,
engine: Union[
TemplateEngine,
DefaultTemplateEngines,
Type[NOT_GIVEN],
] = NOT_GIVEN,
) -> LoadedPrompt
Get the prompt asynchronously. If neither revision nor label is specified then the prompt with latest revision is returned.
Project is loaded from the config by default. You can specify the project name of the prompt if you want to override the value from the config.
By default, once a prompt is retrieved it's cached. You can disable caching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the prompt to retrieve. |
required |
revision
|
Optional[int]
|
Optional specific revision number to retrieve. If not specified, the latest revision is used. |
None
|
label
|
Optional[str]
|
Optional label to filter by. If specified, only prompts with this label will be returned. |
None
|
project
|
Union[str, Type[NOT_GIVEN]]
|
Optional project name override. If not specified, the project name from config is used. |
NOT_GIVEN
|
disable_cache
|
bool
|
If True, bypasses the cache for both reading and writing. |
False
|
provider
|
Union[PromptProvider, _DefaultProviders, Sequence[Union[PromptProvider, _DefaultProviders]], Type[NOT_GIVEN]]
|
The provider(s) to use for retrieving prompts. Can be a string identifier ('local', 'api'), a PromptProvider instance, or a sequence of these. If not specified, defaults to config setting. |
NOT_GIVEN
|
engine
|
Union[TemplateEngine, DefaultTemplateEngines, Type[NOT_GIVEN]]
|
The template engine to use for rendering prompts. Can be a string identifier ('f-string', 'mustache', 'jinja2') or a TemplateEngine instance. If not specified, defaults to config setting. |
NOT_GIVEN
|
Returns:
Name | Type | Description |
---|---|---|
LoadedPrompt |
LoadedPrompt
|
The retrieved prompt object. |
Raises:
Type | Description |
---|---|
PromptNotFoundError
|
If the prompt could not be found with the specified parameters. |
ValueError
|
If the provided provider or engine is invalid. |
PromptProviderError
|
If there was an error communicating with the prompt provider. |
Source code in src/patronus/prompts/clients.py
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 605 606 |
|
push
async
push(
prompt: Prompt,
project: Union[str, Type[NOT_GIVEN]] = NOT_GIVEN,
engine: Union[
TemplateEngine,
DefaultTemplateEngines,
Type[NOT_GIVEN],
] = NOT_GIVEN,
) -> LoadedPrompt
Push a prompt to the API asynchronously, creating a new revision only if needed.
If a prompt revision with the same normalized body and metadata already exists, the existing revision will be returned. If the metadata differs, a new revision will be created.
The engine parameter is only used to set property on output LoadedPrompt object. It is not persisted in any way and doesn't affect how the prompt is stored in Patronus AI Platform.
Note that when a new prompt definition is created, the description is used as provided. However, when creating a new revision for an existing prompt definition, the description parameter doesn't update the existing prompt definition's description.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
Prompt
|
The prompt to push |
required |
project
|
Union[str, Type[NOT_GIVEN]]
|
Optional project name override. If not specified, the project name from config is used. |
NOT_GIVEN
|
engine
|
Union[TemplateEngine, DefaultTemplateEngines, Type[NOT_GIVEN]]
|
The template engine to use for rendering the returned prompt. If not specified, defaults to config setting. |
NOT_GIVEN
|
Returns:
Name | Type | Description |
---|---|---|
LoadedPrompt |
LoadedPrompt
|
The created or existing prompt revision |
Raises:
Type | Description |
---|---|
PromptProviderError
|
If there was an error communicating with the prompt provider. |
Source code in src/patronus/prompts/clients.py
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 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
models
BasePrompt
with_engine
Create a new prompt with the specified template engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
Union[TemplateEngine, DefaultTemplateEngines]
|
Either a TemplateEngine instance or a string identifier ('f-string', 'mustache', 'jinja2') |
required |
Returns:
Type | Description |
---|---|
Self
|
A new prompt instance with the specified engine |
Source code in src/patronus/prompts/models.py
render
Render the prompt template with the provided arguments.
If no engine is set on the prompt, the default engine from context/config will be used. If no arguments are provided, the template body is returned as-is.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Template arguments to be rendered in the prompt body |
{}
|
Returns:
Type | Description |
---|---|
str
|
The rendered prompt |
Source code in src/patronus/prompts/models.py
calculate_normalized_body_hash
Calculate the SHA-256 hash of normalized prompt body.
Normalization is done by stripping whitespace from the start and end of the body.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
body
|
str
|
The prompt body |
required |
Returns:
Type | Description |
---|---|
str
|
SHA-256 hash of the normalized body |
Source code in src/patronus/prompts/models.py
templating
TemplateEngine
get_template_engine
Convert a template engine name to an actual engine instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
Union[TemplateEngine, DefaultTemplateEngines]
|
Either a template engine instance or a string identifier ('f-string', 'mustache', 'jinja2') |
required |
Returns:
Type | Description |
---|---|
TemplateEngine
|
A template engine instance |
Raises:
Type | Description |
---|---|
ValueError
|
If the provided engine string is not recognized |