Ash FrameworkAF
Ash Framework8mo ago
2 replies
Straffern

Bind variables in queries

Had Gemini 2.5 Pro rewrite an adaptation of the ash_ai semantic_search usage rule example.
prepare before_action(fn query, context ->
  config = get_search_config(query.arguments)

  with {:ok, [search_vector]} <-
         config.embedding_module.generate([query.arguments.query], []) do
    query
    |> Ash.Query.let(
      distance: vector_cosine_distance(full_text_vector, ^search_vector)
    )
    |> Ash.Query.filter(distance < ^config.threshold)
    |> Ash.Query.sort(asc: :distance)
  else
    {:error, reason} ->
      {:error, reason}

    _ ->
      # This handles cases where the embedding service returns an unexpected format
      {:error, "Failed to generate a valid search vector."}
  end
end)

I quickly realized that Ash.Query.let does not exist, but is there anything that achieves the same?
I kinda dig it.
Was this page helpful?