File size: 1,481 Bytes
68e9b80
a0e37e2
ce9dc52
bea5044
ce9dc52
68e9b80
ce9dc52
bea5044
 
 
68e9b80
ce9dc52
bea5044
 
 
 
ce9dc52
a0e37e2
bea5044
ce9dc52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68e9b80
ce9dc52
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import Any

from langchain_core.documents import Document


def html_format_docs_chat(docs: list[Document]) -> str:
    """Formats Candid sources

    Parameters
    ----------
    docs : list[Document]
        Retrieved documents for context

    Returns
    -------
    str
        Formatted HTML
    """

    html = ""
    if docs:
        docs_html = []
        for doc in docs:
            s_name = doc.metadata.get("source", "Source")
            s_url = doc.metadata.get("url", "URL")

            s_html = (
                "<span class='source-item'>"
                f"<a href='{s_url}' target='_blank' rel='noreferrer' class='ssearch-source'>"
                f"{doc.metadata['title']} &vert; {s_name}</a></span>"
            )

            docs_html.append(s_html)

        html = f"<h2>Candid Resources</h2><div id='ssearch-sources'>{'<br>'.join(docs_html)}</div>"
    return html


def format_chat_ag_response(chatbot: list[Any]) -> list[Any]:
    """If we called retriever, we appended sources as as one more message. Here we concatinate HTML of sources
        with the AI response
    Returns:
        _type_: updated chatbot message as HTML
    """
    sources = ""
    if chatbot:
        title = (chatbot[-1].get("metadata") or {}).get("title", None)
        if title == "Sources HTML":
            sources = chatbot[-1]["content"]
            chatbot.pop(-1)
            chatbot[-1]["content"] = chatbot[-1]["content"] + sources
    return chatbot