2023-11-13 21:40:16 +00:00
|
|
|
package main
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
// This is a wrapper to statisfy the GRPC service interface
|
|
|
|
// It is meant to be used by the main executable that is the server for the specific backend type (falcon, gpt3, etc)
|
|
|
|
import (
|
|
|
|
bert "github.com/go-skynet/go-bert.cpp"
|
|
|
|
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/base"
|
|
|
|
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Embeddings struct {
|
2023-08-20 12:04:45 +00:00
|
|
|
base.SingleThread
|
2023-07-14 23:19:43 +00:00
|
|
|
bert *bert.Bert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (llm *Embeddings) Load(opts *pb.ModelOptions) error {
|
2023-08-07 20:39:10 +00:00
|
|
|
model, err := bert.New(opts.ModelFile)
|
2023-07-14 23:19:43 +00:00
|
|
|
llm.bert = model
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (llm *Embeddings) Embeddings(opts *pb.PredictOptions) ([]float32, error) {
|
2023-08-18 19:23:14 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
if len(opts.EmbeddingTokens) > 0 {
|
|
|
|
tokens := []int{}
|
|
|
|
for _, t := range opts.EmbeddingTokens {
|
|
|
|
tokens = append(tokens, int(t))
|
|
|
|
}
|
|
|
|
return llm.bert.TokenEmbeddings(tokens, bert.SetThreads(int(opts.Threads)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return llm.bert.Embeddings(opts.Embeddings, bert.SetThreads(int(opts.Threads)))
|
|
|
|
}
|