Let's learn how embeddings work.
So embeddings are what is called semantic meaning
and similarity searches.
So it's not like it's doing the old
style search of if the two words are
the same or fuzzy search and so on.
It's more a semantic search where we have
AI involved.
So in a normal search, a kitten would
not be close to a cat because it
has totally different length, totally different letters and
so on.
But in a vector store where you had
words like wolf, dog, cat, banana, apple, the
word kitten would go close to a cat.
And behind the scenes, such vectors have 1536
different dimensions.
In our world, we of course have the
three or the four if you take time.
But in mathematics, you can have infinite amount
of vectors and dimensions.
So that is how this works.
This is a bit abstract, so we are
going to go into some code and actually
show a little on how we make these
vectors, how they look like, and also how
we check that something fits together.
So in here, we have an embedding data
sample.
And we're using the normal client here, but
now we're not going to new up a
chat client agent.
We don't use it at all in this
scenario.
Instead, we're taking what is called an embedding
client.
And in OpenAI's terms, one of the best
they have is text embedding tree small.
You also have a larger one, but not
all vector stores can support such big embeddings,
so I tend to use this one.
So if we make one of these embedding
generators, we can actually embed this sentence.
In this case, I've just made a little
Q&A part of our Wi-Fi data.
What is the Wi-Fi password at the
office?
And the office password, it's guest 42.
And if we use this embedding generator, just
say generate this text and turn it into
a vector.
So this actually sent up a signal to
OpenAI and got back.
So embeddings are rather fast and also rather
cheap, way cheaper than normal LLM tokens in
terms of cost and speed.
So what we get back is a vector
and we get how when it was created,
what model did it, and then we get
the vector back.
And the vector is just a long series
of numbers.
And I will not claim to understand this
fully because I'm not a mathematician, but we
can technically write it out if we want
to and see how it looks.
And it doesn't give too much meaning for
us.
It's just 1536 of these numbers back and
forward in various different dimensions.
How will it fit that dimension?
This is a rather small embedding.
We can also do a larger embedding here.
I have the first chapter of write and
produce that I can turn into a vector.
And all vectors become these 1536 dimensions.
So it's not like this vector I create
here is any bigger or any smaller than
the one we just made.
It has the same things here.
But what happens is the smaller we make
it, the less defined it will be to
know what area this is in, what area
this is in.
And the bigger, the more it needs to
compress the data and lose some of its
meaning.
So I would say a good vector is
roughly in the middle of such things like
this.
This might be okay in terms of size.
But the more you put into a vector,
the more it will lose its value.
But you can also, if you just put
in every single word into a vector, it
wouldn't know what to do with that because
it will just be too generic for anything.
Vectors can technically be very long.
You can put in up to 6000 words
roughly because they're counted in tokens.
We cannot really know exactly precisely, but that's
roughly 10 to 15 pages if you wish
to.
But figuring out how big a vector should
be, should it be one sentence at a
time, should it be one paragraph at a
time, should it be one chapter at a
time, a page, and so on, is a
whole science that we can talk, we could
make a course just about that by itself.
And I'm no expert, but I'm slowly learning
as we go along.
What we can do is, of course, we
can go in and show that same vector
and we'll just see a whole lot of
numbers again with different values in terms of
the different dimensions.
But again, it's not like just because it
was a longer text that it's a longer
vector.
They will be exactly the same length.
So now we have some vectors and I've
just asked ChatGBT to make me a co
-similarity search score that can compare two vectors
to each other and see how close they
are together.
Because that's how things work.
So I won't claim that I understand this
code specifically, but it's going in and figuring
out how close are two vectors to each
other and giving a value between zero, meaning
it had absolutely nothing to do with each
other, and one being it was the exact
same sentence that was vectorised.
So if we go in and ask, how
close are our Wi-Fi data with the
book data?
And hopefully you will intuitively know that in
private purchase, they probably don't talk a lot
about Wi-Fi and offices and so on.
So they're probably not too similar.
And what we see is that that's correct.
It's like 0.10 out of 1.
So a fairly low score.
But if we want to check a bit
more realistic, we can make a small vector
here that says, how can my customer use
the Wi-Fi?
This is what a real person would perhaps
ask for.
They wouldn't know to ask, what is the
Wi-Fi for the office and so on.
They would just, I have a customer at
the office at the moment.
They need to get onto the Wi-Fi.
They don't specify the word guest.
They don't specify office and anything.
So we can make a vector of that
and check the similarity search between our Wi
-Fi data and the question.
And here we get a better score, 0
.45, which is a rather okay score, but
of course it's not a one.
But again, a one would never really happen,
because if that were to happen, the end
user would have written exactly this sentence with
colons in exactly the same places and so
on.
So a 45 is a pretty okay, and
if we only had these two, this would
of course be the best match.
We can ask in a different way, what
is the office Wi-Fi?
And if we do that, we get an
even better score, because there's more similarity of
this sentence compared to our original Q&A
in terms of this question.
But again, if the book is totally different,
so we had these three, then this one
would be the best way of curing to
get our right information.
So it's all about these numbers, and in
reality, we are not sitting and doing the
match score ourselves.
That's the job of a vector store, and
we are just doing here a sample.
I've never in real life written code like
this, but in order for us to quickly
see that we have different scores for different
vectors when they're compared, it's nice to know.
But again, when we go to the vector
store in the next lecture, we will let
that do this similarity score.
In this case, we're using cosine similarity, where
it's between 0 and 1.
Others use from minus 1 to 1.
Some use from 0 to 2.
So there's different ways.
So you need to check on your vector
store what is a good score, because sometimes
a negative score is a good score.
That's all depending on the implementation of how
the mathematician have said, this is the best
way for me to check two vectors together.
But now we should be ready to go
to our vector store part.