File size: 3,435 Bytes
92feab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
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
110
111
112
113
114
115
from typing import List, Dict, Any
from uuid import uuid4

import gradio as gr

try:
    from retrieval import (
        candid_blog,
        candid_help,
        candid_learning,
        # cds,
        issuelab,
        # news,
        # up_orgs,
        youtube
    )
except ImportError:
    from .retrieval import (
        candid_blog,
        candid_help,
        candid_learning,
        # cds,
        issuelab,
        # news,
        # up_orgs,
        youtube
    )


# TODO summarize messages instead
def filter_messages(messages, k=10):
    return messages[-k:]


def html_format_doc(doc: Dict[str, Any], source: str, show_chunks=False) -> str:
    height_px = 200
    html = ""

    if source == "news":
        # html = news.article_card_html(doc, height_px, show_chunks)
        pass
    elif source == "transactions":
        # html = cds.transaction_card_html(doc, height_px, show_chunks)
        pass
    elif source == "organizations":
        # html = up_orgs.organization_card_html(doc, 400, show_chunks)
        pass
    elif source == "issuelab":
        html = issuelab.issuelab_card_html(doc, height_px, show_chunks)
    elif source == "youtube":
        html = youtube.build_card_html(doc, 400, show_chunks)
    elif source == "candid_blog":
        html = candid_blog.build_card_html(doc, height_px, show_chunks)
    elif source == "candid_learning":
        html = candid_learning.build_card_html(doc, height_px, show_chunks)
    elif source == "candid_help":
        html = candid_help.build_card_html(doc, height_px, show_chunks)
    return html


def html_format_docs_chat(docs):
    """
    Formats Candid sources into a line of buttons
    """
    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 = f"""<a href={s_url} target='_blank' rel='noreferrer' class='ssearch-source'> \
<button class='ssearch-source-btn'>{s_name}</button></a>"""
            docs_html.append(s_html)
        docs_html_insert = "".join(s for s in docs_html)
        html = f"""<div id='ssearch-sources'>{docs_html_insert}</div>"""
    return html


def format_chat_response(chatbot: List[Any]) -> List[Any]:
    """We have sources appended as one more tuple. Here we concatinate HTML of sources
        with the AI response
    Returns:
        _type_: updated chatbot message as HTML
    """
    if chatbot:
        sources = chatbot[-1][1]
        chatbot.pop(-1)
        chatbot[-1][1] = chatbot[-1][1] + sources
    return gr.HTML(chatbot)


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]["metadata"].get("title", None)
        if title == "Sources HTML":
            sources = chatbot[-1]["content"]
            chatbot.pop(-1)
            chatbot[-1]["content"] = chatbot[-1]["content"] + sources
    return gr.HTML(chatbot)


def valid_inputs(*args) -> bool:
    return any(a is not None or (isinstance(a, str) and a.strip() != '') for a in args)

def get_session_id(thread_id: gr.components.Component) -> str:
    if not thread_id:
        thread_id = uuid4().hex
    return thread_id