redis / redis-om-dotnet

Object mapping, and more, for Redis and .NET
MIT License
442 stars 74 forks source link

Consider removing class constraint for IVectorizer #444

Open abbottdev opened 1 month ago

abbottdev commented 1 month ago

Hi.

So I have some types which have their own precomputed embeddings, for example, a PDQ hash. I have implemented these data structures using structs in my code. I have no facility however to be able to use Redis OM for these types.

I tried implementing a custom Vector (and re-implement Vector<T>) without the class constraint but there is an internal prop which I can't override due to it's accessibility.

If I could re-use the existing Vector<T> without the class constraint, I can then build my own vectorizer attributes to extract the relevant embeddings as needed.

slorello89 commented 1 month ago

Hi @abbottdev,

You're really supposed to have a IVectorizer<T> actually do the work of embedding the T in your Vector<T>, so I'm not entirely sure why you would need a non-generic version of Vector, if you really did though, you could always just implement the abstract class and ignore the object, dropping your embedding directly into it's place.

abbottdev commented 1 month ago

@slorello89 - Thanks for the reply - It doesn't need to be non-generic, what I'm saying is that I'm working with structs due to performance reasons I can keep allocations on the heap to a minimum that way - unfortunately as the type constraint on Vector<T> has a class constraint I'm unable to use it:

    public sealed class Vector<T> : Vector, IEquatable<Vector<T>>
    where T : class

https://github.com/redis/redis-om-dotnet/blob/cd8919a9052affdf60cf22504864b3cdd4d8f708/src/Redis.OM/Vector.cs#L55C1-L56C20

I tried to implement the base abstract class, but unfortunately there is aObj variable that's defined internally that I'm unable to override, which means I can't implement the base abstract class:

        internal override object? Obj => Value;

https://github.com/redis/redis-om-dotnet/blob/cd8919a9052affdf60cf22504864b3cdd4d8f708/src/Redis.OM/Vector.cs#L26C1-L30C1