### Run Example Project Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Steps to run an example project, including fetching dependencies, setting up a database, and executing the example script. ```sh cd examples/loading mix deps.get createdb pgvector_example mix run example.exs ``` -------------------------------- ### Postgrex: Start Link with Custom Types Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Start a Postgrex connection, providing the registered pgvector types. ```elixir {:ok, pid} = Postgrex.start_link(types: MyApp.PostgrexTypes) ``` -------------------------------- ### Install pgvector Dependency Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Add the pgvector Elixir library to your project's `mix.exs` file to include it as a dependency. ```elixir {:pgvector, "~> 0.3.0"} ``` -------------------------------- ### Get Sparse Vector Values Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Demonstrates how to retrieve the values of non-zero elements from a sparse vector using Pgvector.SparseVector.values/1. ```elixir values = vec |> Pgvector.SparseVector.values() ``` -------------------------------- ### Get Sparse Vector Indices Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Illustrates how to get the indices of non-zero elements from a sparse vector using Pgvector.SparseVector.indices/1. ```elixir indices = vec |> Pgvector.SparseVector.indices() ``` -------------------------------- ### Get Sparse Vector Dimensions Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Shows how to retrieve the total number of dimensions for a sparse vector using Pgvector.SparseVector.dimensions/1. ```elixir dim = vec |> Pgvector.SparseVector.dimensions() ``` -------------------------------- ### Upgrade Ecto Distance Function (0.3.0) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Provides an example of how to update Ecto distance function calls after upgrading to pgvector-elixir version 0.3.0, requiring explicit conversion of lists to Pgvector structs. ```elixir # before l2_distance(i.embedding, [1, 2, 3]) # after l2_distance(i.embedding, ^Pgvector.new([1, 2, 3])) ``` -------------------------------- ### Clone Repository and Run Tests Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Instructions for cloning the pgvector-elixir repository, fetching dependencies, setting up a database, and running the test suite. ```sh git clone https://github.com/pgvector/pgvector-elixir.git cd pgvector-elixir mix deps.get createdb pgvector_elixir_test mix test ``` -------------------------------- ### Create Sparse Vector from List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Shows how to create a sparse vector from a list of values using Pgvector.SparseVector.new/1. ```elixir vec = Pgvector.SparseVector.new([1, 2, 3]) ``` -------------------------------- ### Ecto: Run Migrations Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Execute pending Ecto migrations to apply schema changes, including the creation of the vector extension. ```sh mix ecto.migrate ``` -------------------------------- ### Ecto: Create Vector Extension Migration Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Generate and apply an Ecto migration to create the 'vector' extension in your PostgreSQL database. ```sh mix ecto.gen.migration create_vector_extension ``` -------------------------------- ### Create Half Vector from List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Demonstrates how to create a half vector from a list of numbers using Pgvector.HalfVector.new/1. ```elixir vec = Pgvector.HalfVector.new([1, 2, 3]) ``` -------------------------------- ### Create Sparse Vector from Map of Elements Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Demonstrates creating a sparse vector from a map of non-zero elements and specifying the total dimensions. ```elixir elements = %{0 => 1.0, 2 => 2.0, 4 => 3.0} vec = Pgvector.SparseVector.new(elements, 6) ``` -------------------------------- ### Ecto: Migration to Create Vector Extension Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Defines the Ecto migration to create the 'vector' extension in PostgreSQL. ```elixir defmodule MyApp.Repo.Migrations.CreateVectorExtension do use Ecto.Migration def up do execute "CREATE EXTENSION IF NOT EXISTS vector" end def down do execute "DROP EXTENSION vector" end end ``` -------------------------------- ### Create Sparse Vector from Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Illustrates creating a sparse vector from an Nx tensor using Pgvector.SparseVector.new/1. ```elixir vec = Nx.tensor([1.0, 2.0, 3.0]) |> Pgvector.SparseVector.new() ``` -------------------------------- ### Reference: Create Vector from List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create a pgvector vector object from a standard Elixir list. ```elixir vec = Pgvector.new([1, 2, 3]) ``` -------------------------------- ### Convert Sparse Vector to List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Shows how to convert a sparse vector to a list representation using Pgvector.to_list/1. ```elixir list = vec |> Pgvector.to_list() ``` -------------------------------- ### Postgrex: Enable Vector Extension Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Execute a SQL command via Postgrex to enable the 'vector' extension in the database. ```elixir Postgrex.query!(pid, "CREATE EXTENSION IF NOT EXISTS vector", []) ``` -------------------------------- ### Postgrex: Create Table with Vector Column Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create a table with a 'vector' column using raw SQL executed through Postgrex. ```elixir Postgrex.query!(pid, "CREATE TABLE items (embedding vector(3))", []) ``` -------------------------------- ### Convert Half Vector to List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Illustrates how to convert a half vector back to a list using the Pgvector.to_list/1 function. ```elixir list = vec |> Pgvector.to_list() ``` -------------------------------- ### Create Half Vector from Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Shows how to create a half vector from an Nx tensor with f16 type using Pgvector.HalfVector.new/1. ```elixir vec = Nx.tensor([1.0, 2.0, 3.0], type: :f16) |> Pgvector.HalfVector.new() ``` -------------------------------- ### Reference: Create Vector from Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create a pgvector vector object from an Nx tensor. ```elixir vec = Nx.tensor([1.0, 2.0, 3.0]) |> Pgvector.new() ``` -------------------------------- ### Convert Half Vector to Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Demonstrates converting a half vector to an Nx tensor using the Pgvector.to_tensor/1 function. ```elixir tensor = vec |> Pgvector.to_tensor() ``` -------------------------------- ### Convert Sparse Vector to Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Illustrates converting a sparse vector to an Nx tensor using Pgvector.to_tensor/1. ```elixir tensor = vec |> Pgvector.to_tensor() ``` -------------------------------- ### Ecto: Define Schema with Vector Field Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Map the 'vector' database type to the `Pgvector.Ecto.Vector` struct in your Ecto schema. ```elixir schema "items" do field :embedding, Pgvector.Ecto.Vector end ``` -------------------------------- ### Ecto: Add Approximate Nearest Neighbor Index (IVFFlat) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create an IVFFlat index on the vector column for approximate nearest neighbor searches, specifying the number of lists. ```elixir create index("items", ["embedding vector_l2_ops"], using: :ivfflat, options: "lists = 100") ``` -------------------------------- ### Ecto: Configure Repo with Custom Types Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Configure your Ecto repository to use the custom Postgrex types defined for pgvector. ```elixir config :my_app, MyApp.Repo, types: MyApp.PostgrexTypes ``` -------------------------------- ### Reference: Convert Vector to List Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Convert a pgvector vector object back into an Elixir list. ```elixir list = vec |> Pgvector.to_list() ``` -------------------------------- ### Postgrex: Insert Vector Data Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Insert a vector into the 'items' table using a parameterized SQL query via Postgrex. ```elixir Postgrex.query!(pid, "INSERT INTO items (embedding) VALUES ($1)", [[1, 2, 3]]) ``` -------------------------------- ### Postgrex: Add Approximate Nearest Neighbor Index (IVFFlat) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create an IVFFlat index on the vector column using a raw SQL command via Postgrex, specifying the number of lists. ```elixir Postgrex.query!(pid, "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", []) ``` -------------------------------- ### Ecto: Add Approximate Nearest Neighbor Index (HNSW) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create an HNSW (Hierarchical Navigable Small World) index on the vector column for efficient approximate nearest neighbor searches. ```elixir create index("items", ["embedding vector_l2_ops"], using: :hnsw) ``` -------------------------------- ### Postgrex: Query Nearest Neighbors Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Query for nearest neighbors using the '<->' operator for vector distance calculation via Postgrex. ```elixir Postgrex.query!(pid, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", [[1, 2, 3]]) ``` -------------------------------- ### Ecto: Define Table with Vector Type Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create a database table with a column of the 'vector' type, specifying its dimension size. ```elixir create table(:items) do add :embedding, :vector, size: 3 end ``` -------------------------------- ### Ecto: Query Nearest Neighbors (L2 Distance) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Retrieve items from the 'items' table, ordered by their L2 distance to a query vector, limiting the results. ```elixir import Ecto.Query import Pgvector.Ecto.Query Repo.all(from i in Item, order_by: l2_distance(i.embedding, ^Pgvector.new([1, 2, 3])), limit: 5) ``` -------------------------------- ### Postgrex: Add Approximate Nearest Neighbor Index (HNSW) Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Create an HNSW index on the vector column using a raw SQL command executed via Postgrex. ```elixir Postgrex.query!(pid, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", []) ``` -------------------------------- ### Postgrex: Register pgvector Extension Types Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Register the pgvector extension types with Postgrex to enable handling of vector data. ```elixir Postgrex.Types.define(MyApp.PostgrexTypes, Pgvector.extensions(), []) ``` -------------------------------- ### Ecto: Insert Vector Data Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Insert a new record into the 'items' table with a vector embedding. ```elixir alias MyApp.{Repo, Item} Repo.insert(%Item{embedding: [1, 2, 3]}) ``` -------------------------------- ### Postgrex: Convert Vector to List or Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Convert a retrieved vector to an Elixir list or an Nx tensor using the Pgvector module. ```elixir vector |> Pgvector.to_list() vector |> Pgvector.to_tensor() ``` -------------------------------- ### Ecto: Convert Vector to List or Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Convert a vector stored in the Ecto model to a standard Elixir list or an Nx tensor. ```elixir item.embedding |> Pgvector.to_list() item.embedding |> Pgvector.to_tensor() ``` -------------------------------- ### Reference: Convert Vector to Nx Tensor Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Convert a pgvector vector object into an Nx tensor. ```elixir tensor = vec |> Pgvector.to_tensor() ``` -------------------------------- ### Ecto: Define Postgrex Types Source: https://github.com/pgvector/pgvector-elixir/blob/master/README.md Define custom Postgrex types for pgvector in your Elixir application to enable Ecto's integration with the vector extension. ```elixir Postgrex.Types.define(MyApp.PostgrexTypes, Pgvector.extensions() ++ Ecto.Adapters.Postgres.extensions(), []) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.