Ash FrameworkAF
Ash Framework7mo ago
5 replies
marot

How can I return vector_cosine_distance from search results?

I'm currently testing AshAi's embeddings support, and implemented a search based on the docs. I struggle to return the result of vector_cosine_distance(full_text_vector, ^search_vector) for debugging purposes. I tried to use a calculation, but I won't have access to search_vector. In Ecto I'd use a virtual field and use select_merge. What is the correct approach in Ash here?

read :search do
  argument :query, :string, allow_nil?: false

  prepare before_action(fn query, context ->
    case YourEmbeddingModel.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
Solution
You can use calculations with arguments, and load that calculation in this preparation
Was this page helpful?