Skip to content

Instantly share code, notes, and snippets.

@ntodd
Last active February 6, 2024 16:10
Show Gist options
  • Save ntodd/bf241a978832fb7e32f19647431ad787 to your computer and use it in GitHub Desktop.
Save ntodd/bf241a978832fb7e32f19647431ad787 to your computer and use it in GitHub Desktop.
Generate embeddings with nomic-embed-text-v1 using Elixir Bumblebee

nomic-embed-text-v1 Embeddings

Mix.install([
  {:bumblebee, "~> 0.4.2"},
  {:nx, "~> 0.6.1"},
  {:exla, "~> 0.6.1"}
])

Nx.global_default_backend({EXLA.Backend, client: :host})

Section

{:ok, model_info} =
  Bumblebee.load_model({:hf, "nomic-ai/nomic-embed-text-v1"},
    architecture: :base,
    module: Bumblebee.Text.Bert
  )

{:ok, tokenizer} =
  Bumblebee.load_tokenizer({:hf, "nomic-ai/nomic-embed-text-v1"},
    module: Bumblebee.Text.BertTokenizer
  )
text = """
George Washington (February 22, 1732 – December 14, 1799) was an American Founding Father, military officer, politician and statesman who served as the first president of the United States from 1789 to 1797.
Appointed by the Second Continental Congress as commander of the Continental Army in 1775, Washington led Patriot forces to victory in the American Revolutionary War and then served as president of the Constitutional Convention in 1787, which drafted and ratified the Constitution of the United States and established the U.S. federal government.
Washington has thus been known as the "Father of the Nation".
"""

serving = Bumblebee.Text.text_embedding(model_info, tokenizer)

Nx.Serving.run(serving, text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment