Question
· Jun 23

How to convert an embeddings vector to be saved as %Vector

I try to get a vector from calling GetEmbedding, but i failed to convert it into a vector 

Here is a simplyfied sample class: 

Class User.myclass Extends %Persistent
{ Property myVECTOR As %Vector(CAPTION = "Vector");

Property myProperty As %String(MAXLEN = 40) [ Required ];

}

here the GetEmbedding part from User.mymethods:

...
ClassMethod GetEmbedding(sentences As %String) As %String [ Language = python ]
{
  import sentence_transformers   model sentence_transformers.SentenceTransformer('C:/InterSystems/IRIS/lib/python/Lib/site-packages/sentence_transformers/models/all-MiniLM-L6-v2')
  embeddings model.encode(sentences)   embeddings_list [str(embedding.tolist()) for embedding in embeddings]
  return embeddings_list
}.....

I try to save the VECTOR into myVECTOR, but that will fail because the vector is not in the right format: 


set VECTOR=##class(User.mymethods).GetEmbedding("this is my text")


/// here I need to convert the VECTOR, but I do not know the right command
 

set data=##class(User.myclass).%New()
set data.myProperty ="anything"
set data.myVECTOR=VECTOR
set ok=data.%Save()

Question: is there a method to convert the vector into the right format to be saved as %Vector

$ZV: IRIS for Windows (x86-64) 2024.1 (Build 262U) Thu Mar 7 2024 15:55:25 EST
Discussion (4)1
Log in or sign up to continue

Hallo @Ditmar Tybussek  lange nix mehr gehört von dir !

See my article Using VECTORs in ObjectScript.

related to your code: 
Simple Set vector=.... doesn't work
it is       Set $VECTOR(vectorname,  pos,type)   very clear example
and type can never be changed 
position is less sensitive. 

In your case using SQL functíon TO_VECTOR() might be quite comfortable
as it does all the checks for you under cover.
i also used all-MiniLM-L6-v2 (as PY method) starting from  label sc3 in the example
in my OEX package Vector-inside-IRIS

LG. aus Wien

 

not really since as you know there is always ObjectScript under most SQL Functions.
BUT as mentioned: I found TO_VECTOR()  the most comfortable one.
without SQL it might look like this:  or worse

;; assume a $LB structure of %Integer as input
set intlist=$LB(...........)
;; fill vector
set maxvecsize=22    ; just an assumption
 for i=1:1:maxvecsize {
  set val=$li(intlist,i)
  if '(val\1=val)  continue
  set $vector(vec,i,"int")=val
 }
write $isvector(vec)
write vec
zwrite vec

I think the difference is obvious.

Servus

 
Try this example