Embeddings map text to points in a space where closeness means similar meaning. This helps machines understand relationships between words or sentences.
Smaller distance means the embeddings are closer, which usually means the texts they represent are semantically similar.
The method embed_documents is the correct one to get embeddings for multiple texts in Langchain's OpenAIEmbeddings.
embeddings = OpenAIEmbeddings() vec1 = embeddings.embed_documents(['text one']) vec2 = embeddings.embed_documents(['text two']) similarity = vec1 + vec2
The method embed_documents returns a list of vectors. Adding two lists concatenates them, which is not a similarity measure. You should use a function like cosine similarity to compare vectors.
from langchain.embeddings import OpenAIEmbeddings from numpy import dot from numpy.linalg import norm def cosine_similarity(a, b): return dot(a, b) / (norm(a) * norm(b)) embeddings = OpenAIEmbeddings() vecs = embeddings.embed_documents(['apple', 'fruit']) sim = cosine_similarity(vecs[0], vecs[1]) print(round(sim, 2))
Words with related meanings like 'apple' and 'fruit' have embeddings that point in similar directions, so cosine similarity is close to 1.
