TDM 20100: Embeddings
Embeddings in LLMs
Embeddings in LLMs are numerical vector representations of text, images, or audio generated by the model. A sentence of text - such as "The sky is blue" - will get converted into text that represents its meaning. These vectors allow computers to search and compare text based on meaning rather than written matches.
Mxbai-embed-large is a highly rated open-source embedding model made by Mixedbread AI. It was designed for high-performance Retrieval-Augmented Generation (RAG) - the process of improving LLM accuracy by using data from external sources when generating a response.
This model can be put in SCRATCH using /anvil/projects/tdm/bin/ollama pull mxbai-embed-large.
Take another modelfile and create a version of mxbai-embed-large that uses just 2 threads.
%%bash
cat > ~/mymodel << HERE
FROM mxbai-embed-large
PARAMETER num_thread 2
HERE
|
Do not forget to use this
|
In your notebook, run the following to test embedding a phrase.
/anvil/projects/tdm/bin/ollama run mxbai-embed-large-2 "The sky is blue"
This should output a long vector of numerical values that represent the phrase you entered.
Take the first ten values from the vectors of three phrases:
# phase: "The sky is blue"
the_sky_is_blue = [-0.021142907,0.009792235,-0.00078518514,-0.041012164,-0.028142547,0.007936959,-0.0028563524,0.04603205,0.07597107,0.043744832]
# phase: "The grass is green"
the_grass_is_green = [-0.050176393,0.011092771,-0.005391531,0.011824846,-0.0063454183,0.052363742,0.033608414,0.010098344,0.04681839,0.04693919]
# phrase: "I like cats"
cats = [-0.015537318,0.03649514,0.026832601,-0.031201454,-0.021033717,-0.04197758,0.008024173,0.042692337,0.0215113,0.024097823]
We would assume that two of these vectors would be more similar to each other than with the third. However, 'the_sky_is_blue', 'the_grass_is_green', and 'cats' contain just ten out of many values in the numerical vectors of their original phrases.
Use:
import numpy as np
similarity = np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
print("Similarity score:", similarity)
….to compare the similarity between each pair of the three vectors.