Skip to content
conneroisu edited this page Jul 31, 2024 · 2 revisions

semanticrouter

import "github.com/conneroisu/go-semantic-router"

Package semanticrouter is a package for abstractly computing similarity scores between a query vector embedding and a set of vector embeddings.

It provides a simple key-value store for embeddings and a router that can be used to find the best route for a given utterance.

The router uses a similarity score to determine the best route for a given utterance.

The semantic router is designed to be used in conjunction with LLMs and agents to provide a superfast decision-making layer.

Index

func EuclideanDistance(xq, index *mat.VecDense) float64

EuclideanDistance calculates the Euclidean distance between two vectors.

The euclidean distance is the square root of the sum of the squared differences between corresponding elements in the two vectors.

The function takes two vectors as input and returns the Euclidean distance between them.

func HammingDistance(xq, index *mat.VecDense) float64

HammingDistance calculates the Hamming distance between two vectors.

The Hamming distance is the number of positions at which the corresponding bits are different.

The function takes two vectors as input and returns the Hamming distance between them.

func JaccardSimilarity(xq, index *mat.VecDense) float64

JaccardSimilarity calculates the Jaccard similarity between two vectors.

The Jaccard similarity is the size of the intersection of the two sets divided by the size of the union of the two sets.

The function takes two vectors as input and returns the Jaccard distance between them.

func ManhattanDistance(xq, index *mat.VecDense) float64

ManhattanDistance calculates the Manhattan distance between two vectors.

The manhattan distance is the sum of the absolute differences between corresponding elements in the two vectors.

The function takes two vectors as input and returns the Manhattan distance between them.

func MinkowskiDistance(xq, index *mat.VecDense, p float64) float64

MinkowskiDistance calculates the Minkowski distance between two vectors.

func NormalizeScores(sim []float64) []float64

NormalizeScores normalizes the similarity scores to a 0-1 range. The function takes a slice of float64 values representing the similarity scores.

The function takes a slice of float64 values representing the similarity scores and returns a slice of float64 values representing the normalized similarity scores.

func PearsonCorrelation(xq, index *mat.VecDense) float64

PearsonCorrelation calculates the Pearson correlation between two vectors.

The Pearson correlation is a measure of the linear relationship between two variables. It ranges from -1 to 1, where -1 indicates a perfect negative correlation, 0 indicates no correlation, and 1 indicates a perfect positive correlation.

The function takes two vectors as input and returns the Pearson correlation between them.

func SimilarityDotMatrix(xq, index *mat.VecDense) float64

SimilarityDotMatrix computes the similarity scores between a query vector and a set of vectors.

The similarity score is the dot product of the query vector and the index vector divided by the product of the query vector norm and the index vector norm.

The similarity matrix returned is a matrix where each element is the similarity score between the query vector and the corresponding index vector.

type Encoder

Encoder represents a encoding driver in the semantic router.

It is an interface that defines a single method, Encode, which takes a string and returns a []float64 representing the embedding of the string.

type Encoder interface {
    Encode(ctx context.Context, utterance string) ([]float64, error)
}

ErrEncoding is an error that is returned when an error occurs during encoding.

type ErrEncoding struct {
    Message string
}

func (ErrEncoding) Error

func (e ErrEncoding) Error() string

Error returns the error message.

ErrGetEmbedding is an error that is returned when an error occurs during getting an embedding.

type ErrGetEmbedding struct {
    Message string
    Storage Store
}

func (ErrGetEmbedding) Error

func (e ErrGetEmbedding) Error() string

Error returns the error message.

ErrNoRouteFound is an error that is returned when no route is found.

type ErrNoRouteFound struct {
    Message   string
    Utterance string
}

func (ErrNoRouteFound) Error

func (e ErrNoRouteFound) Error() string

Error returns the error message.

type Route

Route represents a route in the semantic router.

It is a struct that contains a name and a slice of Utterances.

type Route struct {
    Name       string             `json:"name"       yaml:"name"       toml:"name"`       // Name is the name of the route.
    Utterances []domain.Utterance `json:"utterances" yaml:"utterances" toml:"utterances"` // Utterances is a slice of Utterances.
}

type Router

Router represents a semantic router.

Router is a struct that contains a slice of Routes and an Encoder.

Match can be called on a Router to find the best route for a given utterance.

type Router struct {
    Routes  []Route `json:"routes"  yaml:"routes"  toml:"routes"`  // Routes is a slice of Routes.
    Encoder Encoder `json:"encoder" yaml:"encoder" toml:"encoder"` // Encoder is an Encoder that encodes utterances into vectors.
    Storage Store   `json:"storage" yaml:"storage" toml:"storage"` // Storage is a Store that stores the utterances.
}

func NewRouter

func NewRouter(routes []Route, encoder Encoder, store Store) (router *Router, err error)

NewRouter creates a new semantic router.

func (*Router) Match

func (r *Router) Match(ctx context.Context, utterance string) (bestRouteName string, bestScore float64, err error)

Match returns the route that matches the given utterance.

The score is the similarity score between the query vector and the index vector.

If the given context is canceled, the context's error is returned if it is non-nil.

type Store

Store is an interface that defines a method, Store, which takes a []float64 and stores it in a some sort of data store, and a method, Get, which takes a string and returns a []float64 from the data store.

type Store interface {
    Store(ctx context.Context, keyValPair domain.Utterance) error
    Get(ctx context.Context, key string) ([]float64, error)
}

domain

import "github.com/conneroisu/go-semantic-router/domain"

Package domain provides domain-specific types and functions.

Index

type Embedding

Embedding is the embedding of some text, speech, or other data (images, videos, etc.).

type Embedding []float64

type Utterance

Utterance represents a utterance in the semantic router.

type Utterance struct {
    *bun.BaseModel `          bun:"table:utterances"`
    // ID is the ID of the utterance.
    ID  int `bun:"id,pk,autoincrement"`
    // Utterance is the utterance.
    Utterance string `bun:"utterance"`
    // EmbeddingBytes is the embedding of the utterance.
    EmbeddingBytes []byte `bun:"embedding"           json:"embedding"`
    // Embed is the Embed of the utterance.
    Embed Embedding
}

func (*Utterance) Embedding

func (u *Utterance) Embedding() (Embedding, error)

Embedding returns the embedding of the utterance.

func (*Utterance) SetEmbedding

func (u *Utterance) SetEmbedding(embedding []float64) error

SetEmbedding sets the embedding of the utterance.

UtterancePrime represents a utterance in the semantic router.

type UtterancePrime struct {
    Embedding []float64 `json:"embedding"` // Embedding is the embedding of the utterance.
}

encoders

import "github.com/conneroisu/go-semantic-router/encoders/google"

Package encoders provides encoders for Google language models.

Google language models are language models that is trained on a large corpus of text data.

Index

GoogleEncoder encodes a query string into a Google search URL.

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

func NewGoogleEncoder(client genai.Client) *GoogleEncoder

NewGoogleEncoder creates a new GoogleEncoder.

func (*GoogleEncoder) Encode

func (e *GoogleEncoder) Encode(ctx context.Context, query string) ([]float64, error)

Encode encodes a query string into a Google search URL.

ollama

import "github.com/conneroisu/go-semantic-router/encoders/ollama"

Package ollama provides an encoder using Ollama models.

Ollama is the easiest way to get started with LLMs.

Index

type Encoder

Encoder is an encoder using Ollama models.

type Encoder struct {
    Client *api.Client
    Model  string
}

func NewEncoder(client *api.Client, model string) *Encoder

NewEncoder creates a new Encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(ctx context.Context, query string) (result []float64, err error)

Encode encodes a query string into a Ollama embedding.

openai

import "github.com/conneroisu/go-semantic-router/encoders/openai"

Package openai provides encoders for OpenAI embedding models.

Index

type Encoder

Encoder encodes a query string into an OpenAI embedding.

type Encoder struct {
    // Client is the OpenAI client.
    Client *openai.Client
    // Model is the OpenAI embedding model to use.
    Model openai.EmbeddingModel
}

func (Encoder) Encode

func (o Encoder) Encode(ctx context.Context, utterance string) ([]float64, error)

Encode encodes the given utterance using the OpenAI API.

voyageai

import "github.com/conneroisu/go-semantic-router/encoders/voyageai"

Package voyageai provides encoders for VoyageAI language models.

Index

type Encoder

Encoder is an encoder using VoyageAI embedding models.

type Encoder struct {
    Client *voyageai.Client
    Model  string
}

func NewEncoder(client *voyageai.Client, model string) *Encoder

NewEncoder creates a new Encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(ctx context.Context, query string) (result []float64, err error)

Encode encodes a query string into a VoyageAI embedding.

chit-chat

import "github.com/conneroisu/go-semantic-router/examples/chit-chat"

Package main shows how to use the semantic router to find the best route for a given utterance in the context of a chat bot or other conversational application.

Index

Variables

ChitchatRoutes represents a set of routes that are noteworthy.

var ChitchatRoutes = semantic_router.Route{
    Name: "chitchat",
    Utterances: []domain.Utterance{
        {Utterance: "how's the weather today?"},
        {Utterance: "how are things going?"},
        {Utterance: "lovely weather today"},
        {Utterance: "the weather is horrendous"},
        {Utterance: "let's go to the chippy"},
    },
}

PoliticsRoutes represents a set of routes that are noteworthy.

var PoliticsRoutes = semantic_router.Route{
    Name: "politics",
    Utterances: []domain.Utterance{
        {Utterance: "isn't politics the best thing ever"},
        {Utterance: "why don't you tell me about your political opinions"},
        {Utterance: "don't you just love the president"},
        {Utterance: "they're going to destroy this country!"},
        {Utterance: "they will save the country!"},
    },
}

veterinarian

import "github.com/conneroisu/go-semantic-router/examples/veterinarian"

Package main shows how to use the semantic router to find the best route for a given utterance in the context of a veterinarian appointment.

Index

Variables

ChitchatRoutes represents a set of routes that are chitchat. chitchat here means that the routes are likely to be relevant to a chitchat conversation in a veterinarian appointment.

var ChitchatRoutes = semantic_router.Route{
    Name: "chitchat",
    Utterances: []domain.Utterance{
        {Utterance: "what is your favorite color?"},
        {Utterance: "what is your favorite animal?"},
    },
}

NoteworthyRoutes represents a set of routes that are noteworthy. noteworthy here means that the routes are likely to be relevant to a noteworthy conversation in a veterinarian appointment.

var NoteworthyRoutes = semantic_router.Route{
    Name: "noteworthy",
    Utterances: []domain.Utterance{
        {Utterance: "what is the best way to treat a dog with a cold?"},
        {Utterance: "my cat has been limping, what should I do?"},
    },
}

memory

import "github.com/conneroisu/go-semantic-router/stores/memory"

Package memory provides a simple in memory key-value store for embeddings.

Index

type Store

Store is a simple key-value store for embeddings.

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

func NewStore

func NewStore() *Store

NewStore creates a new Store from a redis client.

func (Store) Get

func (s Store) Get(_ context.Context, utterance string) (embedding []float64, err error)

Get gets a value from the

func (Store) Store

func (s Store) Store(_ context.Context, utterance domain.Utterance) error

Store sets a value in the store

valkey

import "github.com/conneroisu/go-semantic-router/stores/valkey"

Package valkey provides a simple key-value store for embeddings.

Index

type Store

Store is a simple key-value store for embeddings.

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

func NewStore

func NewStore(rds *redis.Client) *Store

NewStore creates a new Store from a redis client.

func (*Store) Get

func (s *Store) Get(ctx context.Context, utterance string) (embedding []float64, err error)

Get gets a value from the

func (*Store) Set

func (s *Store) Set(ctx context.Context, utterance string, value []float64) (string, error)

Set sets a value in the store

Generated by gomarkdoc