Skip to content

Commit 3740022

Browse files
authored
examples: use new vector index syntax (#70)
1 parent 8aed41f commit 3740022

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

examples/image_search/example.ipynb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@
8989
"\n",
9090
" id = Column(Integer, primary_key=True)\n",
9191
" image_id = Column(Integer)\n",
92-
" embedding = Column(\n",
93-
" VectorType(CLIP_DIMENSION),\n",
94-
" # using hnsw index with cosine distance\n",
95-
" comment=\"hnsw(distance=cosine)\"\n",
96-
" )\n",
92+
" embedding = Column(VectorType(CLIP_DIMENSION))\n",
9793
"\n",
9894
"Base.metadata.drop_all(engine)\n",
9995
"Base.metadata.create_all(engine)"

examples/jina-ai-embeddings-demo/jina-ai-embeddings-demo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class Document(Base):
4747
# DIMENSIONS is determined by the embedding model,
4848
# for Jina AI's jina-embeddings-v2-base-en model it's 768.
4949
VectorType(dim=768),
50-
comment="hnsw(distance=cosine)"
5150
)
5251

5352

examples/orm-peewee-quickstart/.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ TIDB_PORT=4000
33
TIDB_USERNAME=******.root
44
TIDB_PASSWORD=********
55
TIDB_DATABASE=test
6-
# For macOS. For other platforms, please refer https://docs.pingcap.com/tidbcloud/secure-connections-to-serverless-clusters#root-certificate-default-path.
7-
TIDB_CA_PATH=/etc/ssl/cert.pem
6+
# TiDB Serverless Cluster requires SSL connection for public network access.
7+
# For local TiDB cluster, please set TIDB_SSL=false to disable SSL.
8+
TIDB_SSL=true

examples/orm-peewee-quickstart/peewee-quickstart.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import os
22
import dotenv
33

4-
from peewee import Model, MySQLDatabase, SQL, TextField
5-
from tidb_vector.peewee import VectorField
4+
from tidb_vector.peewee import VectorField, VectorAdaptor
5+
from tidb_vector.constants import DistanceMetric
6+
from peewee import Model, MySQLDatabase, TextField
67

78
dotenv.load_dotenv()
89

910
# Step 1: Connect to TiDB using Peewee.
1011

1112
# Using `pymysql` as the driver.
12-
connect_kwargs = {
13+
ssl_kwargs = {
1314
'ssl_verify_cert': True,
1415
'ssl_verify_identity': True,
1516
}
1617

1718
# Using `mysqlclient` as the driver.
18-
# connect_kwargs = {
19+
# ssl_kwargs = {
1920
# 'ssl_mode': 'VERIFY_IDENTITY',
2021
# 'ssl': {
2122
# # Root certificate default path
@@ -30,7 +31,7 @@
3031
password=os.environ.get('TIDB_PASSWORD', ''),
3132
host=os.environ.get('TIDB_HOST', 'localhost'),
3233
port=int(os.environ.get('TIDB_PORT', '4000')),
33-
**connect_kwargs,
34+
**ssl_kwargs if os.environ.get('TIDB_SSL', 'false').lower() == 'true' else {},
3435
)
3536

3637

@@ -53,12 +54,16 @@ class Meta:
5354
table_name = 'peewee_demo_documents_with_index'
5455

5556
content = TextField()
56-
embedding = VectorField(3, constraints=[SQL("COMMENT 'hnsw(distance=cosine)'")])
57+
embedding = VectorField(3)
5758

5859

5960
db.connect()
6061
db.drop_tables([Document, DocumentWithIndex])
6162
db.create_tables([Document, DocumentWithIndex])
63+
VectorAdaptor(db).create_vector_index(
64+
DocumentWithIndex.embedding,
65+
DistanceMetric.COSINE,
66+
)
6267

6368
# Step 3. Insert embeddings into the table.
6469
Document.create(content='dog', embedding=[1, 2, 1])
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
TIDB_DATABASE_URL=mysql+pymysql://<USERNAME>:<PASSWORD>@<HOST>:4000/<DATABASE>?ssl_ca=<CA>&ssl_verify_cert=true&ssl_verify_identity=true
1+
TIDB_HOST=gateway01.****.prod.aws.tidbcloud.com
2+
TIDB_PORT=4000
3+
TIDB_USERNAME=******.root
4+
TIDB_PASSWORD=********
5+
TIDB_DATABASE=test
6+
# TiDB Serverless Cluster requires SSL connection for public network access.
7+
# For local TiDB cluster, please set TIDB_SSL=false to disable SSL.
8+
TIDB_SSL=true

examples/orm-sqlalchemy-quickstart/sqlalchemy-quickstart.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
import os
22
import dotenv
33

4-
from sqlalchemy import Column, Integer, create_engine, Text
4+
from sqlalchemy import Column, Integer, create_engine, Text, URL
55
from sqlalchemy.orm import declarative_base, Session
6-
from tidb_vector.sqlalchemy import VectorType
6+
from tidb_vector.sqlalchemy import VectorType, VectorAdaptor
7+
from tidb_vector.constants import DistanceMetric
78

89
dotenv.load_dotenv()
910

1011
# Step 1: Connect to TiDB using SQLAlchemy.
11-
tidb_connection_string = os.environ['TIDB_DATABASE_URL']
12-
engine = create_engine(tidb_connection_string)
12+
13+
# Using `pymysql` as the driver.
14+
drivername = 'mysql+pymysql'
15+
ssl_kwargs = {
16+
'ssl_verify_cert': 'true',
17+
'ssl_verify_identity': 'true',
18+
}
19+
20+
# Using `mysqlclient` as the driver.
21+
# drivername = 'mysql+mysqldb'
22+
# ssl_kwargs = {
23+
# 'ssl_mode': 'VERIFY_IDENTITY',
24+
# 'ssl': {
25+
# # Root certificate default path
26+
# # https://docs.pingcap.com/tidbcloud/secure-connections-to-serverless-clusters/#root-certificate-default-path
27+
# 'ca': os.environ.get('TIDB_CA_PATH', '/path/to/ca.pem'),
28+
# },
29+
# }
30+
31+
engine = create_engine(URL.create(
32+
drivername=drivername,
33+
username=os.environ['TIDB_USERNAME'],
34+
password=os.environ['TIDB_PASSWORD'],
35+
host=os.environ['TIDB_HOST'],
36+
port=os.environ['TIDB_PORT'],
37+
database=os.environ['TIDB_DATABASE'],
38+
query=ssl_kwargs if os.environ.get('TIDB_SSL', 'false').lower() == 'true' else {},
39+
))
40+
1341

1442
# Step 2: Define a table with a vector column.
1543
Base = declarative_base()
@@ -27,11 +55,16 @@ class DocumentWithIndex(Base):
2755
__tablename__ = 'sqlalchemy_demo_documents_with_index'
2856
id = Column(Integer, primary_key=True)
2957
content = Column(Text)
30-
embedding = Column(VectorType(3), comment="hnsw(distance=cosine)")
58+
embedding = Column(VectorType(3))
3159

3260

3361
Base.metadata.drop_all(engine)
3462
Base.metadata.create_all(engine)
63+
VectorAdaptor(engine).create_vector_index(
64+
DocumentWithIndex.embedding,
65+
DistanceMetric.COSINE,
66+
skip_existing=True,
67+
)
3568

3669

3770
# Step 3: Insert embeddings into the table.

examples/semantic-cache/cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Cache(SQLModel, table=True):
5757
sa_column=Column(
5858
VectorType(768),
5959
default=None,
60-
comment="hnsw(distance=l2)",
6160
nullable=False,
6261
)
6362
)

0 commit comments

Comments
 (0)