Following the guide in AshAi

I get this error when I am trying to use the cosine distance between the vector and the embedded question, I have the same code as the guide
function: :vector_cosine_distance, arity: nil, resource: Taleed.Poetry.Verse, could_be_calculation?: false, splode: Ash.Error, bread_crumbs: [\"Error returned from: Taleed.Poetry.Verse.vector_search\"], vars: [], path: [:filter], stacktrace: #Splode.Stacktrace<>, class: :invalid}" while retrieving Exception.message/1 for %Ash.Error.Query.NoSuchFunction{function: :vector_cosine_distance, arity: nil, resource: Taleed.Poetry.Verse, could_be_calculation?: false, splode: Ash.Error
function: :vector_cosine_distance, arity: nil, resource: Taleed.Poetry.Verse, could_be_calculation?: false, splode: Ash.Error, bread_crumbs: [\"Error returned from: Taleed.Poetry.Verse.vector_search\"], vars: [], path: [:filter], stacktrace: #Splode.Stacktrace<>, class: :invalid}" while retrieving Exception.message/1 for %Ash.Error.Query.NoSuchFunction{function: :vector_cosine_distance, arity: nil, resource: Taleed.Poetry.Verse, could_be_calculation?: false, splode: Ash.Error
36 Replies
ZachDaniel
ZachDanielβ€’2mo ago
Add "vector" extension and follow these steps: https://hexdocs.pm/ash_postgres/AshPostgres.Extensions.Vector.html If you get a chance, a PR to enhance that guide would be great.
Abu kumathra
Abu kumathraOPβ€’2mo ago
I did that
ZachDaniel
ZachDanielβ€’2mo ago
πŸ€”
Abu kumathra
Abu kumathraOPβ€’2mo ago
I added the pgvector extension
ZachDaniel
ZachDanielβ€’2mo ago
can I see how you're calling it?
Abu kumathra
Abu kumathraOPβ€’2mo ago
read :vector_search do
argument :query, :string, allow_nil?: false

prepare before_action(fn query, context ->
case Taleed.OpenAIEmbeddingModel.generate([query.arguments.query], []) do
{:ok, [search_vector]} ->
Ash.Query.filter(
query,
vector_cosine_distance(full_text_vector, ^search_vector) < 0.5
)
|> Ash.Query.sort(
{calc(vector_cosine_distance(full_text_vector, ^search_vector),
type: :float
), :asc}
)
|> Ash.Query.limit(10)

{:error, error} ->
{:error, error}
end
end)
end
read :vector_search do
argument :query, :string, allow_nil?: false

prepare before_action(fn query, context ->
case Taleed.OpenAIEmbeddingModel.generate([query.arguments.query], []) do
{:ok, [search_vector]} ->
Ash.Query.filter(
query,
vector_cosine_distance(full_text_vector, ^search_vector) < 0.5
)
|> Ash.Query.sort(
{calc(vector_cosine_distance(full_text_vector, ^search_vector),
type: :float
), :asc}
)
|> Ash.Query.limit(10)

{:error, error} ->
{:error, error}
end
end)
end
ZachDaniel
ZachDanielβ€’2mo ago
πŸ€” does that resource have AshPostgres.DataLayer as its data layer?
Abu kumathra
Abu kumathraOPβ€’2mo ago
yes and I have the AshAi, AshOban extensions
ZachDaniel
ZachDanielβ€’2mo ago
That is very very strange...
Abu kumathra
Abu kumathraOPβ€’2mo ago
yes it is the thing is that I copy pasted it from the guide
ZachDaniel
ZachDanielβ€’2mo ago
πŸ€” right. What if you comment out the sort/filter
Abu kumathra
Abu kumathraOPβ€’2mo ago
I created the index and used the vectorize block
ZachDaniel
ZachDanielβ€’2mo ago
is it just one of them breaking it?
Abu kumathra
Abu kumathraOPβ€’2mo ago
tried it with one of them off and it still failed I commented the filter/sort same thing
ZachDaniel
ZachDanielβ€’2mo ago
wait with both commented out you still get that error? or just with one or the other
Abu kumathra
Abu kumathraOPβ€’2mo ago
no only when there is one of them from the message it says it can't find the vector_cosine_distance
ZachDaniel
ZachDanielβ€’2mo ago
Is this something you can push up somewhere we can check out?
Abu kumathra
Abu kumathraOPβ€’2mo ago
I don't think I will be able to but I will try to check if I can get it to work using PSQL
ZachDaniel
ZachDanielβ€’2mo ago
oh, wiat lemme see your repo
Abu kumathra
Abu kumathraOPβ€’2mo ago
defmodule Taleed.Repo do
use AshPostgres.Repo,
otp_app: :taleed

@impl true
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions", "citext", "pgvector"]
end

# Don't open unnecessary transactions
# will default to `false` in 4.0
@impl true
def prefer_transaction? do
false
end

@impl true
def min_pg_version do
%Version{major: 17, minor: 5, patch: 0}
end
end
defmodule Taleed.Repo do
use AshPostgres.Repo,
otp_app: :taleed

@impl true
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions", "citext", "pgvector"]
end

# Don't open unnecessary transactions
# will default to `false` in 4.0
@impl true
def prefer_transaction? do
false
end

@impl true
def min_pg_version do
%Version{major: 17, minor: 5, patch: 0}
end
end
I just added pgvector rn and recompiling now it works still no vectors match but no errors I get a not found so that's fixed thanks a lot. it's been a roller coaster (first time using it) using the library but the support has been great @Zach Daniel now there is a weird issue if I add pgvector to the extension it will force me to migrate and this won't work the user doesn't have super user privileges, and if I remove it I can't search do I need to install the elixir pgvector package
ZachDaniel
ZachDanielβ€’2mo ago
It's just called "vector" Not "pgvector" Does our guide say "pgvector"?
Abu kumathra
Abu kumathraOPβ€’2mo ago
the guide doesn't mention anything ok so I changed it to vector and created the postgres_types.ex file as per the steps mentioned here, the issue is that there is no mention that this is required, also I installed AshAi chat using igniter
ZachDaniel
ZachDanielβ€’2mo ago
Gotcha, can you please open an issue or PR describing what was missing? This will fix it for the next folks πŸ™‡β€β™‚οΈ
Abu kumathra
Abu kumathraOPβ€’2mo ago
I will do that, I will be traveling this weekend, I will do it in monday one last question does ashai create a column to mark whether the vector contains stale embedding
Abu kumathra
Abu kumathraOPβ€’2mo ago
because it's been 2 hours and 300k embedding tasks, and the vectors are still being embeded
No description
Abu kumathra
Abu kumathraOPβ€’2mo ago
the issue is that the vectors were embedded, I am afraid to look at my openai bill
ZachDaniel
ZachDanielβ€’2mo ago
πŸ˜…it should be scheduling embeddings whenever the values change You should of course trial these things before running them on real data...
Abu kumathra
Abu kumathraOPβ€’2mo ago
I know it shouldn't, anyway thank you great library. I will write a PR+Issue on monday for the guide. Is there something in the roadmap about including real vector dbs currently postgres is complaining about the vector size if I use open ai's text-embedding-3-large which is 3072 Also Can a resource have multiple vectorize statements? I tried but failed miserably. I will probably keep a list of items that I missed from the guide so new comers won't fall into them
ZachDaniel
ZachDanielβ€’2mo ago
only one vectorize statement but you can vectorize multiple attributes vectorize foo: :bar, baz: :buz IIRC for doing it without pgvector everything gets more complicated but also: you can definitely do it with a vector size that big I would always prefer to use pgvector if possible before reaching for a "real" vector db
Abu kumathra
Abu kumathraOPβ€’2mo ago
can you expand on this
ZachDaniel
ZachDanielβ€’2mo ago
Sorry, its like this:
vectorize do
attributes name: :vectorized_name, biography: :vectorized_biography

...
end
vectorize do
attributes name: :vectorized_name, biography: :vectorized_biography

...
end
so you can vectorize multiple attrs Maybe I didn't understand what you wanted though
Abu kumathra
Abu kumathraOPβ€’2mo ago
how do I define the text that will be vectorized for each one
ZachDaniel
ZachDanielβ€’2mo ago
πŸ€” what do you mean? That takes the name attribute, and vectorizes it as vectorized_name so you'd just set the name attribute
Abu kumathra
Abu kumathraOPβ€’2mo ago
oh that will do it actually I thought that I need to define full_text function for each attribute so I can still use the full_text one if I want to concat multiple values but I can only have one full_text function
ZachDaniel
ZachDanielβ€’2mo ago
Ah, yeah currently
Abu kumathra
Abu kumathraOPβ€’2mo ago
ok thank you

Did you find this page helpful?