How pgvector uses SambaNova
pgvector never calls an embedding or chat API. It stores vectors and compares them, so there is no model setting to configure. Your code makes both calls: it embeds text with SambaStack, writes the vectors to Postgres, and sends the retrieved text to a chat model. The same steps work on any Postgres that has pgvector, including managed services that offer it.E5-Mistral-7B-Instruct returns 4096-dimension vectors, which is more than pgvector can index with its standard options. This guide shows what works: exact search for small tables, and a binary quantization index for larger ones. See Index 4096-dimension vectors.
Prerequisites
- A SambaStack instance that serves
E5-Mistral-7B-Instruct, plus the instance’s base URL and API key. See API keys and URLs. - A chat model on that instance. This guide uses
gpt-oss-120b. - A PostgreSQL database with pgvector 0.7.0 or higher, and a connection URL for it. Version 0.7.0 added the binary quantization function used below. To install pgvector, follow its installation instructions.
- Python 3.10 or higher.
Installation and setup
Create a virtual environment
Install dependencies
psycopg 3.3.6, openai 3.35.1, PostgreSQL 17.11, and pgvector 0.8.7.Set your SambaStack base URL, API key, and database URL
/v1.Embed, store, and query
Run the steps in this section in one Python file or session. Each step uses variables from the steps before it.Embed your documents
dimensions argument but ignores it, so you cannot shorten the vectors this way. Every vector has 4096 values.Create a table and insert the vectors
vector column whose size matches dimension. A vector column holds up to 16,000 dimensions. The DROP TABLE IF EXISTS line lets you run the guide again without an already exists error.vector with ::vector. Without the cast, Postgres sees the list as a double precision[] and the distance operators fail.Query the table
E5-Mistral-7B-Instruct is an instruction-tuned model, so write each query as an instruction followed by the question. Documents are embedded as plain text. The <=> operator returns cosine distance, so 1 - distance is the cosine similarity.Answer with a SambaNova chat model
question is the same question you searched with in step 3.The model answers that SambaNova makes the SN40L chip.Index 4096-dimension vectors
For large tables, an approximate index keeps queries fast. pgvector’shnsw and ivfflat indexes cannot cover 4096 dimensions. Their limits are 2,000 dimensions for vector and 4,000 for halfvec, so both of these fail:
LIMIT 20) trades speed for recall. A larger shortlist reranks more candidates. On this four-row table the results match the exact query.
The index expression in the query must match the one in CREATE INDEX exactly, or Postgres will not use the index. To check, run EXPLAIN on the shortlist query and look for Index Scan using items_embedding_bq. On a table this small, Postgres may choose a sequential scan. Run SET enable_seqscan = off first to confirm the index can be used.
Clean up
Additional resources
pgvector on GitHub
psycopg documentation
FastEmbed
Implement embeddings features
Troubleshooting
KeyError: 'DATABASE_URL'
KeyError: 'DATABASE_URL'
DATABASE_URL, SAMBANOVA_BASE_URL, or SAMBANOVA_API_KEY. Set it as shown in Installation and setup, then run the code again from the same shell.column cannot have more than 2000 dimensions for hnsw index
column cannot have more than 2000 dimensions for hnsw index
for ivfflat index when you create an ivfflat index. A vector column with 4096 dimensions is too large for an hnsw or ivfflat index. Use the binary quantization index in Index 4096-dimension vectors, or search without an index.column cannot have more than 4000 dimensions for hnsw index
column cannot have more than 4000 dimensions for hnsw index
halfvec does not help, because its limit is 4,000 dimensions and the model returns 4096. Use binary quantization instead.operator does not exist: vector <=> double precision[]
operator does not exist: vector <=> double precision[]
%s::vector, as in the examples above.function binary_quantize(vector) does not exist
function binary_quantize(vector) does not exist
ALTER EXTENSION vector UPDATE in the database.401 invalid_api_key from the embeddings endpoint
401 invalid_api_key from the embeddings endpoint
SAMBANOVA_BASE_URL points at that instance and ends in /v1. Without /v1, the embeddings request returns 404 page not found.404 model_not_found for E5-Mistral-7B-Instruct
404 model_not_found for E5-Mistral-7B-Instruct

