mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 10:35:01 +00:00
feat: add LangChainGo Huggingface backend (#446)
Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
This commit is contained in:
parent
7282668da1
commit
3ba07a5928
13 changed files with 241 additions and 0 deletions
47
pkg/langchain/huggingface.go
Normal file
47
pkg/langchain/huggingface.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package langchain
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tmc/langchaingo/llms"
|
||||
"github.com/tmc/langchaingo/llms/huggingface"
|
||||
)
|
||||
|
||||
type HuggingFace struct {
|
||||
modelPath string
|
||||
}
|
||||
|
||||
func NewHuggingFace(repoId string) (*HuggingFace, error) {
|
||||
return &HuggingFace{
|
||||
modelPath: repoId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *HuggingFace) PredictHuggingFace(text string, opts ...PredictOption) (*Predict, error) {
|
||||
po := NewPredictOptions(opts...)
|
||||
|
||||
// Init client
|
||||
llm, err := huggingface.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert from LocalAI to LangChainGo format of options
|
||||
co := []llms.CallOption{
|
||||
llms.WithModel(po.Model),
|
||||
llms.WithMaxTokens(po.MaxTokens),
|
||||
llms.WithTemperature(po.Temperature),
|
||||
llms.WithStopWords(po.StopWords),
|
||||
}
|
||||
|
||||
// Call Inference API
|
||||
ctx := context.Background()
|
||||
completion, err := llm.Call(ctx, text, co...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Predict{
|
||||
Completion: completion,
|
||||
}, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue