File size: 1,064 Bytes
5b7d0e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6dd20e
5b7d0e6
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from dataclasses import dataclass
from typing import Iterable

from buster.formatter import ResponseFormatter, Source


@dataclass
class GradioResponseFormatter(ResponseFormatter):
    """Format the answer for gradio chat interface."""

    error_msg_template: str = """Something went wrong:<br>{response.error_msg}"""
    error_fallback_template: str = "Something went very wrong."
    sourced_answer_template: str = (
        """{response.text}<br><br>"""
        """πŸ“ Here are the sources I used to answer your question:<br>"""
        """{sources}<br><br>"""
        """{footnote}"""
    )
    unsourced_answer_template: str = "{response.text}<br><br>{footnote}"
    source_template: str = """[πŸ”— {source.title}]({source.url}), relevance: {source.question_similarity:2.1f} %"""

    def sources_list(self, sources: Iterable[Source]) -> str | None:
        """Format sources into a list."""
        items = [self.source_item(source) for source in sources]
        if not items:
            return None  # No list needed.

        return "<br>".join(items)