Redis::Commands::Vector
Instance methods
Add element with its associated vector to the vector set stored in key.
redis.vadd "embeddings", vectorize(text), text
You can optionally reduce vector to a given dimensionality with the
reduce argument if you need to sacrifice precision to conserve space.
redis.vadd "embeddings", vectorize(text), text, reduce: 50
You can also set attributes for this vector. Any JSON-serializable value
can be passed into the setattr argument.
redis.vadd "embeddings", vectorize(text), text,
setattr: {
lang: "en",
}
Return the number of vectors contained in the vector set stored in key,
also referred to as the cardinality of the set.
Return the vector for element stored in the vector set in key.
Get the attributes for element stored in key, deserializing them as the
type T.
struct Properties
include JSON::Serializable
getter post_id : Int64
getter author_id : Int64
end
if properties = redis.vgetattr("embeddings", post_text, as: Properties)
author = AuthorQuery.new.find(properties.author_id)
end
NOTE: You can only run this method against Immediate types, such as
Client, Connection, ReplicationClient, or Cluster. Calling this
method on a Pipeline or Transaction will result in a compile-time error.
If you need to run this in a pipeline, you will need to deserialize the
string manually with the other vgetattr overload.
Get the attributes for element stored in key as a JSON string.
struct Properties
include JSON::Serializable
getter post_id : Int64
getter author_id : Int64
end
if (json = redis.vgetattr("embeddings", post_text)) && (properties = Properties.from_json(json))
author = AuthorQuery.new.find(properties.author_id)
end
Return an array of elements between start and end in the vector set
stored in key, optionally limiting the result size to count.
The values for start and end have identical semantics to zrange with
the bylex option
(by: :lex in this shard).
Return the elements of the vector set stored in key whose vectors are most
similar to that of element, which must also be stored in key. You can
limit the similarity comparison using epsilon as an upper limit of cosine
distance or by capping the number of results with count (defaults to 10).
results = redis.vsim("embeddings", text, epsilon: 0.2, count: 25)
Return the elements of the vector set stored in key whose vectors are most
similar to vector, which does not need to be stored in key. You can
limit the similarity comparison using epsilon as an upper limit of cosine
distance or by capping the number of results with count (defaults to 10).
results = redis.vsim("embeddings", vectorize(text), epsilon: 0.2, count: 25)