Semantic search represents text as numbers so an application can compare meaning rather than only matching words. lmstudio-python generates these embedding vectors through a model running locally in LM Studio.
An embedding model produces a vector for each input, while a chat model produces a text reply. Nomic Embed Text v1.5 uses task prefixes to distinguish documents from search queries, so the prefix is part of the text sent to the model.
The Python process and LM Studio run on the same machine, with Python 3.10 or newer in an activated project virtual environment. SDK 1.5.0 and newer can discover the local daemon directly without the optional HTTP server; the default local API does not require a token unless authentication has been enabled.
Steps to generate embeddings with lmstudio-python:
- Install libgomp1 on Ubuntu or Debian for the GGUF inference runtime.
$ sudo apt install --assume-yes libgomp1
This dependency step applies to Ubuntu and Debian. Only OS package installation needs sudo; LM Studio, the model, and Python run under your normal account.
Related: How to select an LM Studio runtime from the CLI - Start the local LM Studio daemon.
$ lms daemon up
LM Studio and lms must already be installed and available on your command path. On a headless Linux machine, llmster supplies the daemon.
Related: How to run LM Studio headlessly with llmster - Install the official lmstudio package in your activated Python virtual environment.
$ python -m pip install lmstudio
The package is named lmstudio on PyPI, although the SDK is called lmstudio-python. The virtual environment keeps it separate from system Python packages.
Related: How to create a virtual environment in Python
Related: How to activate a Python virtual environment - Download Nomic Embed Text v1.5 in Q4_K_M GGUF format.
$ lms get \ https://huggingface.co/nomic-ai/nomic-embed-text-v1.5-GGUF@q4_k_m \ --gguf --yesThe full publisher URL selects the embedding model, and @q4_k_m fixes the quantization. The file occupies about 84 MB. LM Studio reuses the file if it is already present, including the copy bundled with current installations.
Related: How to download a model with lms in LM Studio - Create generate_embeddings.py in your project directory using your text editor.
- generate_embeddings.py
import lmstudio as lms texts = [ "search_document: A bicycle has two wheels.", "search_document: Bread dough rises in a warm kitchen.", ]
The list contains two documents to embed. search_document: is the model's prefix for indexed text; search_query: is used for a question that will be compared with document vectors. These prefixes are specified in the Nomic model card.
- Append the model selection and embedding call below the texts list in generate_embeddings.py.
- generate_embeddings.py
model = lms.embedding_model("text-embedding-nomic-embed-text-v1.5") embeddings = model.embed(texts)
embedding_model() accesses the downloaded Nomic model and loads it if necessary. embed(texts) returns one vector per list item, in the same order. A single string passed to embed() returns one vector instead. The SDK sends the text to the local LM Studio process.
Related: LM Studio Python embedding API - Append the vector counts and previews at the end of generate_embeddings.py.
- generate_embeddings.py
print(f"Inputs: {len(texts)}") print(f"Vectors: {len(embeddings)}") for index, vector in enumerate(embeddings, start=1): print(f"Text {index}: {len(vector)} dimensions") print("First 5 values:", [round(value, 6) for value in vector[:5]])
The lengths come from the returned vectors. Only the first five coordinates are printed, rounded to six decimal places; embeddings still holds every coordinate.
- Save generate_embeddings.py in your text editor.
- Run generate_embeddings.py to confirm that both input texts produce embedding vectors. Example output:
$ python generate_embeddings.py Inputs: 2 Vectors: 2 Text 1: 768 dimensions First 5 values: [0.012568, 0.094722, -0.152154, 0.039994, 0.084031] Text 2: 768 dimensions First 5 values: [-0.001026, 0.062819, -0.203077, 0.026368, -0.028452]
Each of the two inputs should have its own 768-dimensional vector with finite numeric values. The coordinate values can vary with quantization and runtime. A connection error or missing-model exception means no embeddings were returned. The 768 dimensions apply to this Nomic model without dimensionality reduction; other embedding models can use different lengths.