Query vector data with CQL #AstraDB #GenAI

Query vector data with CQL #AstraDB #GenAI

Cassandra Query Language (CQL) uses the?vector?data type to enable vector similarity searches of your data. Here, you’ll create a schema and an index, load vector search data into your database, and use CQL to work with that data.

Create the vector schema and load the data into the database (AstraDB - https://astra.datastax.com/)

  1. Add a keyspace to use for your vector search table. This example uses vsearch as the keyspace name.

USE vsearch;        

2. Use this code to create a new table in your keyspace with a five-component vector column.

CREATE TABLE IF NOT EXISTS vsearch.products (
 id int PRIMARY KEY,
 name TEXT,
 description TEXT,
 item_vector VECTOR<FLOAT, 5> //create a 5-dimensional embedding
);        

3. Create the custom index with Storage Attached Indexing (SAI). Creating the index and then loading the data prevents the concurrent building of the index as data loads.

CREATE CUSTOM INDEX IF NOT EXISTS ann_index
  ON vsearch.products(item_vector) USING 'StorageAttachedIndex'
  WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' };        

Valid values for the similarity_function are COSINE (default), DOT_PRODUCT, or EUCLIDEAN.

4. Insert sample data into the table using the new type.

INSERT INTO vsearch.products (id, name, description, item_vector) VALUES (
1, //id
'Coded Cleats', //name
'ChatGPT integrated sneakers that talk to you', //description
[0.1, 0.15, 0.3, 0.12, 0.05] //item_vector
);
INSERT INTO vsearch.products (id, name, description, item_vector)
VALUES (2, 'Logic Layers',
'An AI quilt to help you sleep forever',
[0.45, 0.09, 0.01, 0.2, 0.11]);
INSERT INTO vsearch.products (id, name, description, item_vector)
VALUES (5, 'Vision Vector Frame',
'A deep learning display that controls your mood',
[0.1, 0.05, 0.08, 0.3, 0.6]);        

5. Query data using vector search, use a SELECT query.

SELECT * FROM vsearch.products
ORDER BY item_vector ANN OF [0.15, 0.1, 0.1, 0.35, 0.55]
LIMIT 1;        

Continue : Use Analyzers CQL AstraDB GenAI

Query vector data with CQL #AstraDB #GenAI Courtesy - DataStax


要查看或添加评论,请登录

Pankaj Gajjar的更多文章

社区洞察

其他会员也浏览了