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 (
|
|
|
|
"fmt"
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/base"
|
2023-07-14 23:19:43 +00:00
|
|
|
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
|
|
|
gpt4all "github.com/nomic-ai/gpt4all/gpt4all-bindings/golang"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LLM struct {
|
2023-08-20 12:04:45 +00:00
|
|
|
base.SingleThread
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
gpt4all *gpt4all.Model
|
|
|
|
}
|
|
|
|
|
|
|
|
func (llm *LLM) Load(opts *pb.ModelOptions) error {
|
2023-08-07 20:39:10 +00:00
|
|
|
model, err := gpt4all.New(opts.ModelFile,
|
2023-07-14 23:19:43 +00:00
|
|
|
gpt4all.SetThreads(int(opts.Threads)),
|
|
|
|
gpt4all.SetLibrarySearchPath(opts.LibrarySearchPath))
|
|
|
|
llm.gpt4all = model
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPredictOptions(opts *pb.PredictOptions) []gpt4all.PredictOption {
|
|
|
|
predictOptions := []gpt4all.PredictOption{
|
|
|
|
gpt4all.SetTemperature(float64(opts.Temperature)),
|
|
|
|
gpt4all.SetTopP(float64(opts.TopP)),
|
|
|
|
gpt4all.SetTopK(int(opts.TopK)),
|
|
|
|
gpt4all.SetTokens(int(opts.Tokens)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Batch != 0 {
|
|
|
|
predictOptions = append(predictOptions, gpt4all.SetBatch(int(opts.Batch)))
|
|
|
|
}
|
|
|
|
return predictOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (llm *LLM) Predict(opts *pb.PredictOptions) (string, error) {
|
|
|
|
return llm.gpt4all.Predict(opts.Prompt, buildPredictOptions(opts)...)
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func (llm *LLM) PredictStream(opts *pb.PredictOptions, results chan string) error {
|
2023-07-14 23:19:43 +00:00
|
|
|
predictOptions := buildPredictOptions(opts)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
llm.gpt4all.SetTokenCallback(func(token string) bool {
|
|
|
|
results <- token
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
_, err := llm.gpt4all.Predict(opts.Prompt, predictOptions...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("err: ", err)
|
|
|
|
}
|
|
|
|
llm.gpt4all.SetTokenCallback(nil)
|
|
|
|
close(results)
|
|
|
|
}()
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
return nil
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|