KyselyK
Kysely3y ago
9 replies
elitan

How to order by with embeddings using pgvector?

I'm trying to get this to work:

  const raw = db
    .selectFrom("embeddingChunks")
    .select(["id", "domainPathId"])
    .orderBy(sql`embedding::VECTOR <=> '[${embedding.toString()}]'`)
    .limit(5);

  const c = raw.compile();

  console.log({ c });

  const res = await raw.execute();


But I get this when executing:

error: invalid input syntax for type vector: "[$1]"
    ....
  length: 102,
  severity: 'ERROR',
  code: '22P02',
  detail: undefined,
  hint: undefined,
  position: '86',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'vector.c',
  line: '224',
  routine: 'vector_in'
}


When I run the compiled query manually in TablePlus it works.

Any hints?
Solution
You can't have parameters inside strings. https://kyse.link/?p=s&i=mSBxXrsodd1aLwlEKulE
Was this page helpful?