1
0
Fork 0
kagi.el/README.org

169 lines
7.4 KiB
Org Mode
Raw Normal View History

2023-12-23 18:17:08 +00:00
#+title: kagi.el README
#+author: Bram Schoenmakers
* Introduction
This Emacs package provides the following functionalities from the Kagi search engine:
2023-12-23 18:17:08 +00:00
- FastGPT :: Kagi's open source LLM offering, as a shell inspired by [[https://github.com/xenodium/chatgpt-shell][xenodium's chatgpt-shell]].
- Universal Summarizer :: Summarizes texts, webpages, videos and more.
2023-12-23 18:17:08 +00:00
2023-12-25 20:05:50 +00:00
Both functions are accessed through an [[https://help.kagi.com/kagi/api/overview.html][API]].
* Setup
2023-12-23 18:17:08 +00:00
1. Create a Kagi account if you haven't done so already. An account is free, and comes with 100 trial searches.
2023-12-23 18:17:08 +00:00
2. In [[https://kagi.com/settings?p=billing_api][your account settings]], put a balance for the API part (note that this is a separate balance than the subscription). The recommendation is to start with a one-time charge of $5. A single query ranges from 1 to 5 cents typically, depending on the amount of tokens processed.
3. In [[https://kagi.com/settings?p=api][the API portal]], create an API token. Put the result in ~kagi-api-token~.
2023-12-29 07:25:16 +00:00
* Commands and functions
** FastGPT
The FastGPT functionality has only one command: =kagi-fastgpt-shell=. This opens a shell buffer in a new window where prompts can be typed.
** Universal Summarizer
- =kagi-summarize-buffer= :: Summarizes the content of a buffer and displays it in a separate buffer.
- =kagi-summarize-region= :: Similarly, the text inside the region is summarized and shown in a separate buffer.
- =kagi-summarize-url= :: Prompts for a URL of which a summary is composed and displayed.
2023-12-29 07:25:16 +00:00
- =kagi-summarize= :: Function to retrieve a summary from a text or URL, to be used from Lisp code.
2023-12-23 18:17:08 +00:00
2023-12-28 17:02:12 +00:00
Note that texts submitted to Kagi are subject to their [[https://kagi.com/privacy#Summarizer][Privacy Policy]].
2023-12-23 18:17:08 +00:00
* Installation and configuration
kagi.el is not on MELPA (yet?), so for now only Git access is possible.
Clone with:
: git clone https://codeberg.org/bram85/kagi.el.git /path/to/kagi.el
Note that kagi.el has a dependency on the [[https://melpa.org/#/shell-maker][shell-maker package]], which is available on MELPA.
You way want to load and configure the package with ~use-package~, for example put the following in your Emacs init file:
#+begin_src elisp
(use-package kagi
:commands
kagi-fastgpt-shell
kagi-summarize-buffer
kagi-summarize-region
kagi-summarize-url
2023-12-23 18:17:08 +00:00
:ensure nil
:load-path "/path/to/kagi.el"
:custom
(kagi-api-token "ABCDEF")
;; or use a function, e.g. with the password-store package:
(kagi-api-token (lambda () (password-store-get "Kagi/API")))
;; summarizer settings
(kagi-summarizer-engine "cecil")
(kagi-summarize-default-language "EN")
(kagi-summarize-cache t)
:custom-face
;; kagi-code defaults to fixed-pitch, but can be overridden as
;; follows:
(kagi-code ((t (:inherit org-verbatim)))))
2023-12-23 18:17:08 +00:00
#+end_src
2023-12-27 20:09:33 +00:00
The token can be supplied directly as a string, but you could write a lambda to retrieve the token from a more secure location (e.g. with the combination of [[https://passwordstore.org/][pass(1)]] and the password-store package that comes with it).
2023-12-27 21:14:43 +00:00
** Embark integration
The kagi.el package can be integrated with [[https://github.com/oantolin/embark][Embark]], to easily summarize a buffer, region or an URL. In order to be consistent with all keymaps, and to avoid clashes, the functionality is behind the /K/ prefix key. Press /K s/ to trigger the summarize functionality.
Add the following to your configuration to trigger summary functionality with key /K s/:
#+begin_src elisp
(defmacro embark-kagi-map (name function)
"Macro for defining a keymap for accessing Kagi functionality through Embark."
`(defvar-keymap ,name
:doc "Keymap for accessing Kagi functionality with Embark."
:parent nil
"s" #',function))
(embark-kagi-map embark-kagi-buffer-map kagi-summarize-buffer)
(keymap-set embark-buffer-map "K" embark-kagi-buffer-map)
(embark-kagi-map embark-kagi-region-map kagi-summarize-region)
(keymap-set embark-region-map "K" embark-kagi-region-map)
(embark-kagi-map embark-kagi-url-map kagi-summarize-url)
(keymap-set embark-url-map "K" embark-kagi-url-map)
#+end_src
** Configuration settings
#+begin_src emacs-lisp :exports results :results table :colnames '("Custom variable" "Description")
(let ((rows))
(mapatoms
(lambda (symbol)
(when (and (string-match "\\_<kagi"
(symbol-name symbol))
(custom-variable-p symbol))
(push `(,symbol
,(car
(split-string
(or (get (indirect-variable symbol)
'variable-documentation)
(get symbol 'variable-documentation)
"")
"\n")))
rows))))
(sort rows (lambda (item1 item2)
(string< (car item1) (car item2)))))
#+end_src
#+RESULTS:
| Custom variable | Description |
|-------------------------------------+----------------------------------------------------------------------|
| kagi-api-enrichment-url | The Kagi Enrichment API entry point. |
| kagi-api-fastgpt-url | The Kagi FastGPT API entry point. |
| kagi-api-token | The Kagi API token. |
| kagi-summarize-default-language | Default target language of the summary. |
| kagi-summarizer-cache | Determines whether the Summarizer should cache results. |
| kagi-summarizer-engine | Which summary engine to use. |
*** COMMENT Attribution :noexport:
The code to generate the table of configuration items was inspired by an idea of [[https://xenodium.com/generating-elisp-org-docs/][Álvaro Ramírez]] (a.k.a. xenodium).
** Define own functions
The kagi.el package comes with a few variables
*** Language override
By overriding a variable with a =let= construct you can (temporarily) deviate from the default / configured value. For instance, to obtain a Dutch summary of a video you may want to define the following function:
#+begin_src elisp
(defun my/kagi-dutch-summary (text-or-url)
"Obtain a Dutch summary for the given TEXT-OR-URL."
(let ((kagi-summarize-default-language "NL"))
(kagi-summarize text-or-url)))
#+end_src
*** Caching override
The Summarizer API comes with the following note:
#+begin_quote
For handling sensitive information and documents, we recommend setting the 'cache' API parameter to False. In this way, the document will "flow through" our infrastructure and will not be retained anywhere after processing.
#+end_quote
In a similar fashion as above, you could define a function that disables caching temporarily (while having it enabled by default).
#+begin_src elisp
(defun my/kagi/sensitive-summary (text)
"Summarize the current TEXT with caching disabled.")
(let ((kagi-summarizer-cache nil))
(kagi-summarize text))
#+end_src
* References
2023-12-25 11:40:57 +00:00
- [[https://help.kagi.com/kagi/api/fastgpt.html][Kagi FastGPT API]]
- [[https://help.kagi.com/kagi/api/summarizer.html][Kagi Universal Summarizer API]]
2023-12-25 11:40:57 +00:00
- [[https://github.com/xenodium/chatgpt-shell][xenodium's chatgpt-shell]], which also provides shell-maker required by the FastGPT shell.