Dataset

This module contains functionality related to the the dataset module for common.langfuse.

Dataset

LangfuseDatasetService

Service for managing Langfuse datasets.

Provides methods to create and retrieve datasets in Langfuse platform.

Attributes:
  • langfuse_client

    Client instance for Langfuse API interactions.

Source code in src/common/langfuse/dataset.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class LangfuseDatasetService:
    """Service for managing Langfuse datasets.

    Provides methods to create and retrieve datasets in Langfuse platform.

    Attributes:
        langfuse_client: Client instance for Langfuse API interactions.
    """

    def __init__(self, langfuse_client: Langfuse):
        """Initialize the dataset service.

        Args:
            langfuse_client: Client for Langfuse API interactions.
        """
        self.langfuse_client = langfuse_client

    def create_if_does_not_exist(
        self, dataset: LangfuseDatasetConfiguration
    ) -> None:
        """Create dataset in Langfuse if it doesn't exist.

        Args:
            dataset: Configuration containing dataset details.

        Note:
            NotFoundError exception is caught but still logged due to
            Langfuse implementation.
        """
        try:
            self.langfuse_client.get_dataset(dataset.name)
            logging.info(f"Dataset {dataset.name} exists.")
        except NotFoundError:
            logging.info(f"Dataset {dataset.name} does not exist. Creating...")
            self.langfuse_client.create_dataset(
                name=dataset.name,
                description=dataset.description,
                metadata=dataset.metadata,
            )

    def get_dataset(self, dataset_name: str) -> DatasetClient:
        """Retrieve a dataset client by name.

        Args:
            dataset_name: Name of the dataset to retrieve.

        Returns:
            DatasetClient: Client for interacting with the specified dataset.
        """
        return self.langfuse_client.get_dataset(dataset_name)

__init__(langfuse_client)

Initialize the dataset service.

Parameters:
  • langfuse_client (Langfuse) –

    Client for Langfuse API interactions.

Source code in src/common/langfuse/dataset.py
20
21
22
23
24
25
26
def __init__(self, langfuse_client: Langfuse):
    """Initialize the dataset service.

    Args:
        langfuse_client: Client for Langfuse API interactions.
    """
    self.langfuse_client = langfuse_client

create_if_does_not_exist(dataset)

Create dataset in Langfuse if it doesn't exist.

Parameters:
  • dataset (LangfuseDatasetConfiguration) –

    Configuration containing dataset details.

Note

NotFoundError exception is caught but still logged due to Langfuse implementation.

Source code in src/common/langfuse/dataset.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def create_if_does_not_exist(
    self, dataset: LangfuseDatasetConfiguration
) -> None:
    """Create dataset in Langfuse if it doesn't exist.

    Args:
        dataset: Configuration containing dataset details.

    Note:
        NotFoundError exception is caught but still logged due to
        Langfuse implementation.
    """
    try:
        self.langfuse_client.get_dataset(dataset.name)
        logging.info(f"Dataset {dataset.name} exists.")
    except NotFoundError:
        logging.info(f"Dataset {dataset.name} does not exist. Creating...")
        self.langfuse_client.create_dataset(
            name=dataset.name,
            description=dataset.description,
            metadata=dataset.metadata,
        )

get_dataset(dataset_name)

Retrieve a dataset client by name.

Parameters:
  • dataset_name (str) –

    Name of the dataset to retrieve.

Returns:
  • DatasetClient( DatasetClient ) –

    Client for interacting with the specified dataset.

Source code in src/common/langfuse/dataset.py
51
52
53
54
55
56
57
58
59
60
def get_dataset(self, dataset_name: str) -> DatasetClient:
    """Retrieve a dataset client by name.

    Args:
        dataset_name: Name of the dataset to retrieve.

    Returns:
        DatasetClient: Client for interacting with the specified dataset.
    """
    return self.langfuse_client.get_dataset(dataset_name)