libsqlvector

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: MIT Imports: 9 Imported by: 1

README

libsql-vector-go

This library is intended to provide an easy way to work with libSQL vectors which are used by providers like Turso. This library works in a similar way to pgvector-go, which this code has been adapted from.

Typically vectors in databases (like those provided by pgvector) take the format of an array of floating points, e.g. UPDATE table SET embedding = '[1,2,3]'. However the vectors in libSQL must be wrapped in a vector() function, e.g. UPDATE table SET embedding = vector('[1,2,3]') which means that most other database vector libraries are incompatible.

Getting Started

Get the library:

go get github.com/ryanskidmore/libsql-vector-go

Then import it:

import (
	github.com/ryanskidmore/libsql-vector-go
)

Gorm

If you're using gorm, you want to instead import the gorm variant which is held as a separate package:

import (
    libsqlvectorgorm github.com/ryanskidmore/libsql-vector-go/gorm
)

You can then use the type in your models:

type Product struct {
	Embedding *libsqlvectorgorm.Vector `gorm:"type:F32_BLOB(1536)"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

Vector is a wrapper for []float32 to implement sql.Scanner and driver.Valuer.

func NewVector

func NewVector(vec []float32) Vector

NewVector creates a new Vector from a slice of float32.

func (*Vector) DecodeBinary

func (v *Vector) DecodeBinary(buf []byte) error

DecodeBinary decodes a binary representation of a vector.

func (Vector) EncodeBinary

func (v Vector) EncodeBinary(buf []byte) (newBuf []byte, err error)

EncodeBinary encodes a binary representation of the vector.

func (Vector) FormatFloats

func (v Vector) FormatFloats() string

func (Vector) MarshalJSON

func (v Vector) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Vector) Parse

func (v *Vector) Parse(s string) error

func (*Vector) Scan

func (v *Vector) Scan(src interface{}) (err error)

Scan implements the sql.Scanner interface.

func (Vector) Slice

func (v Vector) Slice() []float32

Slice returns the underlying slice of float32.

func (Vector) String

func (v Vector) String() string

String returns a string representation of the vector

func (*Vector) UnmarshalJSON

func (v *Vector) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Vector) Value

func (v Vector) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Directories

Path Synopsis
gorm module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL