Sub topic 10
Creating embeddings locally
This chapter assumes you can already point the OpenAI Python client at a local server, as covered earlier. It also assumes you know what a prompt and a JSON response look like, and that your server has an embedding model downloaded.
What an embedding is
An embedding is a list of numbers (a vector) that represents the meaning of a piece of text. Texts with similar meaning produce vectors that point in similar directions. Search, clustering, and retrieval-augmented generation all rely on this property.
Embeddings come from a different kind of model than chat. A chat model generates text. An embedding model only turns text into a vector. You must pull a dedicated embedding model, such as nomic-embed-text on Ollama. A chat model like llama3.2 is the wrong choice for this endpoint, even if the server accepts the request.
The request
The embeddings endpoint is POST /v1/embeddings. It takes a model and an input. The input can be one string or a list of strings. Sending a list embeds all of them in one request, which is faster than looping.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
result = client.embeddings.create(
model="nomic-embed-text",
input=["local models are private", "hosted models are convenient"],
)
print(len(result.data), len(result.data[0].embedding))The first number printed is the count of vectors, which is 2 here. The second is the vector length, called the dimension. For nomic-embed-text the dimension is 768. The dimension is fixed by the model, so you cannot choose it per request on most local servers.
The response shape
The response holds a data list with one item per input string. Each item has an index and an embedding. Keep results in input order by using the index field if you ever sort or batch them.
Comparing two vectors
A vector alone tells you little. You use it by comparing it with other vectors. The usual measure is cosine similarity, which gives a value near 1 for similar meaning and lower values for unrelated text.
import math
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
texts = [
"How do I reset my password?",
"I forgot my login credentials.",
"The weather is sunny today.",
]
result = client.embeddings.create(model="nomic-embed-text", input=texts)
vectors = [item.embedding for item in result.data]
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
norm_a = math.sqrt(sum(x * x for x in a))
norm_b = math.sqrt(sum(y * y for y in b))
return dot / (norm_a * norm_b)
print("password vs login:", round(cosine(vectors[0], vectors[1]), 3))
print("password vs weather:", round(cosine(vectors[0], vectors[2]), 3))The first score should be clearly higher than the second. Exact numbers depend on the model, so compare scores from the same model only.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| 404 on the request | Model not downloaded, or the server lacks the endpoint | Pull the embedding model and check the `/v1` path |
| Vectors of different lengths | Two different models were used | Embed everything with one model |
| Poor search results | Chat model used for embeddings | Switch to a dedicated embedding model |
| Slow or failing large batch | Too many inputs in one request | Send batches of a few dozen strings |
Never mix vectors from different models in one index. Their dimensions and meaning differ, so comparisons between them are not valid. If you change models, re-embed all of your text.
Exercise
You embedded 500 documents with nomic-embed-text. A teammate then embeds a search query with a different embedding model and compares it to your vectors. Why is the result unreliable, and what is the fix?
Show answer
Different models produce vectors with different dimensions and different meanings, so similarity scores between them are not valid. Embed the query with the same model used for the documents, or re-embed all documents with the new model.