Skip to content
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

feat: add vector function support #9

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

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.

None yet

2 participants