-
Notifications
You must be signed in to change notification settings - Fork 13
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
update README: new vecton index syntax #62
Open
wd0517
wants to merge
6
commits into
main
Choose a base branch
from
update-readme-vector-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
This is a Python client for TiDB Vector. | ||
|
||
> Now only TiDB Cloud Serverless cluster support vector data type, see this [docs](https://docs.pingcap.com/tidbcloud/vector-search-overview?utm_source=github&utm_medium=tidb-vector-python) for more information. | ||
Both TiDB Cloud Serverless ([doc](https://docs.pingcap.com/tidbcloud/vector-search-overview?utm_source=github&utm_medium=tidb-vector-python)) and TiDB Open Source Version (>= 8.4 DMR) support vector data type. | ||
|
||
## Installation | ||
|
||
|
@@ -42,44 +42,51 @@ from tidb_vector.sqlalchemy import VectorType | |
engine = create_engine('mysql://****.root:******@gateway01.xxxxxx.shared.aws.tidbcloud.com:4000/test') | ||
Base = declarative_base() | ||
|
||
class Test(Base): | ||
__tablename__ = 'test' | ||
class Document(Base): | ||
__tablename__ = 'sqlalchemy_documents' | ||
id = Column(Integer, primary_key=True) | ||
embedding = Column(VectorType(3)) | ||
|
||
# or add hnsw index when creating table | ||
class TestWithIndex(Base): | ||
__tablename__ = 'test_with_index' | ||
id = Column(Integer, primary_key=True) | ||
embedding = Column(VectorType(3), comment="hnsw(distance=l2)") | ||
|
||
Base.metadata.create_all(engine) | ||
``` | ||
|
||
Insert vector data | ||
|
||
```python | ||
test = Test(embedding=[1, 2, 3]) | ||
session.add(test) | ||
doc = Document(embedding=[1, 2, 3]) | ||
session.add(doc) | ||
session.commit() | ||
``` | ||
|
||
Get the nearest neighbors | ||
|
||
```python | ||
session.scalars(select(Test).order_by(Test.embedding.l2_distance([1, 2, 3.1])).limit(5)) | ||
session.scalars(select(Document).order_by(Document.embedding.l2_distance([1, 2, 3.1])).limit(5)) | ||
``` | ||
|
||
Get the distance | ||
|
||
```python | ||
session.scalars(select(Test.embedding.l2_distance([1, 2, 3.1]))) | ||
session.scalars(select(Document.embedding.l2_distance([1, 2, 3.1]))) | ||
``` | ||
|
||
Get within a certain distance | ||
|
||
```python | ||
session.scalars(select(Test).filter(Test.embedding.l2_distance([1, 2, 3.1]) < 0.2)) | ||
session.scalars(select(Document).filter(Document.embedding.l2_distance([1, 2, 3.1]) < 0.2)) | ||
``` | ||
|
||
Add vector index to speed up query | ||
|
||
```python | ||
# vector index currently depends on tiflash | ||
session.execute(text('ALTER TABLE sqlalchemy_documents SET TIFLASH REPLICA 1')) | ||
index = Index( | ||
'idx_embedding', | ||
func.vec_cosine_distance(Document.embedding), | ||
mysql_prefix="vector", | ||
) | ||
index.create(engine) | ||
``` | ||
|
||
### Django | ||
|
@@ -119,48 +126,50 @@ db = MySQLDatabase( | |
**connect_kwargs, | ||
) | ||
|
||
class TestModel(Model): | ||
class Meta: | ||
database = db | ||
table_name = 'test' | ||
|
||
class DocumentModel(Model): | ||
embedding = VectorField(3) | ||
|
||
# or add hnsw index when creating table | ||
class TestModelWithIndex(Model): | ||
class Meta: | ||
database = db | ||
table_name = 'test_with_index' | ||
|
||
embedding = VectorField(3, constraints=[SQL("COMMENT 'hnsw(distance=l2)'")]) | ||
|
||
table_name = 'peewee_documents' | ||
|
||
db.connect() | ||
db.create_tables([TestModel, TestModelWithIndex]) | ||
db.create_tables([DocumentModel]) | ||
``` | ||
|
||
Insert vector data | ||
|
||
```python | ||
TestModel.create(embedding=[1, 2, 3]) | ||
DocumentModel.create(embedding=[1, 2, 3]) | ||
``` | ||
|
||
Get the nearest neighbors | ||
|
||
```python | ||
TestModel.select().order_by(TestModel.embedding.l2_distance([1, 2, 3.1])).limit(5) | ||
DocumentModel.select().order_by(DocumentModel.embedding.l2_distance([1, 2, 3.1])).limit(5) | ||
``` | ||
|
||
Get the distance | ||
|
||
```python | ||
TestModel.select(TestModel.embedding.cosine_distance([1, 2, 3.1]).alias('distance')) | ||
DocumentModel.select(DocumentModel.embedding.cosine_distance([1, 2, 3.1]).alias('distance')) | ||
``` | ||
|
||
Get within a certain distance | ||
|
||
```python | ||
TestModel.select().where(TestModel.embedding.l2_distance([1, 2, 3.1]) < 0.5) | ||
DocumentModel.select().where(DocumentModel.embedding.l2_distance([1, 2, 3.1]) < 0.5) | ||
``` | ||
|
||
Add vector index to speed up query | ||
|
||
```python | ||
# vector index currently depends on tiflash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is by design and permanent. If TiFlash component is not deployed corresponding operation should fail, and asking user to deploy a TiFlash in order to use Vector Index (just like FULL TEXT INDEX will fail in MySQL if ENGINE is MEMORY). |
||
db.execute_sql(SQL( | ||
"ALTER TABLE peewee_documents SET TIFLASH REPLICA 1;" | ||
)) | ||
DocumentModel.add_index(SQL( | ||
"CREATE VECTOR INDEX idx_embedding ON peewee_documents ((vec_cosine_distance(embedding)))" | ||
)) | ||
``` | ||
|
||
### TiDB Vector Client | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be encapsulated? User don't know what is TiFlash.
In the syntax design, an explicit SET TIFLASH REPLICA is intentional, because otherwise, adding a vector index to an existing table with existing data may cause replicating a huge amount of data (because of implicit set replica 1) which is out of user expectation. However in this driver, the index can be added right after table creation, thus there is no such risk.
On the other hand, when index is added while table is created is allowed without explicitly setting a TiFlash replica factor, because there is no such risk: