branch: externals/ellama commit cddea3d08e72e234a2745678123abd84b82165a8 Author: Sergey Kostyaev <sskosty...@gmail.com> Commit: Sergey Kostyaev <sskosty...@gmail.com>
Add semantic similarity check functionality Added a new function `ellama-semantic-similar-p` to check if two texts have the same meaning using an LLM. Also, added a custom variable `ellama-semantic-identity-template` for defining the template used in this semantic identity check. --- ellama.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ellama.el b/ellama.el index 4b94bfd3c3..4c6f7e30d8 100644 --- a/ellama.el +++ b/ellama.el @@ -367,6 +367,20 @@ is not changed. :group 'ellama :type 'string) +(defcustom ellama-semantic-identity-template "Determine if two texts have the same meaning. If they are similar but differ in key aspects, they are not the same. Return the answer as a JSON object. +<TEXT_1> +%s +</TEXT_1> +<TEXT_2> +%s +</TEXT_2> +<EXAMPLE> +{\"same\": true} +</EXAMPLE>" + "Extract string list template." + :group 'ellama + :type 'string) + (defcustom ellama-extraction-provider nil "LLM provider for data extraction." :group 'ellama @@ -2197,6 +2211,21 @@ otherwise prompt user for URL to summarize." (kill-region (point) (point-max)) (ellama-summarize)))) +(defun ellama-semantic-similar-p (text1 text2) + "Check if TEXT1 means the same as TEXT2." + (plist-get + (json-parse-string + (llm-chat + (or ellama-extraction-provider ellama-provider) + (llm-make-chat-prompt + (format ellama-semantic-identity-template text1 text2) + :response-format '(:type object :properties + (:same (:type boolean)) + :required (same)))) + :object-type 'plist + :false-object nil) + :same)) + (defun ellama--make-extract-string-list-prompt (elements input) "Create LLM prompt for list of ELEMENTS extraction from INPUT." (llm-make-chat-prompt