Skip to content

Commit 337f385

Browse files
committed
feat: add URL indexing from chatbox
1 parent f55fc60 commit 337f385

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

libs/ktem/ktem/index/file/ui.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ def on_register_events(self):
683683
if self._index.id == 1:
684684
self.quick_upload_state = gr.State(value=[])
685685
print("Setting up quick upload event")
686+
687+
# override indexing function from chat page
688+
self._app.chat_page.first_indexing_url_fn = (
689+
self.index_fn_url_with_default_loaders
690+
)
686691
quickUploadedEvent = (
687692
self._app.chat_page.quick_file_upload.upload(
688693
fn=lambda: gr.update(

libs/ktem/ktem/pages/chat/__init__.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from kotaemon.base import Document
2323
from kotaemon.indices.ingests.files import KH_DEFAULT_FILE_EXTRACTORS
2424

25-
from ...utils import SUPPORTED_LANGUAGE_MAP, get_file_names_regex
25+
from ...utils import SUPPORTED_LANGUAGE_MAP, get_file_names_regex, get_urls
2626
from .chat_panel import ChatPanel
2727
from .common import STATE
2828
from .control import ConversationControl
@@ -140,6 +140,7 @@ def on_building_ui(self):
140140
# get the file selector choices for the first index
141141
if index_id == 0:
142142
self.first_selector_choices = index_ui.selector_choices
143+
self.first_indexing_url_fn = None
143144

144145
if gr_index:
145146
if isinstance(gr_index, list):
@@ -284,6 +285,7 @@ def on_register_events(self):
284285
self.chat_panel.text_input,
285286
self.chat_panel.chatbot,
286287
self._app.user_id,
288+
self._app.settings_state,
287289
self.chat_control.conversation_id,
288290
self.chat_control.conversation_rn,
289291
self.first_selector_choices,
@@ -634,6 +636,7 @@ def submit_msg(
634636
chat_input,
635637
chat_history,
636638
user_id,
639+
settings,
637640
conv_id,
638641
conv_name,
639642
first_selector_choices,
@@ -643,22 +646,44 @@ def submit_msg(
643646
raise ValueError("Input is empty")
644647

645648
chat_input_text = chat_input.get("text", "")
649+
file_ids = []
646650

647-
# get all file names with pattern @"filename" in input_str
648-
file_names, chat_input_text = get_file_names_regex(chat_input_text)
649651
first_selector_choices_map = {
650652
item[0]: item[1] for item in first_selector_choices
651653
}
652-
file_ids = []
653654

654-
if file_names:
655+
# get all file names with pattern @"filename" in input_str
656+
file_names, chat_input_text = get_file_names_regex(chat_input_text)
657+
# get all urls in input_str
658+
urls, chat_input_text = get_urls(chat_input_text)
659+
660+
if urls and self.first_indexing_url_fn:
661+
print("Detected URLs", urls)
662+
file_ids = self.first_indexing_url_fn(
663+
"\n".join(urls),
664+
True,
665+
settings,
666+
user_id,
667+
)
668+
elif file_names:
655669
for file_name in file_names:
656670
file_id = first_selector_choices_map.get(file_name)
657671
if file_id:
658672
file_ids.append(file_id)
659673

674+
# add new file ids to the first selector choices
675+
first_selector_choices.extend(zip(urls, file_ids))
676+
677+
# if file_ids is not empty and chat_input_text is empty
678+
# set the input to summary
679+
if not chat_input_text and file_ids:
680+
chat_input_text = "Summary"
681+
660682
if file_ids:
661-
selector_output = ["select", file_ids]
683+
selector_output = [
684+
"select",
685+
gr.update(value=file_ids, choices=first_selector_choices),
686+
]
662687
else:
663688
selector_output = [gr.update(), gr.update()]
664689

libs/ktem/ktem/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .conversation import get_file_names_regex
1+
from .conversation import get_file_names_regex, get_urls
22
from .lang import SUPPORTED_LANGUAGE_MAP
33

4-
__all__ = ["SUPPORTED_LANGUAGE_MAP", "get_file_names_regex"]
4+
__all__ = ["SUPPORTED_LANGUAGE_MAP", "get_file_names_regex", "get_urls"]

libs/ktem/ktem/utils/conversation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,15 @@ def get_file_names_regex(input_str: str) -> tuple[list[str], str]:
2929
return matches, input_str
3030

3131

32+
def get_urls(input_str: str) -> tuple[list[str], str]:
33+
# get all urls in input_str
34+
# also remove these urls from input_str
35+
pattern = r"https?://[^\s]+"
36+
matches = re.findall(pattern, input_str)
37+
input_str = re.sub(pattern, "", input_str).strip()
38+
39+
return matches, input_str
40+
41+
3242
if __name__ == "__main__":
3343
print(sync_retrieval_n_message([[""], [""], [""]], []))

0 commit comments

Comments
 (0)