diff --git a/lib/lib_llm.metta b/lib/lib_llm.metta index 05663737..923273a2 100644 --- a/lib/lib_llm.metta +++ b/lib/lib_llm.metta @@ -29,10 +29,24 @@ ($outmsg (py-call (builtins.index $output 1))) ;0 is ResponseReasoningItem ($content (py-call (getattr $outmsg "content"))) ($part (py-call (builtins.index $content 0))) - ($text (py-call (getattr $part "text")))) + ($text (py-call (getattr $part "text"))) + ($_ (cut))) $text)) (= (sread-safe $w) (case (catch (sread $w)) (((Error $A $B) (empty)) ($z $z)))) + +(= (useGPTEmbedding $text) + (let* (($client (py-call (openai.OpenAI))) + ($embeddings (py-call (getattr $client "embeddings"))) + ($create (py-call (getattr $embeddings "create"))) + ($mydict (py-str ("{'model':'text-embedding-3-large','input':\"" $text "\"}"))) + ($kwargs (once (py-eval $mydict))) + ($res (py-call (builtins.apply_kwargs $create $kwargs))) + ($data (py-call (getattr $res "data"))) + ($item (py-call (builtins.index $data 0))) + ($vector (py-call (getattr $item "embedding"))) + ($_ (cut))) + $vector)) diff --git a/lib/lib_vector.metta b/lib/lib_vector.metta new file mode 100644 index 00000000..ac3d18de --- /dev/null +++ b/lib/lib_vector.metta @@ -0,0 +1,13 @@ +(= (dot () ()) 0.0) +(= (dot (cons $a $as) (cons $b $bs)) + (+ (* $a $b) (dot $as $bs))) + +(= (cosine-of-normalized $a $b) + (dot $a $b)) + +(= (norm $v) + (sqrt-math (dot $v $v))) + +(= (cosine $a $b) + (/ (dot $a $b) + (* (norm $a) (norm $b))))