fix: Implement COSINE-SIMILARITY and fix memory persistence serialization

- Replace stub COSINE-SIMILARITY with real dot-product implementation
- Fix memory persistence to convert hash tables to alists before serialization
This commit is contained in:
2026-04-22 15:26:39 -04:00
parent 6bfc95e136
commit 586847bd02
4 changed files with 80 additions and 20 deletions

View File

@@ -13,7 +13,25 @@ A static, hardcoded architecture is inherently fragile. The ~opencortex~ Skill E
#+begin_src lisp :tangle ../library/skills.lisp
(in-package :opencortex)
(defun COSINE-SIMILARITY (v1 v2) 1.0) ; Stub
(defun COSINE-SIMILARITY (v1 v2)
"Computes the cosine similarity between two vectors.
Both arguments should be sequences of numbers. Returns a value between -1.0 and 1.0."
(let ((len1 (length v1)) (len2 (length v2)))
(if (or (zerop len1) (zerop len2))
0.0
(let ((dot-product 0.0d0)
(norm1 0.0d0)
(norm2 0.0d0))
(let ((len (min len1 len2)))
(dotimes (i len)
(let ((x (coerce (elt v1 i) 'double-float)))
(let ((y (coerce (elt v2 i) 'double-float)))
(incf dot-product (* x y))
(incf norm1 (* x x))
(incf norm2 (* y y))))))
(if (or (zerop norm1) (zerop norm2))
0.0
(/ dot-product (sqrt (* norm1 norm2))))))))
(defun VAULT-MASK-STRING (s) "[MASKED]") ; Stub
(defvar *VAULT-MEMORY* (make-hash-table :test 'equal))