pgvector / pgvector-php

pgvector support for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid parameter number: parameter was not defined

flexchar opened this issue · comments

commented

Latest versions of Laravel 10 and PHP 8.2.5. Using pgvector docker image for the server.

I'm trying to query pgsql using Laravel ->orderByRaw method and I get:

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (Connection: pgsql, SQL: select * from "data_chunks" order by embedding <-> -0.023646029 limit 5)
$embedding = $azureOpenAi->embed($query);
assert(is_array($embedding));

$relatedData = DataChunk::query()
            ->orderByRaw('embedding <-> ?', [$embedding])
            ->take(5)
            ->ray()
            ->get();

If I log query using ray (equal to dump), it seems that I'm only getting first float out of 1536 existing in the vector.

select
  *
from
  "data_chunks"
order by
  embedding < -> -0.023646029
limit
  5

I tried nesting it into another array but it doesn't seem to take it in. I'm lost and would like some help :)

Hi @flexchar, you'll want to pass a Vector instead of an array.

->orderByRaw('embedding <-> ?', [new Vector($embedding)])
commented

Yes, I've missed that from the docs. Thank you for helping out!

commented

Hi

I'm getting the same error when I use DB , not sure why

use Pgvector\Vector;

           $embedding = new Vector([3,1,2]);
            $documents = DB::table('documents')
            ->whereRaw("collection_id=?", [$collection_id])
            ->orderByRaw("embeds <-> '?'", [$embedding])
            ->limit(5)
            ->get();
  SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (Connection: pgsql, SQL: select * from "documents" where collection_id=1 order by embeds <-> '[3,1,2]' limit 5)

Hi @mrahmadt, make sure the question mark isn't in quotes.

->orderByRaw("embeds <-> ?", [$embedding])
commented

Hi @mrahmadt, make sure the question mark isn't in quotes.

->orderByRaw("embeds <-> ?", [$embedding])

Woundfull, working perfectly now.