{"id":18652942,"url":"https://github.com/elixir-nx/hnswlib","last_synced_at":"2026-03-12T04:32:42.490Z","repository":{"id":159282716,"uuid":"634392722","full_name":"elixir-nx/hnswlib","owner":"elixir-nx","description":"Elixir binding for the hnswlib library.","archived":false,"fork":false,"pushed_at":"2026-01-12T15:58:10.000Z","size":197,"stargazers_count":71,"open_issues_count":0,"forks_count":13,"subscribers_count":5,"default_branch":"main","last_synced_at":"2026-01-12T20:49:46.779Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elixir-nx.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-04-30T00:56:50.000Z","updated_at":"2026-01-12T14:02:16.000Z","dependencies_parsed_at":"2026-01-12T17:04:25.449Z","dependency_job_id":null,"html_url":"https://github.com/elixir-nx/hnswlib","commit_stats":{"total_commits":116,"total_committers":6,"mean_commits":"19.333333333333332","dds":0.1293103448275862,"last_synced_commit":"a407cdb05301c910126161f104e84a0130cf8e87"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/elixir-nx/hnswlib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixir-nx%2Fhnswlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixir-nx%2Fhnswlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixir-nx%2Fhnswlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixir-nx%2Fhnswlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elixir-nx","download_url":"https://codeload.github.com/elixir-nx/hnswlib/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixir-nx%2Fhnswlib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30415541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T04:25:42.844Z","status":"ssl_error","status_checked_at":"2026-03-12T04:25:34.624Z","response_time":114,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T07:09:31.354Z","updated_at":"2026-03-12T04:32:42.450Z","avatar_url":"https://github.com/elixir-nx.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HNSWLib\n\nElixir binding for the [hnswlib](https://github.com/nmslib/hnswlib) library.\n\nCurrently in development, alpha software.\n\n## Usage\n\n### Create an Index\n\n```elixir\n# working in L2-space\n# other possible values are\n#  `:ip` (inner product)\n#  `:cosine` (cosine similarity)\niex\u003e space = :l2\n:l2\n# each vector is a 2D-vec\niex\u003e dim = 2\n2\n# limit the maximum elements to 200\niex\u003e max_elements = 200\n200\n# create Index\niex\u003e {:ok, index} = HNSWLib.Index.new(space, dim, max_elements)\n{:ok,\n %HNSWLib.Index{\n   space: :l2,\n   dim: 2,\n   reference: #Reference\u003c0.2548668725.3381002243.154990\u003e\n }}\n```\n\n### Add vectors to the Index\n\n```elixir\niex\u003e data =\n  Nx.tensor(\n    [\n      [42, 42],\n      [43, 43],\n      [0, 0],\n      [200, 200],\n      [200, 220]\n    ],\n    type: :f32\n  )\n#Nx.Tensor\u003c\n  f32[5][2]\n  [\n    [42.0, 42.0],\n    [43.0, 43.0],\n    [0.0, 0.0],\n    [200.0, 200.0],\n    [200.0, 220.0]\n  ]\n\u003e\niex\u003e HNSWLib.Index.get_current_count(index)\n{:ok, 0}\niex\u003e HNSWLib.Index.add_items(index, data)\n:ok\niex\u003e HNSWLib.Index.get_current_count(index)\n{:ok, 5}\n```\n\n### Query nearest vector(s) in the index\n\n```elixir\n# query\niex\u003e query = Nx.tensor([1, 2], type: :f32)\n#Nx.Tensor\u003c\n  f32[2]\n  [1.0, 2.0]\n\u003e\niex\u003e {:ok, labels, dists} = HNSWLib.Index.knn_query(index, query)\n{:ok,\n #Nx.Tensor\u003c\n   u64[1][1]\n   [\n     [2]\n   ]\n \u003e,\n #Nx.Tensor\u003c\n   f32[1][1]\n   [\n     [5.0]\n   ]\n \u003e}\n\niex\u003e {:ok, labels, dists} = HNSWLib.Index.knn_query(index, query, k: 3)\n{:ok,\n #Nx.Tensor\u003c\n   u64[1][3]\n   [\n     [2, 0, 1]\n   ]\n \u003e,\n #Nx.Tensor\u003c\n   f32[1][3]\n   [\n     [5.0, 3281.0, 3445.0]\n   ]\n \u003e}\n```\n\n### Save an Index to file\n\n```elixir\niex\u003e HNSWLib.Index.save_index(index, \"my_index.bin\")\n:ok\n```\n\n### Load an Index from file\n\n```elixir\niex\u003e {:ok, saved_index} = HNSWLib.Index.load_index(space, dim, \"my_index.bin\")\n{:ok,\n %HNSWLib.Index{\n   space: :l2,\n   dim: 2,\n   reference: #Reference\u003c0.2105700569.2629697564.236704\u003e\n }}\niex\u003e HNSWLib.Index.get_current_count(saved_index)\n{:ok, 5}\niex\u003e {:ok, data} = HNSWLib.Index.get_items(saved_index, [2, 0, 1])\n{:ok,\n [\n   \u003c\u003c0, 0, 0, 0, 0, 0, 0, 0\u003e\u003e,\n   \u003c\u003c0, 0, 40, 66, 0, 0, 40, 66\u003e\u003e,\n   \u003c\u003c0, 0, 44, 66, 0, 0, 44, 66\u003e\u003e\n ]}\niex\u003e tensors = Nx.stack(Enum.map(data, fn d -\u003e Nx.from_binary(d, :f32) end))\n#Nx.Tensor\u003c\n  f32[3][2]\n  [\n    [0.0, 0.0],\n    [42.0, 42.0],\n    [43.0, 43.0]\n  ]\n\u003e\n```\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `hnswlib` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:hnswlib, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/hnswlib\u003e.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felixir-nx%2Fhnswlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felixir-nx%2Fhnswlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felixir-nx%2Fhnswlib/lists"}