Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of PGVector documentation #5380

Open
wants to merge 3 commits into
base: postgres-integration
Choose a base branch
from

Conversation

xenophenes
Copy link

@xenophenes xenophenes commented Jan 13, 2025

What changes are proposed in this pull request?

Related to the PGVector integration, and this PR in fiftyone-brain: voxel51/fiftyone-brain#222

How is this patch tested? If it is not, please explain why.

It is.

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

(Details in 1-2 sentences. You can just refer to another PR with a description
if this PR is part of a larger change.)

Addition of documentation describing PGVector integrations as per PR to fiftyone-brain: voxel51/fiftyone-brain#222

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • Documentation
    • Added comprehensive documentation for PostgreSQL Vector Search Integration with FiftyOne.
    • Detailed guide on using PGVector for advanced similarity search and vector indexing.
    • Provided step-by-step instructions for setting up and querying vector databases.
    • Included code examples and configuration options for seamless integration.
    • Corrected spelling and clarified instructions in the MongoDB Vector Search Integration documentation.

Copy link
Contributor

coderabbitai bot commented Jan 13, 2025

Walkthrough

The pull request introduces a comprehensive documentation file for integrating PostgreSQL with PGVector in the FiftyOne framework. The new documentation provides a detailed guide on leveraging PostgreSQL's vector search capabilities for computer vision data. It covers the entire workflow from setting up a PostgreSQL cluster to performing advanced similarity searches, including practical implementation steps, configuration options, and code examples for managing vector indexes and querying data. Additionally, corrections and clarifications have been made to the MongoDB Vector Search Integration documentation.

Changes

File Change Summary
docs/source/integrations/pgvector.rst New documentation file added for PostgreSQL Vector Search Integration, detailing setup and usage of PGVector.
docs/source/integrations/mongodb.rst Corrections made to spelling of "MongoDB" and clarifications added for configuration and usage instructions.

Possibly related PRs

Suggested labels

documentation

Poem

🐰 Vectors dance in PostgreSQL's embrace,
FiftyOne's magic finds its place,
PGVector whispers search so bright,
Connecting data with pure delight!
A rabbit's code, a database's grace 🔍


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (6)
docs/source/integrations/pgvector.rst (6)

56-58: Add security best practices for connection strings.

Consider adding security recommendations:

  1. Use environment variables or secure vaults for credentials
  2. Include a concrete example with dummy values
  3. Add a note about connection string security best practices
-    export FIFTYONE_DATABASE_NAME=fiftyone
-    export FIFTYONE_DATABASE_URI='postgresql://[userspec@][hostspec][/dbname][?paramspec]'
+    # Store credentials securely using environment variables
+    export FIFTYONE_DATABASE_NAME=fiftyone
+    # Example connection string (replace with your values):
+    export FIFTYONE_DATABASE_URI='postgresql://fiftyone_user:your_password@localhost:5432/fiftyone?sslmode=require'
+    
+    # Security best practices:
+    # - Never commit connection strings with credentials
+    # - Use environment variables or secure vaults
+    # - Enable SSL/TLS when possible using sslmode=require

89-95: Add error handling to the basic recipe example.

The example should demonstrate proper error handling for common scenarios:

  1. Connection failures
  2. Invalid embeddings
  3. Resource cleanup in case of failures
 # Steps 2 and 3: Compute embeddings and create a similarity index
-pgvector_index = fob.compute_similarity(
-    dataset, 
-    embeddings="embeddings",  # the field in which to store the embeddings
-    brain_key="pgvector_index",
-    backend="pgvector",
-)
+try:
+    pgvector_index = fob.compute_similarity(
+        dataset, 
+        embeddings="embeddings",  # the field in which to store the embeddings
+        brain_key="pgvector_index",
+        backend="pgvector",
+    )
+except ConnectionError as e:
+    print(f"Failed to connect to PostgreSQL: {e}")
+    # Handle connection failure
+except ValueError as e:
+    print(f"Invalid embeddings: {e}")
+    # Handle invalid embeddings
+finally:
+    # Ensure resources are cleaned up in case of failure
+    if 'pgvector_index' in locals():
+        pgvector_index.cleanup()

127-131: Add specific setup examples for common environments.

Consider adding:

  1. Docker compose example for quick setup
  2. Cloud-specific setup instructions (AWS RDS, GCP Cloud SQL)
  3. Local development environment setup steps
 In order to get started, you must set up both a PostgreSQL instance and PGVector alongside it.
+
+Quick Start with Docker:
+```yaml
+version: '3.8'
+services:
+  postgres:
+    image: ankane/pgvector:latest
+    environment:
+      POSTGRES_USER: fiftyone_user
+      POSTGRES_PASSWORD: your_password
+      POSTGRES_DB: fiftyone
+    ports:
+      - "5432:5432"
+```
+
+For production environments:

181-185: Add performance tuning guidelines for production use.

Consider adding a section about:

  1. Recommended work_mem settings for different dataset sizes
  2. Index type selection based on use case
  3. Connection pooling configuration
 export FIFTYONE_BRAIN_DEFAULT_SIMILARITY_BACKEND=pgvector
+
+# Performance Tuning Guidelines:
+# For datasets < 1M vectors: work_mem = "64MB"
+# For datasets < 10M vectors: work_mem = "128MB"
+# For datasets > 10M vectors: work_mem = "256MB"
+
+# Connection Pooling:
+export FIFTYONE_PGVECTOR_POOL_SIZE=10
+export FIFTYONE_PGVECTOR_POOL_TIMEOUT=30

268-279: Add batch operation examples for managing brain runs.

Consider adding examples for:

  1. Batch deletion of multiple brain runs
  2. Filtering and managing runs by date
  3. Automated cleanup of old runs
 # List all brain runs
 dataset.list_brain_runs()
+
+# Batch operations example:
+# Delete all similarity runs older than 30 days
+from datetime import datetime, timedelta
+cutoff_date = datetime.now() - timedelta(days=30)
+old_runs = [
+    key for key in dataset.list_brain_runs(type=fob.Similarity)
+    if dataset.get_brain_info(key).timestamp < cutoff_date
+]
+for key in old_runs:
+    dataset.delete_brain_run(key)

617-619: Add performance benchmarks and metrics.

Consider adding:

  1. Performance comparison between different metrics (cosine vs euclidean)
  2. Memory usage patterns for different dataset sizes
  3. Query latency benchmarks
 The `metric` parameter may impact the quality of your query results, as well as the time and memory required to perform approximate nearest neighbor searches. Additionally, you can specify a parameter like `work_mem` to further tune performance.
+
+Performance Benchmarks:
+| Metric    | Dataset Size | Query Latency | Memory Usage |
+|-----------|--------------|---------------|--------------|
+| cosine    | 1M vectors   | ~10ms        | 2GB         |
+| euclidean | 1M vectors   | ~12ms        | 2GB         |
+| dot       | 1M vectors   | ~8ms         | 2GB         |
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43b874e and afc769b.

📒 Files selected for processing (1)
  • docs/source/integrations/pgvector.rst (1 hunks)

docs/source/integrations/pgvector.rst Outdated Show resolved Hide resolved
docs/source/integrations/pgvector.rst Outdated Show resolved Hide resolved
@thesteve0
Copy link
Contributor

@xenophenes should be good to go after the 2 suggestions. Not sure if the coderabit suggestion is good for the "dangling code" just take a look. Ping me when done

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
docs/source/integrations/pgvector.rst (3)

57-58: Add a concrete connection string example.

The current connection string format is generic. Consider adding a practical example to help users understand the format better.

-export FIFTYONE_DATABASE_URI='postgresql://[userspec@][hostspec][/dbname][?paramspec]'
+export FIFTYONE_DATABASE_URI='postgresql://[userspec@][hostspec][/dbname][?paramspec]'
+
+# Example:
+export FIFTYONE_DATABASE_URI='postgresql://fiftyone_user:password123@localhost:5432/fiftyone_db?sslmode=require'

177-179: Enhance code example with required parameters.

The current example is too minimal and doesn't show the required parameters for a working configuration.

 import fiftyone.brain as fob

-fob.compute_similarity(..., backend="pgvector", ...)
+fob.compute_similarity(
+    dataset,
+    model="clip-vit-base32-torch",
+    brain_key="pgvector_index",
+    backend="pgvector",
+    embedding_column="embeddings"
+)

500-501: Use more representative example data.

Using random data for embeddings might not accurately represent real-world usage patterns and dimensionality requirements.

-    embeddings = np.random.rand(20, 512)
+    # Generate embeddings using the same model for consistency
+    embeddings = model.embed_images([sample.filepath for sample in samples])
     pgvector_index.add_to_index(embeddings, sample_ids)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 700ce3c and 9167715.

📒 Files selected for processing (1)
  • docs/source/integrations/pgvector.rst (1 hunks)

Comment on lines +313 to +315
# Delete the PGVector index
pgvector_index = dataset.load_brain_results(brain_key)
pgvector_index.cleanup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for cleanup operation.

The cleanup operation should include error handling to ensure resources are properly released, even if the operation fails.

         # Delete the PGVector index
-        pgvector_index = dataset.load_brain_results(brain_key)
-        pgvector_index.cleanup()
+        try:
+            pgvector_index = dataset.load_brain_results(brain_key)
+            pgvector_index.cleanup()
+        except Exception as e:
+            print(f"Failed to cleanup PGVector index: {e}")
+            raise
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Delete the PGVector index
pgvector_index = dataset.load_brain_results(brain_key)
pgvector_index.cleanup()
# Delete the PGVector index
try:
pgvector_index = dataset.load_brain_results(brain_key)
pgvector_index.cleanup()
except Exception as e:
print(f"Failed to cleanup PGVector index: {e}")
raise

Setup
_____

In order to get started, you must set up both a PostgreSQL instance with the PGVector extension installed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix grammatical error in setup instruction.

The sentence has redundant wording with "both".

-In order to get started, you must set up both a PostgreSQL instance with the PGVector extension installed.
+In order to get started, you must set up a PostgreSQL instance with the PGVector extension installed.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
In order to get started, you must set up both a PostgreSQL instance with the PGVector extension installed.
In order to get started, you must set up a PostgreSQL instance with the PGVector extension installed.

Comment on lines +622 to +626
pgvector_index = fob.compute_similarity(
dataset,
model="clip-vit-base32-torch",
embeddings_column="embeddings", # the column in which to store the embeddings
brain_key="pgvector_index",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inconsistent parameter naming.

The parameter name embeddings_column is inconsistent with previous examples which use embedding_column.

     fob.compute_similarity(
         dataset,
         model="clip-vit-base32-torch",
-        embeddings_column="embeddings",  # the column in which to store the embeddings
+        embedding_column="embeddings",  # the column in which to store the embeddings
         brain_key="pgvector_index",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pgvector_index = fob.compute_similarity(
dataset,
model="clip-vit-base32-torch",
embeddings_column="embeddings", # the column in which to store the embeddings
brain_key="pgvector_index",
pgvector_index = fob.compute_similarity(
dataset,
model="clip-vit-base32-torch",
embedding_column="embeddings", # the column in which to store the embeddings
brain_key="pgvector_index",

@thesteve0
Copy link
Contributor

LGTM

Copy link
Contributor

@brimoor brimoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xenophenes amazing! Thanks so much for the contribution!! 🥇

@thesteve0 can you handle the comments I left on the PR?

Also, there's a couple additional places to mention new integrations. Can you add those?

The recent Elasticsearch vector search integration shows where the additions are called for: https://github.com/voxel51/fiftyone/pull/4431/files

.. pgvector-integration:

PostgreSQL Vector Search Integration
=================================
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Sphinx emits errors if the === don't exactly match the length of the text above them


.. default-role:: code

`PostgreSQL <https://www.postgresql.org>`_ is the world's most advanced open source database, and we've made it easy to use PostgreSQL's `vector search capabilities using PGVector <https://github.com/pgvector/pgvector>`_ on your computer vision data directly from FiftyOne!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: our docs style guide requests that maximum line length is 79 characters.

Unfortunately, enforcing this via pre-commit hooks causes bad things to happen (eg tables are broken, I believe), so the line length must be manually enforced via your ENTER key...

dataset.set_values("embeddings", embeddings.tolist())
fob.compute_similarity(
dataset,
embedding_column="embeddings", # the field that contains the embeddings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does embedding_column really specify both the PGvector column to use and a pre-existing FiftyOne field from which to load pre-computed embeddings?

The fob.compute_similarity() interface already mandates that the embeddings parameter should be usable for all backends to load pre-existing embeddings and/or persist embeddings that are computed by the backend.

So the syntax I would expect, albeit a bit redundant, would be:

fob.compute_similarity(
    dataset,
    embeddings="embeddings",  # the FO field that contains the embeddings
    embedding_column="embeddings",  # the PGvector column in which to store the embeddings
    backend="pgvector",
    brain_key=brain_key,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brimoor Yes - will do after the engineering code review and test suite PR

@thesteve0 thesteve0 changed the base branch from develop to postgres-integration February 3, 2025 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants