Skip to content

trulens.core.database.sqlalchemy

trulens.core.database.sqlalchemy

Attributes

Classes

SQLAlchemyDB

Bases: DB

Database implemented using sqlalchemy.

See abstract class DB for method reference.

Attributes
redact_keys class-attribute instance-attribute

Redact secrets before writing out data.

table_prefix class-attribute instance-attribute
table_prefix: str = DEFAULT_DATABASE_PREFIX

The prefix to use for all table names.

DB interface requirement.

engine_params class-attribute instance-attribute
engine_params: dict = Field(default_factory=dict)

Sqlalchemy-related engine params.

session_params class-attribute instance-attribute
session_params: dict = Field(default_factory=dict)

Sqlalchemy-related session.

engine class-attribute instance-attribute
engine: Optional[Engine] = None

Sqlalchemy engine.

session class-attribute instance-attribute
session: Optional[sessionmaker] = None

Sqlalchemy session(maker).

orm instance-attribute
orm: Type[ORM]

Container of all the ORM classes for this database.

This should be set to a subclass of ORM upon initialization.

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

from_tru_args classmethod
from_tru_args(
    database_url: Optional[str] = None,
    database_engine: Optional[Engine] = None,
    database_file: Optional[str] = None,
    database_redact_keys: Optional[
        bool
    ] = mod_db.DEFAULT_DATABASE_REDACT_KEYS,
    database_prefix: Optional[
        str
    ] = mod_db.DEFAULT_DATABASE_PREFIX,
    **kwargs: Dict[str, Any]
) -> SQLAlchemyDB

Process database-related configuration provided to the Tru class to create a database.

Emits warnings if appropriate.

from_db_url classmethod
from_db_url(
    url: str, **kwargs: Dict[str, Any]
) -> SQLAlchemyDB

Create a database for the given url.

PARAMETER DESCRIPTION
url

The database url. This includes database type.

TYPE: str

kwargs

Additional arguments to pass to the database constructor.

TYPE: Dict[str, Any] DEFAULT: {}

RETURNS DESCRIPTION
SQLAlchemyDB

A database instance.

from_db_engine classmethod
from_db_engine(
    engine: Engine, **kwargs: Dict[str, Any]
) -> SQLAlchemyDB

Create a database for the given engine. Args: engine: The database engine. kwargs: Additional arguments to pass to the database constructor. Returns: A database instance.

check_db_revision
check_db_revision()
migrate_database
migrate_database(prior_prefix: Optional[str] = None)
reset_database
reset_database()
insert_record
insert_record(record: Record) -> RecordID
batch_insert_record
batch_insert_record(
    records: List[Record],
) -> List[RecordID]

See [DB.batch_insert_record][trulens_eval.database.base.DB.batch_insert_record].

get_app
get_app(app_id: AppID) -> Optional[JSONized]

See DB.get_app.

get_apps
get_apps() -> Iterable[JSON]
insert_app
insert_app(app: AppDefinition) -> AppID
delete_app
delete_app(app_id: AppID) -> None

Deletes an app from the database based on its app_id.

PARAMETER DESCRIPTION
app_id

The unique identifier of the app to be deleted.

TYPE: AppID

insert_feedback_definition
insert_feedback_definition(
    feedback_definition: FeedbackDefinition,
) -> FeedbackDefinitionID
get_feedback_defs
get_feedback_defs(
    feedback_definition_id: Optional[
        FeedbackDefinitionID
    ] = None,
) -> DataFrame
insert_feedback
insert_feedback(
    feedback_result: FeedbackResult,
) -> FeedbackResultID
batch_insert_feedback
batch_insert_feedback(
    feedback_results: List[FeedbackResult],
) -> List[FeedbackResultID]

See [DB.batch_insert_feedback][trulens_eval.database.base.DB.batch_insert_feedback].

get_feedback_count_by_status
get_feedback_count_by_status(
    record_id: Optional[RecordID] = None,
    feedback_result_id: Optional[FeedbackResultID] = None,
    feedback_definition_id: Optional[
        FeedbackDefinitionID
    ] = None,
    status: Optional[
        Union[
            FeedbackResultStatus,
            Sequence[FeedbackResultStatus],
        ]
    ] = None,
    last_ts_before: Optional[datetime] = None,
    offset: Optional[int] = None,
    limit: Optional[int] = None,
    shuffle: bool = False,
    run_location: Optional[FeedbackRunLocation] = None,
) -> Dict[FeedbackResultStatus, int]
get_feedback
get_feedback(
    record_id: Optional[RecordID] = None,
    feedback_result_id: Optional[FeedbackResultID] = None,
    feedback_definition_id: Optional[
        FeedbackDefinitionID
    ] = None,
    status: Optional[
        Union[
            FeedbackResultStatus,
            Sequence[FeedbackResultStatus],
        ]
    ] = None,
    last_ts_before: Optional[datetime] = None,
    offset: Optional[int] = None,
    limit: Optional[int] = None,
    shuffle: Optional[bool] = False,
    run_location: Optional[FeedbackRunLocation] = None,
) -> DataFrame
get_records_and_feedback
get_records_and_feedback(
    app_ids: Optional[List[str]] = None,
    offset: Optional[int] = None,
    limit: Optional[int] = None,
) -> Tuple[DataFrame, Sequence[str]]
insert_ground_truth
insert_ground_truth(
    ground_truth: GroundTruth,
) -> GroundTruthID
batch_insert_ground_truth
batch_insert_ground_truth(
    ground_truths: List[GroundTruth],
) -> List[GroundTruthID]

See [DB.batch_insert_ground_truth][trulens_eval.database.base.DB.batch_insert_ground_truth].

get_ground_truth
get_ground_truth(
    ground_truth_id: str | None = None,
) -> Optional[JSONized]
get_ground_truths_by_dataset
get_ground_truths_by_dataset(
    dataset_name: str,
) -> DataFrame | None
insert_dataset
insert_dataset(dataset: Dataset) -> DatasetID
get_datasets
get_datasets() -> DataFrame

AppsExtractor

Utilities for creating dataframes from orm instances.

Functions
get_df_and_cols
get_df_and_cols(
    apps: Optional[
        List["mod_orm.ORM.AppDefinition"]
    ] = None,
    records: Optional[List["mod_orm.ORM.Record"]] = None,
) -> Tuple[DataFrame, Sequence[str]]

Produces a records dataframe which joins in information from apps and feedback results.

PARAMETER DESCRIPTION
apps

If given, includes all records of all of the apps in this iterable.

TYPE: Optional[List['mod_orm.ORM.AppDefinition']] DEFAULT: None

records

If given, includes only these records. Mutually exclusive with apps.

TYPE: Optional[List['mod_orm.ORM.Record']] DEFAULT: None

extract_apps
extract_apps(
    apps: Iterable["mod_orm.ORM.AppDefinition"],
    records: Optional[List["mod_orm.ORM.Record"]] = None,
) -> Iterable[DataFrame]

Creates record rows with app information.

TODO: The means for enumerating records in this method is not ideal as it does a lot of filtering.

Functions