memory

package module
v0.0.0-...-e6ff8ee Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 18 Imported by: 0

README

Disclaimer: This is a work in progress, not ready for production, there will be breaking changes

Memory is a long term memory implementation for golang

Description

It is a simple abstraction that allows you to store and retrieve documents based on their text similarity. It uses a vector store to store the document embeddings and an embedder to generate the embeddings. we use document.Document to represent a document, and we can add metadata if we want to.

We can create a new Memory instance by providing a vector store and an embedder. A local store can be provided but a default sqlite store will be created if not there

It's designed with simple interfaces to be easily extended to other vector stores, embedders and datasources.

Create a new data source:
If not path is given for sqlite or boltdb it will use a default path
package main

import "github.com/aldarisbm/memory/datasource/sqliteds"

func main() {
	// can pass options
	ls := sqliteds.NewLocalStorer()
}
Create a new embedder
package main

import (
	oai "github.com/aldarisbm/memory/embeddings/openai"
	"os"
)

func main() {
	// can pass options such as model, but uses default if not provided
	embedder := oai.NewOpenAIEmbedder(
		oai.WithApiKey(os.Getenv("OPENAI_API_KEY")),
	)
}
Create a new vector store

Here we are using pinecone, but there is heisenberg that can run locally

package main

import (
    "github.com/aldarisbm/memory/vectorstore/pinecone"
    "os"
)

func main() { 
	// can pass options such as project name, index name, environment and api key 
	// it will create a default namespace if not provided
	vs := pinecone.NewStorer(
        pinecone.WithApiKey(os.Getenv("PINECONE_API_KEY")),
        pinecone.WithIndexName(os.Getenv("PINECONE_INDEX_NAME")),
        pinecone.WithProjectName(os.Getenv("PINECONE_PROJECT_NAME")),
        pinecone.WithEnvironment(os.Getenv("PINECONE_ENVIRONMENT")),
    )
}
Example of using Memory

This example will show how to use the default sqlite store, heisenberg, and possibly the local embedder

package main

import (
	"fmt"
	"github.com/aldarisbm/memory"
	"github.com/aldarisbm/memory/embeddings/local"
	"github.com/aldarisbm/memory/vectorstore/heisenberg"
	"os"
	"time"
)

func main() {
	// for this to work you should be running something like
	// https://github.com/aldarisbm/sentence_transformers locally
	emb := local.New(local.WithHost("http://127.0.0.1:5050"))
	// Uses default SQLite for storage
	// And default Heisenberg for vector store
	mem := memory.NewMemory(memory.WithEmbedder(emb))
	defer mem.Close()

	user := "my-user"
	texts := []string{
		"seinfield is the best comedy show in the world",
		"friends is an ok comedy show in the world",
		"the office is a good comedy show",
		"Wednesday is not really a comedy show",
		"Black Mirror is a suspenseful show",
		"If we are talking about suspenseful shows, then Twilight Zone is the best",
	}

	for _, t := range texts {
		doc := mem.NewDocument(t, user)
		if err := mem.StoreDocument(doc); err != nil {
			panic(err)
		}
	}

	q := "what is a good suspenseful show?"
	docs, err := mem.RetrieveSimilarDocumentsByText(q, 3)
	if err != nil {
		panic(err)
	}
	for _, doc := range docs {
		fmt.Println(doc.Text)
	}
	// Output:
	// the office is a good comedy show
	// Black Mirror is a suspenseful show
	// If we are talking about suspenseful shows, then Twilight Zone is the best

	// the last one being the closest to the query
}

Documentation

Index

Constants

View Source
const (
	BoltDB     = "memoryinternal"
	BucketName = "memories"
)
View Source
const CacheSize = 30

CacheSize is the size of the cache for saving recent documents

Variables

View Source
var (
	ErrInvalidVectorStoreType = errors.New("invalid vector store type")
	ErrInvalidEmbedderType    = errors.New("invalid embedder type")
	ErrInvalidDataSourceType  = errors.New("invalid data source type")
)

Functions

This section is empty.

Types

type CallOptions

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

func WithCacheSize

func WithCacheSize(size int) CallOptions

func WithDataSource

func WithDataSource(ds datasource.DataSourcer) CallOptions

func WithEmbedder

func WithEmbedder(e embeddings.Embedder) CallOptions

func WithVectorStore

func WithVectorStore(vs vectorstore.VectorStorer) CallOptions

type DTO

type DTO struct {
	VS      vectorstore.Converter `json:"vs"`
	VSType  string                `json:"vsType"`
	Emb     embeddings.Converter  `json:"emb"`
	EmbType string                `json:"embType"`
	DS      datasource.Converter  `json:"ds"`
	DSType  string                `json:"dsType"`
}

func (*DTO) MarshallJSON

func (d *DTO) MarshallJSON() ([]byte, error)

func (*DTO) UnmarshalJSON

func (d *DTO) UnmarshalJSON(b []byte) error

type Memory

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

Memory is a long-term memory for a chatbot

func New

func New(name string, opts ...CallOptions) *Memory

New creates or loads a new Memory instance from the given options

func (*Memory) Close

func (m *Memory) Close() error

Close closes the Memory

func (*Memory) NewDocument

func (m *Memory) NewDocument(text string, user string) *types.Document

NewDocument creates a new document we should be able to accept CreatedAt and LastReadAt as parameters especially if someone already has many conversations that they want to load into this memory

func (*Memory) RetrieveSimilarDocumentsByText

func (m *Memory) RetrieveSimilarDocumentsByText(text string, topK int64) ([]*types.Document, error)

RetrieveSimilarDocumentsByText retrieves similar documents from the Memory TODO Update LastReadAt for the document when it is retrieved

func (*Memory) StoreDocument

func (m *Memory) StoreDocument(document *types.Document) error

StoreDocument stores a document in the Memory

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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