Skip to content

Commit 0b09089

Browse files
authored
feat: add support for reasoning llm with thought visualization (#652) bump:patch
* fix: lanceDB query with empty file_ids * feat: add thinking display * feat: add low request mode for local llm
1 parent f5b2200 commit 0b09089

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

libs/kotaemon/kotaemon/indices/qa/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,18 @@ def find_start_end_phrase(
8080
final_match = None
8181

8282
return final_match, matched_length
83+
84+
85+
def replace_think_tag_with_details(text):
86+
text = text.replace(
87+
"<think>",
88+
'<details><summary><span style="color:grey">Thought</span></summary><blockquote>', # noqa
89+
)
90+
text = text.replace("</think>", "</blockquote></details>")
91+
return text
92+
93+
94+
def strip_think_tag(text):
95+
if "</think>" in text:
96+
text = text.split("</think>")[1]
97+
return text

libs/kotaemon/kotaemon/storages/docstores/lancedb.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def get(self, ids: Union[List[str], str]) -> List[Document]:
9898
if not isinstance(ids, list):
9999
ids = [ids]
100100

101+
if len(ids) == 0:
102+
return []
103+
101104
id_filter = ", ".join([f"'{_id}'" for _id in ids])
102105
try:
103106
document_collection = self.db_connection.open_table(self.collection_name)

libs/ktem/ktem/index/file/pipelines.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Generator, Optional, Sequence
1515

1616
import tiktoken
17+
from decouple import config
1718
from ktem.db.models import engine
1819
from ktem.embeddings.manager import embedding_models_manager
1920
from ktem.llms.manager import llms
@@ -270,7 +271,7 @@ def get_user_settings(cls) -> dict:
270271
},
271272
"use_llm_reranking": {
272273
"name": "Use LLM relevant scoring",
273-
"value": True,
274+
"value": not config("USE_LOW_LLM_REQUESTS", default=False, cast=bool),
274275
"choices": [True, False],
275276
"component": "checkbox",
276277
},

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66

77
import gradio as gr
8+
from decouple import config
89
from ktem.app import BasePage
910
from ktem.components import reasonings
1011
from ktem.db.models import Conversation, engine
@@ -23,6 +24,7 @@
2324

2425
from kotaemon.base import Document
2526
from kotaemon.indices.ingests.files import KH_DEFAULT_FILE_EXTRACTORS
27+
from kotaemon.indices.qa.utils import strip_think_tag
2628

2729
from ...utils import SUPPORTED_LANGUAGE_MAP, get_file_names_regex, get_urls
2830
from ...utils.commands import WEB_SEARCH_COMMAND
@@ -367,13 +369,22 @@ def on_building_ui(self):
367369
elem_id="citation-dropdown",
368370
)
369371

370-
self.use_mindmap = gr.State(value=True)
371-
self.use_mindmap_check = gr.Checkbox(
372-
label="Mindmap (on)",
373-
container=False,
374-
elem_id="use-mindmap-checkbox",
375-
value=True,
376-
)
372+
if not config("USE_LOW_LLM_REQUESTS", default=False, cast=bool):
373+
self.use_mindmap = gr.State(value=True)
374+
self.use_mindmap_check = gr.Checkbox(
375+
label="Mindmap (on)",
376+
container=False,
377+
elem_id="use-mindmap-checkbox",
378+
value=True,
379+
)
380+
else:
381+
self.use_mindmap = gr.State(value=False)
382+
self.use_mindmap_check = gr.Checkbox(
383+
label="Mindmap (off)",
384+
container=False,
385+
elem_id="use-mindmap-checkbox",
386+
value=False,
387+
)
377388

378389
with gr.Column(
379390
scale=INFO_PANEL_SCALES[False], elem_id="chat-info-panel"
@@ -1361,6 +1372,7 @@ def check_and_suggest_name_conv(self, chat_history):
13611372
# check if this is a newly created conversation
13621373
if len(chat_history) == 1:
13631374
suggested_name = suggest_pipeline(chat_history).text
1375+
suggested_name = strip_think_tag(suggested_name)
13641376
suggested_name = suggested_name.replace('"', "").replace("'", "")[:40]
13651377
new_name = gr.update(value=suggested_name)
13661378
renamed = True

libs/ktem/ktem/reasoning/simple.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from textwrap import dedent
44
from typing import Generator
55

6+
from decouple import config
67
from ktem.embeddings.manager import embedding_models_manager as embeddings
78
from ktem.llms.manager import llms
89
from ktem.reasoning.prompt_optimization import (
@@ -29,6 +30,7 @@
2930
)
3031
from kotaemon.indices.qa.citation_qa_inline import AnswerWithInlineCitation
3132
from kotaemon.indices.qa.format_context import PrepareEvidencePipeline
33+
from kotaemon.indices.qa.utils import replace_think_tag_with_details
3234
from kotaemon.llms import ChatLLM
3335

3436
from ..utils import SUPPORTED_LANGUAGE_MAP
@@ -313,6 +315,13 @@ def generate_relevant_scores():
313315
**kwargs,
314316
)
315317

318+
# check <think> tag from reasoning models
319+
processed_answer = replace_think_tag_with_details(answer.text)
320+
if processed_answer != answer.text:
321+
# clear the chat message and render again
322+
yield Document(channel="chat", content=None)
323+
yield Document(channel="chat", content=processed_answer)
324+
316325
# show the evidence
317326
if scoring_thread:
318327
scoring_thread.join()
@@ -410,7 +419,11 @@ def get_user_settings(cls) -> dict:
410419
},
411420
"highlight_citation": {
412421
"name": "Citation style",
413-
"value": "highlight",
422+
"value": (
423+
"highlight"
424+
if not config("USE_LOW_LLM_REQUESTS", default=False, cast=bool)
425+
else "off"
426+
),
414427
"component": "radio",
415428
"choices": [
416429
("citation: highlight", "highlight"),

0 commit comments

Comments
 (0)