High-Level SDK¶
This example uses akasicdb.sdk.AkasicDB to create a collection, insert records, and run vector search with metadata filters.
Install¶
Example¶
from akasicdb.sdk import AkasicDB, Record, filters
DB_URL = "postgresql+psycopg://postgres:mysecretpassword@localhost:5432/postgres"
TARGET = [1.0, 2.0, 5.0]
client = AkasicDB(
db_url=DB_URL,
collection_name="example",
dimension=3,
create_vector_index=True,
recreate=True,
)
try:
client.insert(
[
Record.from_text("Hello world", [1.0, 2.0, 3.0], {"src": "one"}),
Record.from_text("Hello PostgreSQL", [1.0, 2.0, 4.0], {"src": "one"}),
Record.from_text("Hello AkasicDB", [1.0, 3.0, 4.0], {"src": "two"}),
]
)
print("Filter helper")
for record, distance in client.search(
TARGET,
top_k=3,
filter=filters.meta_contains({"src": "one"}),
):
print(record.text, distance)
print("Dictionary filter")
for record, distance in client.search(
TARGET,
top_k=3,
filter={"src": {"$eq": "one"}},
):
print(record.text, distance)
finally:
client.drop()
recreate=True recreates the collection table if it already exists. The final client.drop() removes the collection_example table created by the example.