Retrieve with context
retrieve_with_contextRetrieve rows similar to a query vector using k-NN, then expand each hit with its parent and child records via foreign keys for context-rich results.
Instructions
Context-packed k-NN retrieval (a one-shot RAG building block). Runs a pgvector k-NN against schema.table.embedding_column for a caller-supplied query_vector (no embedding model needed), then expands each hit one hop along foreign keys and returns the hit row + its related parent / child records in one object. Parents (when include_parents, default true) are the rows a hit references via outbound FKs; children (when include_children, default true) are rows referencing the hit via inbound FKs, capped at max_related (default 5) per FK. Limitations: 1 hop only; inbound (child) expansion is same-schema only. The embedding column is dropped from every returned row. Requires the vector extension. Returns available, dimension, detail, and hits (each with distance, row, and related — a list of fk_name/direction/related_schema/related_table/rows).
Example: retrieve_with_context(schema='public', table='docs', embedding_column='embedding', query_vector=[0.1, 0.2, 0.3], k=5)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| k | No | ||
| table | Yes | ||
| metric | No | l2 | |
| schema | Yes | ||
| database | No | Optional: target a configured secondary (read-only) database by name; omit for the primary. Call list_databases to see the configured ids. | |
| max_related | No | ||
| query_vector | Yes | ||
| include_parents | No | ||
| embedding_column | Yes | ||
| include_children | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hits | Yes | ||
| detail | No | ||
| available | Yes | ||
| dimension | Yes |