karthik-0306 opened a new pull request, #41722:
URL: https://github.com/apache/beam/pull/41722
Fixes #41713
`apache_beam.ml.rag` can write embeddings to Postgres, AlloyDB and Cloud SQL
with `PostgresVectorWriterConfig`, but had no handler to search them; the
search handlers covered BigQuery, Milvus and Qdrant. This adds
`PostgresVectorSearchEnrichmentHandler` for use with `Enrichment`:
```python
handler = PostgresVectorSearchEnrichmentHandler(
ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='10.0.0.5', port=5432, user='beam', password='...',
db_id='rag'),
PostgresVectorSearchParameters(
table_name='embeddings',
limit=5,
distance=PostgresVectorDistance.COSINE,
filter_template="metadata->>'language' = :language",
filter_params_fn=lambda item: {'language':
item.metadata['language']}))
enriched = items | Enrichment(handler)
```
- The defaults (`id`, `embedding`, `content`, `metadata`) match the tables
written by `PostgresVectorWriterConfig` with its default column specs.
- Supports pgvector's cosine (`<=>`), L2 (`<->`) and inner product (`<#>`)
distances.
- Each batch is searched with one query. Each item gets its own `ORDER BY
distance LIMIT k` subquery, combined with `UNION ALL`, so a pgvector index is
used for every item, and filters can take values from each item.
- Embeddings and filter values are bind parameters. Table and column names
are checked against a strict pattern and used unquoted, like
`PostgresVectorWriterConfig` does.
- Results use the same shape as the Milvus handler (`id`, `distance`,
`fields`) in `metadata['enrichment_data']`.
- Statement timeouts and "too many connections" errors are raised as
`UserCodeTimeoutException` and `UserCodeQuotaException`, so `Enrichment`
retries them.
- Connections reuse `CloudSQLConnectionConfig` and
`ExternalSQLDBConnectionConfig` from the Cloud SQL enrichment handler, so no
new dependency is added.
Open questions from #41713, which are easy to change if you prefer another
option:
1. Connection settings come from the Cloud SQL enrichment handler, rather
than the JDBC `ConnectionConfig` used by the ingestion side, because this
handler runs queries in Python through SQLAlchemy and pg8000.
2. The default `max_batch_size` is 100 rather than 1000, since each item
adds a subquery.
Testing:
- `postgres_search_test.py`: 57 unit tests for parameter validation, the
generated SQL and parameters, filters, result grouping, error translation and
retries.
- `postgres_search_it_test.py`: 5 tests against `pgvector/pgvector:pg16`
with testcontainers (marked `require_docker_in_docker`, like the Qdrant tests):
each distance, returned columns, per-item filters including an item with no
matches, and an `EXPLAIN` check that the HNSW index is used for each item. All
5 pass locally in Docker.
- yapf and ruff are clean on the touched files.
R: @claudevdm
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]