Skip to content

Commit

Permalink
Updated ollama macros; added cosSimilarity function (via javascript).
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Oct 21, 2024
1 parent 4f51eb6 commit 841fc78
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions macros/macros-ollama.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PREFIX eg: <http://www.example.org/>
PREFIX udf: <https://w3id.org/aksw/norse#udf.>
PREFIX url: <http://jsa.aksw.org/fn/url/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX norse: <https://w3id.org/aksw/norse#>
PREFIX ollama: <https://w3id.org/aksw/norse#ollama.>

#
Expand Down Expand Up @@ -73,4 +75,11 @@ ollama:embed
""" "url" "model" "prompt") ;
.

norse:json.cosSimilarity
rdfs:comment "Computes the cosinus similarity (1=similar, -1 not similar) between two vectors represented as JSON arrays of numbers. Uses components starting from 0 up to that of the shorter vector." ;
sh:prefixes eg:prefixMapping ;
udf:simpleDefinition ("""
norse:json.js('(v1, v2) => { var v1Sq = 0; var v2Sq = 0; var dot = 0; for (i = 0; i < Math.min(v1.length, v2.length); ++i) { v1Sq += v1[i] * v1[i]; v2Sq += v2[i] * v2[i]; dot += v1[i] * v2[i]; } v1Len = Math.sqrt(v1Sq); v2Len = Math.sqrt(v2Sq); return dot / (v1Len * v2Len); }', ?v1, ?v2)
""" "v1" "v2") ;
.

0 comments on commit 841fc78

Please sign in to comment.