Utils

This module contains functionality related to the the utils module for augmentation.

Utils

ConversationUtils

Utility class for handling conversation messages.

Provides methods for managing welcome messages and reference handling in conversations.

Attributes:
  • WELCOME_TEMPLATE

    Template string for welcome message.

  • REFERENCES_TEMPLATE

    Template string for formatting references section.

Source code in src/augmentation/utils.py
 8
 9
10
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class ConversationUtils:
    """Utility class for handling conversation messages.

    Provides methods for managing welcome messages and reference handling in conversations.

    Attributes:
        WELCOME_TEMPLATE: Template string for welcome message.
        REFERENCES_TEMPLATE: Template string for formatting references section.
    """

    WELCOME_TEMPLATE = "Welcome to our Bavarian Beer Chat! 🍻 We're here to guide you through the rich tapestry of Bavarian beer culture. Whether you're curious about traditional brews, local beer festivals, or the history behind Bavaria's renowned beer purity law, you've come to the right place. Type your question below, and let's embark on this flavorful journey together. Prost!"
    REFERENCES_TEMPLATE = "\n\n**References**:\n" "{references}\n"

    @staticmethod
    def get_welcome_message() -> Message:
        """Create and return a welcome message.

        Returns:
            Message: Configured welcome message object.
        """
        return Message(
            author="Assistant", content=ConversationUtils.WELCOME_TEMPLATE
        )

    @staticmethod
    def add_references(message: Message, response: StreamingResponse) -> None:
        """Add source references to a message.

        Args:
            message: Message object to append references to.
            response: StreamingResponse containing source nodes.
        """
        message.content += ConversationUtils._get_references_str(
            response.source_nodes
        )

    @staticmethod
    def _get_references_str(nodes: List[NodeWithScore]) -> str:
        """Generate formatted references string from source nodes.

        Args:
            nodes: List of source nodes with relevance scores.

        Returns:
            str: Formatted string of unique references.
        """
        raw_references = [
            ConversationUtils._get_reference_str(node) for node in nodes
        ]
        references = "\n".join(set(raw_references))
        return ConversationUtils.REFERENCES_TEMPLATE.format(
            references=references
        )

    @staticmethod
    def _get_reference_str(node: NodeWithScore) -> str:
        """Format a single node's reference as a string.

        Args:
            node: Source node with metadata containing title and optional URL.

        Returns:
            str: Formatted reference string, with URL link if available.
        """
        title = node.metadata.get("title")
        if not title:
            title = node.metadata.get("Title")

        url = node.metadata.get("url")

        if url:
            return "- [{}]({})".format(title, url)
        else:
            return f"- {title}"

_get_reference_str(node) staticmethod

Format a single node's reference as a string.

Parameters:
  • node (NodeWithScore) –

    Source node with metadata containing title and optional URL.

Returns:
  • str( str ) –

    Formatted reference string, with URL link if available.

Source code in src/augmentation/utils.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@staticmethod
def _get_reference_str(node: NodeWithScore) -> str:
    """Format a single node's reference as a string.

    Args:
        node: Source node with metadata containing title and optional URL.

    Returns:
        str: Formatted reference string, with URL link if available.
    """
    title = node.metadata.get("title")
    if not title:
        title = node.metadata.get("Title")

    url = node.metadata.get("url")

    if url:
        return "- [{}]({})".format(title, url)
    else:
        return f"- {title}"

_get_references_str(nodes) staticmethod

Generate formatted references string from source nodes.

Parameters:
  • nodes (List[NodeWithScore]) –

    List of source nodes with relevance scores.

Returns:
  • str( str ) –

    Formatted string of unique references.

Source code in src/augmentation/utils.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@staticmethod
def _get_references_str(nodes: List[NodeWithScore]) -> str:
    """Generate formatted references string from source nodes.

    Args:
        nodes: List of source nodes with relevance scores.

    Returns:
        str: Formatted string of unique references.
    """
    raw_references = [
        ConversationUtils._get_reference_str(node) for node in nodes
    ]
    references = "\n".join(set(raw_references))
    return ConversationUtils.REFERENCES_TEMPLATE.format(
        references=references
    )

add_references(message, response) staticmethod

Add source references to a message.

Parameters:
  • message (Message) –

    Message object to append references to.

  • response (StreamingResponse) –

    StreamingResponse containing source nodes.

Source code in src/augmentation/utils.py
32
33
34
35
36
37
38
39
40
41
42
@staticmethod
def add_references(message: Message, response: StreamingResponse) -> None:
    """Add source references to a message.

    Args:
        message: Message object to append references to.
        response: StreamingResponse containing source nodes.
    """
    message.content += ConversationUtils._get_references_str(
        response.source_nodes
    )

get_welcome_message() staticmethod

Create and return a welcome message.

Returns:
  • Message( Message ) –

    Configured welcome message object.

Source code in src/augmentation/utils.py
21
22
23
24
25
26
27
28
29
30
@staticmethod
def get_welcome_message() -> Message:
    """Create and return a welcome message.

    Returns:
        Message: Configured welcome message object.
    """
    return Message(
        author="Assistant", content=ConversationUtils.WELCOME_TEMPLATE
    )