Skip to content

Commit 3fc8c4a

Browse files
committed
fix(huggingface-langchain): fail if no token is set
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 04b4d9d commit 3fc8c4a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

backend/go/llm/langchain/langchain.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44
// It is meant to be used by the main executable that is the server for the specific backend type (falcon, gpt3, etc)
55
import (
66
"fmt"
7+
"os"
78

89
"github.com/go-skynet/LocalAI/pkg/grpc/base"
910
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
@@ -18,9 +19,14 @@ type LLM struct {
1819
}
1920

2021
func (llm *LLM) Load(opts *pb.ModelOptions) error {
21-
llm.langchain, _ = langchain.NewHuggingFace(opts.Model)
22+
var err error
23+
hfToken := os.Getenv("HUGGINGFACEHUB_API_TOKEN")
24+
if hfToken == "" {
25+
return fmt.Errorf("no huggingface token provided")
26+
}
27+
llm.langchain, err = langchain.NewHuggingFace(opts.Model, hfToken)
2228
llm.model = opts.Model
23-
return nil
29+
return err
2430
}
2531

2632
func (llm *LLM) Predict(opts *pb.PredictOptions) (string, error) {

pkg/langchain/huggingface.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@ package langchain
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/tmc/langchaingo/llms"
78
"github.com/tmc/langchaingo/llms/huggingface"
89
)
910

1011
type HuggingFace struct {
1112
modelPath string
13+
token string
1214
}
1315

14-
func NewHuggingFace(repoId string) (*HuggingFace, error) {
16+
func NewHuggingFace(repoId, token string) (*HuggingFace, error) {
17+
if token == "" {
18+
return nil, fmt.Errorf("no huggingface token provided")
19+
}
1520
return &HuggingFace{
1621
modelPath: repoId,
22+
token: token,
1723
}, nil
1824
}
1925

2026
func (s *HuggingFace) PredictHuggingFace(text string, opts ...PredictOption) (*Predict, error) {
2127
po := NewPredictOptions(opts...)
2228

2329
// Init client
24-
llm, err := huggingface.New()
30+
llm, err := huggingface.New(huggingface.WithToken(s.token))
2531
if err != nil {
2632
return nil, err
2733
}

0 commit comments

Comments
 (0)