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

Fail to query empty index #126

Open
Volland opened this issue Mar 22, 2024 · 0 comments
Open

Fail to query empty index #126

Volland opened this issue Mar 22, 2024 · 0 comments

Comments

@Volland
Copy link

Volland commented Mar 22, 2024

if index is empty i get error on query data

code sample

import { EmbeddingModel, FlagEmbedding } from 'fastembed'
import Database from "better-sqlite3";
import * as sqlite_vss from "sqlite-vss";

const setupDb = (path) => {
    const db = new Database(path);
    sqlite_vss.load(db);

    db.exec(`
     CREATE TABLE IF NOT EXISTS nodes (
        id PRIMARY KEY NOT NULL,
        label VARCHAR
     )
    `)

     db.exec(`select crsql_as_crr('nodes');`)
     console.log('1')
     db.exec(`
    create virtual table node_vector using vss0(
        vectorLabel(768)
      );
    `)
   //  db.exec(`select crsql_as_crr('node_vector');`)
   return db

} 
const main = async () => {
    const embeddingModel = await FlagEmbedding.init({
        model: EmbeddingModel.BGEBaseENV15
    });

    const db1 =  setupDb('1-empty.sqlite')
    // query vectors 

    const query =  'cat drink coffee'
    const queryVector = JSON.stringify(Array.from( await embeddingModel.queryEmbed(query)))


    console.log('before query')

    const q = db1.prepare(' select * from node_vector').all()

    console.log(q)

    console.log('after query')


    const vectorSearchQuery = db1.prepare(
        `with matches as (
            select rowid,
            distance
            from node_vector where vss_search(vectorLabel, @qv)
            limit 10
          )
          select
          nodes.id,
          nodes.label,
          matches.distance
          from matches 
          left join nodes on nodes.rowid = matches.rowid`,
        )
        const result = vectorSearchQuery.all({qv: queryVector})

        console.log('after query 2 ', result)

    db1.exec(`select crsql_finalize();`)
    db1.close()
}

main().catch().then(() => console.log('done'))
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

No branches or pull requests

1 participant