mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-24 07:55:07 +01:00
Re-rank incremental search results using cross-encoder if user idle
This provides a relatively smooth mechanism - to improve relevance of results on idle - while providing the rapid, incremental results while typing
This commit is contained in:
parent
ad242cafa7
commit
9ab3edf6d6
1 changed files with 19 additions and 6 deletions
|
@ -46,6 +46,14 @@
|
||||||
:group 'khoj
|
:group 'khoj
|
||||||
:type 'integer)
|
:type 'integer)
|
||||||
|
|
||||||
|
(defcustom khoj--rerank-after-idle-time 1.0
|
||||||
|
"Idle time (in seconds) to trigger cross-encoder to rerank incremental search results"
|
||||||
|
:group 'khoj
|
||||||
|
:type 'float)
|
||||||
|
|
||||||
|
(defvar khoj--rerank-timer nil
|
||||||
|
"Idle timer to make cross-encoder re-rank incremental search results if user idle.")
|
||||||
|
|
||||||
(defconst khoj--query-prompt "Khoj: "
|
(defconst khoj--query-prompt "Khoj: "
|
||||||
"Query prompt shown to user in the minibuffer.")
|
"Query prompt shown to user in the minibuffer.")
|
||||||
|
|
||||||
|
@ -124,9 +132,10 @@
|
||||||
((or (equal file-extension "markdown") (equal file-extension "md")) "markdown")
|
((or (equal file-extension "markdown") (equal file-extension "md")) "markdown")
|
||||||
(t "org"))))
|
(t "org"))))
|
||||||
|
|
||||||
(defun khoj--construct-api-query (query search-type)
|
(defun khoj--construct-api-query (query search-type &optional rerank)
|
||||||
(let ((encoded-query (url-hexify-string query)))
|
(let ((rerank (or rerank "false"))
|
||||||
(format "%s/search?q=%s&t=%s" khoj--server-url encoded-query search-type)))
|
(encoded-query (url-hexify-string query)))
|
||||||
|
(format "%s/search?q=%s&t=%s&r=%s" khoj--server-url encoded-query search-type rerank)))
|
||||||
|
|
||||||
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
||||||
;; get json response from api
|
;; get json response from api
|
||||||
|
@ -156,11 +165,12 @@
|
||||||
(read-only-mode t)))
|
(read-only-mode t)))
|
||||||
|
|
||||||
;; Incremental Search on Khoj
|
;; Incremental Search on Khoj
|
||||||
(defun khoj--incremental-query ()
|
(defun khoj--incremental-query (&optional rerank)
|
||||||
(let* ((search-type khoj--search-type)
|
(let* ((rerank (cond (rerank "true") (t "false")))
|
||||||
|
(search-type khoj--search-type)
|
||||||
(buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type)))
|
(buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type)))
|
||||||
(query (minibuffer-contents-no-properties))
|
(query (minibuffer-contents-no-properties))
|
||||||
(query-url (khoj--construct-api-query query search-type)))
|
(query-url (khoj--construct-api-query query search-type rerank)))
|
||||||
(khoj--query-api-and-render-results
|
(khoj--query-api-and-render-results
|
||||||
query
|
query
|
||||||
search-type
|
search-type
|
||||||
|
@ -168,6 +178,8 @@
|
||||||
buffer-name)))
|
buffer-name)))
|
||||||
|
|
||||||
(defun khoj--remove-incremental-query ()
|
(defun khoj--remove-incremental-query ()
|
||||||
|
(khoj--incremental-query t)
|
||||||
|
(cancel-timer khoj--rerank-timer)
|
||||||
(remove-hook 'post-command-hook #'khoj--incremental-query)
|
(remove-hook 'post-command-hook #'khoj--incremental-query)
|
||||||
(remove-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query))
|
(remove-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query))
|
||||||
|
|
||||||
|
@ -179,6 +191,7 @@
|
||||||
(search-type (completing-read "Type: " '("org" "markdown" "ledger" "music") nil t default-type))
|
(search-type (completing-read "Type: " '("org" "markdown" "ledger" "music") nil t default-type))
|
||||||
(buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type))))
|
(buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type))))
|
||||||
(setq khoj--search-type search-type)
|
(setq khoj--search-type search-type)
|
||||||
|
(setq khoj--rerank-timer (run-with-idle-timer khoj--rerank-after-idle-time t 'khoj--incremental-query t))
|
||||||
(switch-to-buffer buffer-name)
|
(switch-to-buffer buffer-name)
|
||||||
(minibuffer-with-setup-hook
|
(minibuffer-with-setup-hook
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
Loading…
Reference in a new issue