Module: formatted_prompt

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from cl.runtime.records.protocols import TDataDict
from cl.convince.prompts.template_prompt import TemplatePrompt


@dataclass(slots=True, kw_only=True)
class FormattedPrompt(TemplatePrompt):
    """Uses Python interpolated string format for the template, param names are PascalCase in curly braces."""

    def _render(self, dict_with_pascal_case_keys: TDataDict) -> str:
        """Protected method performing the actual rendering, must throw KeyError if a parameter is missing."""
        # Throws KeyError on missing parameter as required by the method specification
        result = self.template.format(**dict_with_pascal_case_keys)
        return result

Classes

class FormattedPrompt (*, prompt_id: str = None, params_type: str = None, template: str = None)

Uses Python interpolated string format for the template, param names are PascalCase in curly braces.

Expand source code
@dataclass(slots=True, kw_only=True)
class FormattedPrompt(TemplatePrompt):
    """Uses Python interpolated string format for the template, param names are PascalCase in curly braces."""

    def _render(self, dict_with_pascal_case_keys: TDataDict) -> str:
        """Protected method performing the actual rendering, must throw KeyError if a parameter is missing."""
        # Throws KeyError on missing parameter as required by the method specification
        result = self.template.format(**dict_with_pascal_case_keys)
        return result

Ancestors

Static methods

def get_key_type() -> Type

Inherited from: TemplatePrompt.get_key_type

Return key type even when called from a record.

Fields

var params_type -> str

Inherited from: TemplatePrompt.params_type

Record whose pascalized fields are used as template parameters in ClassName format.

var prompt_id -> str

Inherited from: TemplatePrompt.prompt_id

Unique prompt identifier.

var template -> str

Inherited from: TemplatePrompt.template

Uses a template to render the prompt, param names are PascalCase in curly braces.

Methods

def get_key(self) -> PromptKey

Inherited from: TemplatePrompt.get_key

Return a new key object whose fields populated from self, do not return self.

def init_all(self) -> None

Inherited from: TemplatePrompt.init_all

Invoke ‘init’ for each class in the order from base to derived, then validate against schema.

def render(self, params: RecordMixin) -> str

Inherited from: TemplatePrompt.render

Use data from the parameters object of ‘params_type’ to render the template.