Ports vectorstore/{rest_client,rest_writer,store,reader,embeddings,records},
evidence/model (leaf dep of records), and cli/_guards (require_vector_write_allowed
workstation write-guard). Renamed psdwp3->nsp, verbatim.
VectorRestClient gains an api_key property so reader/writer clients carry their
distinct keys visibly (spec D11: vector_reader / vector_writer on the same endpoint).
scripts/create_vector_reader_rpc.sql is NEW: the reader RPCs (search_similar,
list_tables) lived server-side in Supabase and were never versioned. Authored now
mirroring the writer allowlist pattern (table allowlist, security definer, revoke
from anon/authenticated, grant to vector_reader only). Writer RPC ported verbatim.
L1: test_vector_dual_key (7 tests) pins the dual-key construction + the workstation
write-guard (exit 4 without writer key).
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import requests
|
|
|
|
from nsp.config import EmbeddingsConfig
|
|
|
|
DOC_PREFIX = "search_document: "
|
|
QUERY_PREFIX = "search_query: "
|
|
|
|
|
|
class EmbeddingsError(Exception):
|
|
pass
|
|
|
|
|
|
class OllamaEmbeddings:
|
|
"""Client embeddings via Ollama. Applica i prefissi di task richiesti da nomic v2:
|
|
ometterli degrada il retrieval in modo silenzioso."""
|
|
|
|
def __init__(self, cfg: EmbeddingsConfig):
|
|
self.cfg = cfg
|
|
|
|
def _embed(self, texts: list[str]) -> list[list[float]]:
|
|
url = f"{self.cfg.base_url.rstrip('/')}/api/embed"
|
|
out: list[list[float]] = []
|
|
for i in range(0, len(texts), self.cfg.batch_size):
|
|
batch = texts[i : i + self.cfg.batch_size]
|
|
try:
|
|
resp = requests.post(
|
|
url, json={"model": self.cfg.model, "input": batch},
|
|
timeout=self.cfg.timeout,
|
|
)
|
|
resp.raise_for_status()
|
|
except requests.RequestException as e:
|
|
raise EmbeddingsError(
|
|
f"Ollama non raggiungibile su {self.cfg.base_url} "
|
|
f"(modello {self.cfg.model}): {e}"
|
|
) from e
|
|
embeddings = resp.json().get("embeddings", [])
|
|
for v in embeddings:
|
|
if len(v) != self.cfg.dim:
|
|
raise EmbeddingsError(
|
|
f"dimensione embedding inattesa: {len(v)} != {self.cfg.dim} "
|
|
f"(modello {self.cfg.model})"
|
|
)
|
|
out.extend(embeddings)
|
|
return out
|
|
|
|
def embed_documents(self, texts: list[str]) -> list[list[float]]:
|
|
return self._embed([DOC_PREFIX + t for t in texts])
|
|
|
|
def embed_query(self, text: str) -> list[float]:
|
|
return self._embed([QUERY_PREFIX + text])[0]
|