branch: externals/ellama commit e0b6d939f537c62054afe9089004a82404d779ff Merge: ed530c7f0d a9df949586 Author: Sergey Kostyaev <s-kosty...@users.noreply.github.com> Commit: GitHub <nore...@github.com>
Merge pull request #200 from s-kostyaev/add-semantic-similarity-check-function Add semantic similarity check function --- .github/workflows/melpa.yml | 1 + ellama.el | 29 +++++++++++++++++++++++ tests/integration-test-ellama.el | 50 ++++++++++++++++++++++++++++++++++++++++ tests/test-ellama.el | 2 +- 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/.github/workflows/melpa.yml b/.github/workflows/melpa.yml index 4b61dce6a5..9a34a8e9f4 100644 --- a/.github/workflows/melpa.yml +++ b/.github/workflows/melpa.yml @@ -58,6 +58,7 @@ jobs: - 29.1 - 29.2 - 29.3 + - 29.4 ignore_warnings: - false warnings_as_errors: 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 diff --git a/tests/integration-test-ellama.el b/tests/integration-test-ellama.el new file mode 100644 index 0000000000..23334e1bd5 --- /dev/null +++ b/tests/integration-test-ellama.el @@ -0,0 +1,50 @@ +;;; integration-test-ellama.el --- Ellama integration tests -*- lexical-binding: t; package-lint-main-file: "../ellama.el"; -*- + +;; Copyright (C) 2023-2025 Free Software Foundation, Inc. + +;; Author: Sergey Kostyaev <sskosty...@gmail.com> + +;; This file is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. + +;;; Commentary: +;; +;; Ellama tests. +;; + +;;; Code: + +(ert-deftest ellama-extract-sting-list-test () + "Check if `ellama-extract-string-list' works correctly." + (should (equal-including-properties + (ellama-extract-string-list + "fruits" + "Here is the fruits: apple, banana, dragon fruit. I like it.") + '("apple" "banana" "dragon fruit")))) + +(ert-deftest ellama-semantic-similar-test () + "Check if `ellama-semantic-similar-p' works correctly." + (should (equal-including-properties + (let ((res)) + (dolist (el '("How many r's in strawberry?" + "How many times letter r appears in word strawberry?" + "How many r's in strawberry?" + "How many times letter e appears in word strawberry?")) + (cl-pushnew el res :test #'ellama-semantic-similar-p)) + (reverse res)) + '("How many r's in strawberry?" + "How many times letter e appears in word strawberry?")))) + +(provide 'integration-test-ellama) + +;;; integration-test-ellama.el ends here diff --git a/tests/test-ellama.el b/tests/test-ellama.el index 39af77314a..8c93f6ee03 100644 --- a/tests/test-ellama.el +++ b/tests/test-ellama.el @@ -1,6 +1,6 @@ ;;; test-ellama.el --- Ellama tests -*- lexical-binding: t; package-lint-main-file: "../ellama.el"; -*- -;; Copyright (C) 2023 Free Software Foundation, Inc. +;; Copyright (C) 2023-2025 Free Software Foundation, Inc. ;; Author: Sergey Kostyaev <sskosty...@gmail.com>