Skip to content

Commit

Permalink
really fast search
Browse files Browse the repository at this point in the history
  • Loading branch information
cbh123 committed Sep 27, 2023
1 parent 280dd09 commit 43a2867
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/emoji/embeddings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Emoji.Embeddings do
@doc """
Creates an embedding and returns it in binary form.
"""
require Logger

def create("", _), do: nil

def create(text, embeddings_model) do
Expand Down Expand Up @@ -41,16 +43,29 @@ defmodule Emoji.Embeddings do
"data:#{mime_type};base64,#{base64}"
end

def search_emojis(query, num_results \\ 9, via_images \\ false) do
embedding_binary =
create(
query,
"daanelson/imagebind:0383f62e173dc821ec52663ed22a076d9c970549c209666ac3db181618b7a304"
)
defp find_or_create_embedding(search_word) do
case Emoji.Search.get_query_by_content(search_word) do
nil ->
Logger.info("Creating embedding for #{search_word}")

embedding = Nx.from_binary(embedding_binary, :f32)
embedding =
create(
search_word,
"daanelson/imagebind:0383f62e173dc821ec52663ed22a076d9c970549c209666ac3db181618b7a304"
)

{:ok, _query} = Emoji.Search.create_query(%{content: search_word, embedding: embedding})
embedding

{:ok, _query} = Emoji.Search.create_query(%{content: query, embedding: embedding_binary})
%Emoji.Search.Query{} = query ->
Logger.info("Embedding already found for #{query.content}")
query.embedding
end
end

def search_emojis(query, num_results \\ 9, via_images \\ false) do
embedding_binary = find_or_create_embedding(query)
embedding = Nx.from_binary(embedding_binary, :f32)

%{labels: labels, distances: distances} =
if via_images do
Expand Down
12 changes: 12 additions & 0 deletions lib/emoji/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ defmodule Emoji.Search do
"""
def get_query!(id), do: Repo.get!(Query, id)

@doc """
Gets a single query by content.
"""
def get_query_by_content(content) do
from(q in Query,
where: ilike(q.content, ^content),
select: q,
limit: 1
)
|> Repo.one()
end

@doc """
Creates a query.
Expand Down

0 comments on commit 43a2867

Please sign in to comment.