Prompt_service

This module contains functionality related to the the prompt_service module for augmentation.langfuse.

Prompt_service

LangfusePromptService

Source code in src/augmentation/langfuse/prompt_service.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class LangfusePromptService:

    def __init__(
        self, client: Langfuse, logger=LoggerConfiguration.get_logger(__name__)
    ):
        """
        Initializes the LangfusePromptService with a Langfuse client and a logger.

        Args:
            client (Langfuse): The Langfuse client instance.
            logger (Logger): The logger instance.
        """
        self.client = client
        self.logger = logger

    def create_prompt_if_not_exists(
        self,
        prompt_name: str,
        prompt_template: str,
    ) -> None:
        """
        Creates a new prompt in Langfuse if it does not already exist.

        Args:
            prompt_name (str): The name of the prompt.
            prompt_template (str): The template of the prompt.
        """
        if self.prompt_exists(prompt_name):
            return

        self.logger.info(f"Creating {prompt_name} prompt in Langfuse")
        self.client.create_prompt(
            name=prompt_name, prompt=prompt_template, labels=["production"]
        )

    def prompt_exists(
        self,
        prompt_name: str,
    ) -> bool:
        """
        Checks if a prompt exists in Langfuse.

        Args:
            prompt_name (str): The name of the prompt.

        Returns:
            bool: True if the prompt exists, False otherwise.
        """
        try:
            self.client.get_prompt(prompt_name)
            return True
        except Exception:
            return False

    def get_prompt_template(self, prompt_name: str) -> str:
        """
        Retrieves the prompt template from Langfuse.

        Args:
            prompt_name (str): The name of the prompt.

        Returns:
            str: The prompt template.
        """
        prompt = self.client.get_prompt(prompt_name)
        return prompt.prompt

__init__(client, logger=LoggerConfiguration.get_logger(__name__))

Initializes the LangfusePromptService with a Langfuse client and a logger.

Parameters:
  • client (Langfuse) –

    The Langfuse client instance.

  • logger (Logger, default: get_logger(__name__) ) –

    The logger instance.

Source code in src/augmentation/langfuse/prompt_service.py
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(
    self, client: Langfuse, logger=LoggerConfiguration.get_logger(__name__)
):
    """
    Initializes the LangfusePromptService with a Langfuse client and a logger.

    Args:
        client (Langfuse): The Langfuse client instance.
        logger (Logger): The logger instance.
    """
    self.client = client
    self.logger = logger

create_prompt_if_not_exists(prompt_name, prompt_template)

Creates a new prompt in Langfuse if it does not already exist.

Parameters:
  • prompt_name (str) –

    The name of the prompt.

  • prompt_template (str) –

    The template of the prompt.

Source code in src/augmentation/langfuse/prompt_service.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def create_prompt_if_not_exists(
    self,
    prompt_name: str,
    prompt_template: str,
) -> None:
    """
    Creates a new prompt in Langfuse if it does not already exist.

    Args:
        prompt_name (str): The name of the prompt.
        prompt_template (str): The template of the prompt.
    """
    if self.prompt_exists(prompt_name):
        return

    self.logger.info(f"Creating {prompt_name} prompt in Langfuse")
    self.client.create_prompt(
        name=prompt_name, prompt=prompt_template, labels=["production"]
    )

get_prompt_template(prompt_name)

Retrieves the prompt template from Langfuse.

Parameters:
  • prompt_name (str) –

    The name of the prompt.

Returns:
  • str( str ) –

    The prompt template.

Source code in src/augmentation/langfuse/prompt_service.py
67
68
69
70
71
72
73
74
75
76
77
78
def get_prompt_template(self, prompt_name: str) -> str:
    """
    Retrieves the prompt template from Langfuse.

    Args:
        prompt_name (str): The name of the prompt.

    Returns:
        str: The prompt template.
    """
    prompt = self.client.get_prompt(prompt_name)
    return prompt.prompt

prompt_exists(prompt_name)

Checks if a prompt exists in Langfuse.

Parameters:
  • prompt_name (str) –

    The name of the prompt.

Returns:
  • bool( bool ) –

    True if the prompt exists, False otherwise.

Source code in src/augmentation/langfuse/prompt_service.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def prompt_exists(
    self,
    prompt_name: str,
) -> bool:
    """
    Checks if a prompt exists in Langfuse.

    Args:
        prompt_name (str): The name of the prompt.

    Returns:
        bool: True if the prompt exists, False otherwise.
    """
    try:
        self.client.get_prompt(prompt_name)
        return True
    except Exception:
        return False

LangfusePromptServiceFactory

Bases: Factory

Factory class for creating and managing Langfuse prompt service instances.

This class implements the Singleton pattern through inheriting from Factory, ensuring only one Langfuse prompt service instance exists throughout the application.

Attributes:
  • _configuration_class (Type) –

    The configuration class used for creating Langfuse prompt service instances. In this case, it is LangfuseConfiguration.

Source code in src/augmentation/langfuse/prompt_service.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class LangfusePromptServiceFactory(Factory):
    """
    Factory class for creating and managing Langfuse prompt service instances.

    This class implements the Singleton pattern through inheriting from Factory,
    ensuring only one Langfuse prompt service instance exists throughout the application.

    Attributes:
        _configuration_class (Type): The configuration class used for creating
            Langfuse prompt service instances. In this case, it is LangfuseConfiguration.
    """

    _configuration_class: Type = LangfuseConfiguration

    @classmethod
    def _create_instance(cls, configuration: LangfuseConfiguration) -> Langfuse:
        """
        Creates a new Langfuse prompt service instance.

        Args:
            configuration (LangfuseConfiguration): Configuration object containing
                Langfuse API credentials and URL settings.

        Returns:
            LangfusePromptService: A configured Langfuse prompt service instance ready for use with the
                provided credentials and host.
        """
        client = LangfuseClientFactory.create(configuration)
        return LangfusePromptService(client=client)