Skip to content

App Definition

Bases: SerialModel, WithClassInfo, ABC

Source code in trulens_eval/trulens_eval/schema.py
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
class AppDefinition(SerialModel, WithClassInfo, ABC):
    # Serialized fields here whereas app.py:App contains
    # non-serialized fields.

    class Config:
        arbitrary_types_allowed = True

    app_id: AppID
    tags: Tags
    metadata: Metadata  # TODO: rename to meta for consistency with other metas

    # Feedback functions to evaluate on each record. Unlike the above, these are
    # meant to be serialized.
    feedback_definitions: Sequence[FeedbackDefinition] = []

    # NOTE: Custom feedback functions cannot be run deferred and will be run as
    # if "withappthread" was set.
    feedback_mode: FeedbackMode = FeedbackMode.WITH_APP_THREAD

    # Class of the main instrumented object.
    root_class: Class  # TODO: make classvar

    # App's main method. To be filled in by subclass. Want to make this abstract
    # but this causes problems when trying to load an AppDefinition from json.
    root_callable: ClassVar[FunctionOrMethod]

    # Wrapped app in jsonized form.
    app: JSON

    # Info to store about the app and to display in dashboard. This is useful if
    # app itself cannot be serialized. `app_extra_json`, then, can stand in place for
    # whatever the user might want to see about the app.
    app_extra_json: JSON

    def jsonify_extra(self, content):
        # Called by jsonify for us to add any data we might want to add to the
        # serialization of `app`.
        if self.app_extra_json is not None:
            content['app'].update(self.app_extra_json)

        return content

    def __init__(
        self,
        app_id: Optional[AppID] = None,
        tags: Optional[Tags] = None,
        metadata: Optional[Metadata] = None,
        feedback_mode: FeedbackMode = FeedbackMode.WITH_APP_THREAD,
        app_extra_json: JSON = None,
        **kwargs
    ):

        # for us:
        kwargs['app_id'] = "temporary"  # will be adjusted below
        kwargs['feedback_mode'] = feedback_mode
        kwargs['tags'] = ""
        kwargs['metadata'] = {}
        kwargs['app_extra_json'] = app_extra_json or dict()

        # for WithClassInfo:
        kwargs['obj'] = self

        super().__init__(**kwargs)

        if app_id is None:
            app_id = obj_id_of_obj(obj=self.dict(), prefix="app")

        self.app_id = app_id

        if tags is None:
            tags = "-"  # Set tags to a "-" if None is provided
        self.tags = tags

        if metadata is None:
            metadata = {}
        self.metadata = metadata

    def dict(self):
        return jsonify(self)

    @classmethod
    def select_inputs(cls) -> JSONPath:
        """
        Get the path to the main app's call inputs.
        """

        return getattr(
            Select.RecordCalls,
            cls.root_callable.default_factory().name
        ).args

    @classmethod
    def select_outputs(cls) -> JSONPath:
        """
        Get the path to the main app's call outputs.
        """

        return getattr(
            Select.RecordCalls,
            cls.root_callable.default_factory().name
        ).rets

select_inputs() classmethod

Get the path to the main app's call inputs.

Source code in trulens_eval/trulens_eval/schema.py
500
501
502
503
504
505
506
507
508
509
@classmethod
def select_inputs(cls) -> JSONPath:
    """
    Get the path to the main app's call inputs.
    """

    return getattr(
        Select.RecordCalls,
        cls.root_callable.default_factory().name
    ).args

select_outputs() classmethod

Get the path to the main app's call outputs.

Source code in trulens_eval/trulens_eval/schema.py
511
512
513
514
515
516
517
518
519
520
@classmethod
def select_outputs(cls) -> JSONPath:
    """
    Get the path to the main app's call outputs.
    """

    return getattr(
        Select.RecordCalls,
        cls.root_callable.default_factory().name
    ).rets