Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic Caching in Java

foogaro 26 views 29 slides Jun 14, 2024
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

Vector databases are redefining data handling, enabling semantic searches across text, images, and audio encoded as vectors.

Redis OM for Java simplifies this innovative approach, making it accessible even for those new to vector data.



This presentation explores the cutting-edge features of vect...


Slide Content

Unleashing the Power of
Vector Search
And
Semantic Caching
in Java

Who?

In the IT industry for over 20 years.

I have always been involved in programming, starting with
QuickBasic, Pascal, Fortran, and moving on to Java in 1996 with
the first Servlet specification.

My professional journey began in 1999 as a WebMaster. Since
then, my career has evolved continuously (with some detours such
as developing a management system in Clipper 5.2) until I joined
Red Hat in 2012, where I spent over 7 fantastic years!

Afterwards, I joined Datadog, where I had the pleasure of
exploring the world of monitoring and observability.

In the summer of 2021, I received a call from Redis - a technology
made in Italy.
Redis is the technology where architectures, data, and applications
thrive together!
Luigi Fugaro
Solutions Architect
[email protected]

Unleashing the Power of
Vector Search
And
Semantic Caching
in Java

Vector embeddings
★USED TO REPRESENT UNSTRUCTURED DATA
★A LIST OF FLOATING-POINT NUMBERS
★IT HAS FIXED SIZE
★COMPACT AND DENSE DATA REPRESENTATION
★PRODUCED BY FEATURE ENGINEERING OR DEEP LEARNING TECHNIQUES
★TRANSLATE PERCEIVED SEMANTIC SIMILARITY TO THE VECTOR SPACE

1.THE INPUT IS TRANSFORMED
INTO A NUMERICAL
REPRESENTATION
2.FEATURES ARE CAPTURED BY THE
NETWORK
3.A LAYER IS EXTRACTED, IT
PROVIDES A DENSE
REPRESENTATION OF THE
FEATURES
4.THIS LAYER IS THE EMBEDDING
AND IS FEASIBLE FOR SIMILARITY
SEARCH
How to create Vector embeddings

Vector embeddings
R
G
B
[0..255]
[0..255]
[0..255]

Vector Database
★A DATABASE THAT STORES VECTOR DATA
★IT CAN INDEX VECTORS
★IT CAN SEARCH THE VECTOR SPACE

Redis as Vector Database
Raw file Embedding ModelVector Embeddings

Vector Similarity Search focuses on finding out how alike or different two
vectors are. To achieve this in a reliable and measurable way, we need a
specific type of score that can be calculated and compared objectively.
These scores are known as distance metrics.
Redis as Vector Database

Redis as Vector Database
Text Embedding ModelVector Embeddings
That is a very
happy person
That is a
happy dog
That is a
sunny day

Vector embeddings
That is a happy dog
That is a sunny day
That is a very happy person

Redis as Vector Database
Text Embedding ModelVector Embeddings
That is a
happy person

Redis as Vector Database
Text Embedding ModelVector Embeddings
That is a very
happy person
That is a
happy dog
That is a
sunny day

Vector embeddings
That is a happy person
That is a happy dog
That is a sunny day
That is a very happy person

Vector Similarity Search features
Vector indexing algorithms
Redis Enterprise manages vectors in an index data structure to enable intelligent
similarity search that balances search speed and search quality. Choose from two
popular techniques, FLAT (a brute force approach) and HNSW (Hierarchical Navigable
Small World - a faster, and approximate approach).
Vector search distance metrics
Redis Enterprise uses a distance metric to measure the similarity between two
vectors. Choose from three popular metrics – Euclidean, Inner Product, and Cosine
Similarity – used to calculate how “close” or “far apart” two vectors are.
Powerful hybrid filtering
Take advantage of the full suite of search features available in Redis Enterprise query
and search. Enhance your workflows by combining the power of vector similarity
with more traditional geo, numeric, text, and tag filters. Incorporate more business
logic into queries and simplify client application code.

Vector Similarity Search features
Real-time updates
Real-time search and recommendation systems generate large volumes of
changing data. New images, text, products, or metadata? Perform updates,
insertions, and deletes to the search index seamlessly as your dataset changes
overtime. Redis Enterprise reduces costly impacts of stagnant data.
Vector range queries
Traditional vector search is performed by finding the “top K” most similar vectors.
Redis Enterprise also enables the discovery of relevant content within a
predefined similarity range or threshold for an alternative, and offers a more
flexible search experience.

How to implement a Generative AI application?

Why?
●Redis OM simplifies application
development by abstracting
Redis' complexity, increasing
productivity, and enhancing
code readability.
What?
◆Redis OM enables building
real-time applications,
supporting:
●Model Data
●Perform CRUD Operations
●Index and Search Data

spring.data.redis.host=35.187.74.111
spring.data.redis.port=12000
spring.data.redis.username =default
spring.data.redis.password =redis
server.port=8080

spring.mvc.hiddenmethod.filter.enabled =true
com.redis.om.vss.useLocalImages =false
com.redis.om.vss.maxLines =300

redis.om.spring.djl.enabled =true

redis.om.spring.djl.image-embedding-model-engine =PyTorch
redis.om.spring.djl.image-embedding-model-model-urls =djl://ai.djl.pytorch/resnet18_embedding

redis.om.spring.djl.sentence-tokenizer-max-length =768
redis.om.spring.djl.sentence-tokenizer-model =sentence-transformers/all-mpnet-base-v2
redis.om.spring.djl.sentence-tokenizer-model-max-length =768

redis.om.spring.djl.face-detection-model-engine =PyTorch
redis.om.spring.djl.face-detection-model-name =retinaface
redis.om.spring.djl.face-detection-model-model-urls =https://resources.djl.ai/test-models/pytorch/retinaface.zip

redis.om.spring.djl.face-embedding-model-engine =PyTorch
redis.om.spring.djl.face-embedding-model-name =face_feature
redis.om.spring.djl.face-embedding-model-model-urls =https://resources.djl.ai/test-models/pytorch/face_feature.zip
Demo Plan B

@Document
public class ImageData {
@Id
private String id;
@Indexed
private String name;
@Indexed
private int height;
@Indexed
private int width;
@Indexed(schemaFieldType = SchemaFieldType.VECTOR,
algorithm = VectorField.VectorAlgorithm.HNSW,
type = VectorType.FLOAT32,
dimension = 512,
distanceMetric = DistanceMetric.L2,
initialCapacity = 10)
private float[] imageEmbedding;
@Vectorize(destination = "imageEmbedding", embeddingType = EmbeddingType.FACE)
private String imagePath;
@Indexed
private double score = 0;

...

}
Demo Plan B

Demo Plan B
@Service
public class BestOfMatchService {

@Autowired
private EntityStream entityStream;
@Autowired
public ZooModel<Image, float[]> faceEmbeddingModel ;

private List<ImageData> matchAll(byte[] image, int limit) {
List<ImageData> imageDataList = new ArrayList<>();
try (Predictor<Image, float[]> predictor = faceEmbeddingModel .newPredictor()) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( image);
Image img = ImageFactory.getInstance().fromInputStream( byteArrayInputStream );
float[] embedding = predictor.predict(img);
byte[] embeddingAsByteArray = floatArrayToByteArray (embedding);

SearchStream<ImageData> stream = entityStream.of(ImageData.class);
List<Pair<ImageData,Double>> matchWithScore = stream
.filter(ImageData$.IMAGE_EMBEDDING.knn(K, embeddingAsByteArray))
.sorted( ImageData$._IMAGE_EMBEDDING_SCORE , SortedField.SortOrder.ASC)
.limit( limit)
.map( Fields.of(ImageData$._THIS, ImageData$._IMAGE_EMBEDDING_SCORE ))
.collect( Collectors.toList());

for (Pair<ImageData,Double> pair : matchWithScore) {
ImageData imageData = pair.getFirst();
Double score = pair.getSecond();
imageData.setScore(score);
imageDataList.add(imageData);
}
return imageDataList;
} catch (Exception e) {
throw new RuntimeException( e);
}
}
}

Demo Plan B

Demo Plan B

Demo Plan B

Demo Plan B

Databases like
Redis
and frameworks like
Redis OM
can help!

Redis AI ecosystem
INTEGRATIONS
FEATURES
Storage: HASH | JSON
Indexing: HNSW (ANN) | Flat (KNN)
Distance: L2 | Cosine | IP
Search Types: KNN/ANN | Hybrid | Range | Full Text
Management: Realtime CRUD operations, aliasing,
temp indices, and more
Ecosystem integrations
NEW REDIS ENTERPRISE 7.2 FEATURE
Scalable search and query for improved performance,
up to 16X compared to previous versions

ⓒ 2024 Redis Ltd. All rights reserved. 29