N
Neon2y ago
national-gold

Error passing query vector into Neon db

Sorry for a dumb question. It's my first time using PG / Neon so I apologize in advance. I'm building a Next.js web app which allows semantic search of my PG database. I've successfully set up embeddings, which are type "vector" in Neon (using extension pgvector). However, when I enter a query, something breaks once I send my SQL query from Javascript to PG. I know this because I can successfully hardcode my SQL query in Neon Here's my code: if (searchQuery) { let searchEmbedding = await generate_embeddings(searchQuery); let indexEmbedding = params.length + 1; baseQuery = SELECT * FROM db ORDER BY embedding <-> $${indexEmbedding}::vector LIMIT 15; params.push(searchEmbedding); console.log("Updated SQL query:", baseQuery, "Params:", params) } The console log then returns this error: error: 'malformed vector literal: "{"0.019361679","0.02858…65","0.017489731","0.0062273005","-0.013786826"}" Here's another console log from my terminal: Updated SQL query: SELECT * FROM portcos_test ORDER BY embedding <-> $1::vector LIMIT 15 Params: [ [ 0.019361679, 0.028584778, 0.027478006, -0.01475696, -0.007323825, -0.06602373, 0.02546942, ... etc.. ] ] I know the numbers aren't supposed to be strings. But I've been struggling to correctly cast them. Would really appreciate any help!
3 Replies
rare-sapphire
rare-sapphire2y ago
calling the strings with parseFloat(yourString) should give u the correct float
national-gold
national-goldOP2y ago
Thanks for the help @Flo. Do you mind clarifying what my string is in this context? Would it be the search embedding? Also, for future reference: is this somewhere in the docs? I couldn’t find it earlier, but I’m sure I missed it
rare-sapphire
rare-sapphire2y ago
your searchEmbedding array has to return all values with parseFloat so they are not strings anymore so in your generate_embeddings you would do somewhere do something like
const myReturnArray = results.map((r) => parseFloat(r))
const myReturnArray = results.map((r) => parseFloat(r))
or somewhat else parse the strings to floats

Did you find this page helpful?