-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add alembic operations for vectorizer (#266)
* feat: add alembic operations for vectorizer * chore: cleanup set up of operations * chore: add shared base class * docs: update docs * chore: unify sql generation * chore: add more test cases * chore: simplify code and tests a bit * chore: use shared base classes, make use of more optional params * chore: revert dockerfile change * chore: move configuration to alembic package * feat: add code generation for migration dataclasses * chore: downgrade voyageai for tests * chore: rename table_name to target_table_name * feat: expose CreateVectorizer directly and add docs for it * chore: update docs/python-integration.md Co-authored-by: James Guthrie <[email protected]> Signed-off-by: Jascha Beste <[email protected]> * chore: update projects/pgai/pgai/vectorizer/generate/README.md Co-authored-by: James Guthrie <[email protected]> Signed-off-by: Jascha Beste <[email protected]> * chore: fix link to code gen * chore: remove default_value from code generation * chore: add some tests for vectorizer creation from python * chore: upgrade uv to 0.5.20 --------- Signed-off-by: Jascha Beste <[email protected]> Co-authored-by: James Guthrie <[email protected]>
- Loading branch information
1 parent
3ce3134
commit b01acfe
Showing
27 changed files
with
2,591 additions
and
1,143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pgai.alembic.operations import ( | ||
CreateVectorizerOp, | ||
DropVectorizerOp, | ||
register_operations, | ||
) | ||
|
||
__all__ = ["CreateVectorizerOp", "DropVectorizerOp", "register_operations"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Any | ||
|
||
from alembic.operations import MigrateOperation, Operations | ||
from sqlalchemy import text | ||
|
||
from pgai.vectorizer.create_vectorizer import CreateVectorizer | ||
|
||
|
||
class CreateVectorizerOp(MigrateOperation): | ||
def __init__( | ||
self, | ||
**kw: dict[str, Any], | ||
): | ||
self.params = CreateVectorizer( | ||
**kw # type: ignore | ||
) | ||
|
||
@classmethod | ||
def create_vectorizer(cls, operations: Operations, **kw: Any): | ||
op = CreateVectorizerOp(**kw) # type: ignore | ||
return operations.invoke(op) | ||
|
||
|
||
class DropVectorizerOp(MigrateOperation): | ||
def __init__(self, target_table: str | None, drop_all: bool): | ||
self.target_table = target_table | ||
self.drop_all = drop_all | ||
|
||
@classmethod | ||
def drop_vectorizer( | ||
cls, | ||
operations: Operations, | ||
target_table: str | None, | ||
drop_all: bool = True, | ||
): | ||
op = DropVectorizerOp(target_table, drop_all) | ||
return operations.invoke(op) | ||
|
||
|
||
def create_vectorizer(operations: Operations, operation: CreateVectorizerOp): | ||
params = operation.params | ||
operations.execute(params.to_sql()) | ||
|
||
|
||
def drop_vectorizer(operations: Operations, operation: DropVectorizerOp): | ||
connection = operations.get_bind() | ||
result = connection.execute( | ||
text("SELECT id FROM ai.vectorizer WHERE target_table = :table_name"), | ||
{"table_name": operation.target_table}, | ||
).scalar() | ||
|
||
if result is None: | ||
return | ||
|
||
# Drop the vectorizer | ||
connection.execute( | ||
text("SELECT ai.drop_vectorizer(:id, drop_all=>:drop_all)"), | ||
{"id": result, "drop_all": operation.drop_all}, | ||
) | ||
|
||
|
||
_operations_registered = False | ||
|
||
|
||
def register_operations(): | ||
global _operations_registered | ||
|
||
if not _operations_registered: | ||
Operations.register_operation("create_vectorizer")(CreateVectorizerOp) | ||
Operations.register_operation("drop_vectorizer")(DropVectorizerOp) | ||
Operations.implementation_for(CreateVectorizerOp)(create_vectorizer) | ||
Operations.implementation_for(DropVectorizerOp)(drop_vectorizer) | ||
_operations_registered = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .create_vectorizer import CreateVectorizer | ||
from .vectorizer import Vectorizer, Worker | ||
|
||
__all__ = ["Vectorizer", "Worker"] | ||
__all__ = ["Vectorizer", "Worker", "CreateVectorizer"] |
Oops, something went wrong.