Skip to content

Commit

Permalink
Move the text rendering out for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
trducng committed Apr 13, 2024
1 parent af38708 commit 78462ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
51 changes: 18 additions & 33 deletions libs/ktem/ktem/reasoning/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import tiktoken
from ktem.llms.manager import llms
from ktem.utils.render import Render

from kotaemon.base import (
BaseComponent,
Expand Down Expand Up @@ -559,8 +560,6 @@ async def ainvoke( # type: ignore
def stream( # type: ignore
self, message: str, conv_id: str, history: list, **kwargs # type: ignore
) -> Generator[Document, None, Document]:
import markdown

docs = []
doc_ids = []
if self.use_rewrite:
Expand All @@ -571,19 +570,15 @@ def stream( # type: ignore
if doc.doc_id not in doc_ids:
docs.append(doc)
doc_ids.append(doc.doc_id)

for doc in docs:
# TODO: a better approach to show the information
text = markdown.markdown(
doc.text, extensions=["markdown.extensions.tables"]
)
yield Document(
content=(
"<details open>"
f"<summary>{doc.metadata['file_name']}</summary>"
f"{text}"
"</details><br>"
),
channel="info",
content=Render.collapsible(
header=doc.metadata["file_name"],
content=Render.table(doc.text),
open=True,
),
)

evidence_mode, evidence = self.evidence_pipeline(docs).content
Expand Down Expand Up @@ -638,23 +633,17 @@ def stream( # type: ignore
ss = sorted(ss, key=lambda x: x["start"])
text = id2docs[id].text[: ss[0]["start"]]
for idx, span in enumerate(ss):
text += (
"<mark>" + id2docs[id].text[span["start"] : span["end"]] + "</mark>"
)
text += Render.highlight(id2docs[id].text[span["start"] : span["end"]])
if idx < len(ss) - 1:
text += id2docs[id].text[span["end"] : ss[idx + 1]["start"]]
text += id2docs[id].text[ss[-1]["end"] :]
text_out = markdown.markdown(
text, extensions=["markdown.extensions.tables"]
)
yield Document(
content=(
"<details open>"
f"<summary>{id2docs[id].metadata['file_name']}</summary>"
f"{text_out}"
"</details><br>"
),
channel="info",
content=Render.collapsible(
header=id2docs[id].metadata["file_name"],
content=Render.table(text),
open=True,
),
)
lack_evidence = False

Expand All @@ -667,17 +656,13 @@ def stream( # type: ignore
content="Retrieved segments without matching evidence:\n",
)
for id in list(not_detected):
text_out = markdown.markdown(
id2docs[id].text, extensions=["markdown.extensions.tables"]
)
yield Document(
content=(
"<details>"
f"<summary>{id2docs[id].metadata['file_name']}</summary>"
f"{text_out}"
"</details><br>"
),
channel="info",
content=Render.collapsible(
header=id2docs[id].metadata["file_name"],
content=Render.table(id2docs[id].text),
open=False,
),
)

return answer
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions libs/ktem/ktem/utils/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import markdown


class Render:
"""Default text rendering into HTML for the UI"""

@staticmethod
def collapsible(header, content, open: bool = False) -> str:
"""Render an HTML friendly collapsible section"""
o = " open" if open else ""
return f"<details{o}><summary>{header}</summary>{content}</details><br>"

@staticmethod
def table(text: str) -> str:
"""Render table from markdown format into HTML"""
return markdown.markdown(text, extensions=["markdown.extensions.tables"])

@staticmethod
def highlight(text: str) -> str:
"""Highlight text"""
return f"<mark>{text}</mark>"

0 comments on commit 78462ab

Please sign in to comment.