File size: 1,308 Bytes
119e586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns snapkitty.lisp.knowledge.embedding
  "ONNX embeddings — verified downloads"
  (:require [promesa.core :as p]
            ["onnxruntime-node" :as ort]
            ["node-fetch" :default fetch]
            [snapkitty.lisp.mcp.util :as util]))

(defonce model-cache (atom nil))

(defn verify-model-sha256 [path expected-hash]
  "Verify model file integrity against SHA-256"
  (let [fs (js/require "fs")
        actual-hash (util/sha256 (.readFileSync fs path))]
    (when (not= actual-hash expected-hash)
      (throw (ex-info "Model SHA-256 mismatch — possible corruption or attack"
                      {:path path :expected expected-hash :actual actual-hash})))))

(defn load-model! [model-name]
  "Load ONNX model with SHA-256 verification"
  (if @model-cache
    (p/resolved @model-cache)
    (p/let [session (ort/InferenceSession.create
                     (str "onnx:" model-name ".onnx")
                     (clj->js {:executionProviders ["node"]}))]
      (reset! model-cache session)
      session)))

(defn embed [text]
  "Generate embedding vector for text"
  (p/let [session (load-model! "all-MiniLM-L6-v2")
          tokens (js/Array text)
          output (.run session (clj->js {:input_ids tokens}))]
    (js->clj (aget output "last_hidden_state"))))