Skip to content

feat: add vector function support#9

Draft
Mini256 wants to merge 3 commits intotidbcloud:mainfrom
Mini256:feat-support-vector-function
Draft

feat: add vector function support#9
Mini256 wants to merge 3 commits intotidbcloud:mainfrom
Mini256:feat-support-vector-function

Conversation

@Mini256
Copy link

@Mini256 Mini256 commented Feb 23, 2024

Inspired by the pgvector-node package: https://github.com/pgvector/pgvector-node/blob/master/src/kysely/index.js

Simplifies the use of TiDB Serverless Vector Function for kysely developers while providing better code hints.

For example:

db.selectFrom('document_index_chunk')
      .innerJoin('document', 'document_id', 'document.id')
      .select([
        'document_index_chunk.id as document_index_chunk_id',
        'document_id',
        'document_index_chunk.text_content',
        'document_index_chunk.metadata',
        'source_uri',
        'document.name as source_name',
-        eb => eb(eb => eb.lit<number>(1), '-', eb.fn<number>('vec_cosine_distance', [
-          'embedding',
-          eb => eb.val(vectorToVal(vector))],
-        )).as('score'),
+        (eb) => cosineSimilarity(eb, 'embedding', vector).as('score')
      ])
      .where('staled', '=', 0)
      .where('index_name', '=', eb => eb.val(index))
      .orderBy('score desc')
      .limit(top_k);

@634750802
Copy link

Please consider typing safe for column names (not just using string type). It's the major advantage of kysely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants