feat: add LangChainGo Huggingface backend (#446)
Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>renovate/github.com-imdario-mergo-1.x
parent
7282668da1
commit
3ba07a5928
@ -0,0 +1,68 @@ |
||||
# Data query example |
||||
|
||||
Example of integration with HuggingFace Inference API with help of [langchaingo](https://github.com/tmc/langchaingo). |
||||
|
||||
## Setup |
||||
|
||||
Download the LocalAI and start the API: |
||||
|
||||
```bash |
||||
# Clone LocalAI |
||||
git clone https://github.com/go-skynet/LocalAI |
||||
|
||||
cd LocalAI/examples/langchain-huggingface |
||||
|
||||
docker-compose up -d |
||||
``` |
||||
|
||||
Node: Ensure you've set `HUGGINGFACEHUB_API_TOKEN` environment variable, you can generate it |
||||
on [Settings / Access Tokens](https://huggingface.co/settings/tokens) page of HuggingFace site. |
||||
|
||||
This is an example `.env` file for LocalAI: |
||||
|
||||
```ini |
||||
MODELS_PATH=/models |
||||
CONTEXT_SIZE=512 |
||||
HUGGINGFACEHUB_API_TOKEN=hg_123456 |
||||
``` |
||||
|
||||
## Using remote models |
||||
|
||||
Now you can use any remote models available via HuggingFace API, for example let's enable using of |
||||
[gpt2](https://huggingface.co/gpt2) model in `gpt-3.5-turbo.yaml` config: |
||||
|
||||
```yml |
||||
name: gpt-3.5-turbo |
||||
parameters: |
||||
model: gpt2 |
||||
top_k: 80 |
||||
temperature: 0.2 |
||||
top_p: 0.7 |
||||
context_size: 1024 |
||||
backend: "langchain-huggingface" |
||||
stopwords: |
||||
- "HUMAN:" |
||||
- "GPT:" |
||||
roles: |
||||
user: " " |
||||
system: " " |
||||
template: |
||||
completion: completion |
||||
chat: gpt4all |
||||
``` |
||||
|
||||
Here is you can see in field `parameters.model` equal `gpt2` and `backend` equal `langchain-huggingface`. |
||||
|
||||
## How to use |
||||
|
||||
```shell |
||||
# Now API is accessible at localhost:8080 |
||||
curl http://localhost:8080/v1/models |
||||
# {"object":"list","data":[{"id":"gpt-3.5-turbo","object":"model"}]} |
||||
|
||||
curl http://localhost:8080/v1/completions -H "Content-Type: application/json" -d '{ |
||||
"model": "gpt-3.5-turbo", |
||||
"prompt": "A long time ago in a galaxy far, far away", |
||||
"temperature": 0.7 |
||||
}' |
||||
``` |
@ -0,0 +1,15 @@ |
||||
version: '3.6' |
||||
|
||||
services: |
||||
api: |
||||
image: quay.io/go-skynet/local-ai:latest |
||||
build: |
||||
context: ../../ |
||||
dockerfile: Dockerfile |
||||
ports: |
||||
- 8080:8080 |
||||
env_file: |
||||
- ../../.env |
||||
volumes: |
||||
- ./models:/models:cached |
||||
command: ["/usr/bin/local-ai"] |
@ -0,0 +1 @@ |
||||
{{.Input}} |
@ -0,0 +1,17 @@ |
||||
name: gpt-3.5-turbo |
||||
parameters: |
||||
model: gpt2 |
||||
top_k: 80 |
||||
temperature: 0.2 |
||||
top_p: 0.7 |
||||
context_size: 1024 |
||||
backend: "langchain-huggingface" |
||||
stopwords: |
||||
- "HUMAN:" |
||||
- "GPT:" |
||||
roles: |
||||
user: " " |
||||
system: " " |
||||
template: |
||||
completion: completion |
||||
chat: gpt4all |
@ -0,0 +1,4 @@ |
||||
The prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response. |
||||
### Prompt: |
||||
{{.Input}} |
||||
### Response: |
@ -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 |
||||
} |
@ -0,0 +1,57 @@ |
||||
package langchain |
||||
|
||||
type PredictOptions struct { |
||||
Model string `json:"model"` |
||||
// MaxTokens is the maximum number of tokens to generate.
|
||||
MaxTokens int `json:"max_tokens"` |
||||
// Temperature is the temperature for sampling, between 0 and 1.
|
||||
Temperature float64 `json:"temperature"` |
||||
// StopWords is a list of words to stop on.
|
||||
StopWords []string `json:"stop_words"` |
||||
} |
||||
|
||||
type PredictOption func(p *PredictOptions) |
||||
|
||||
var DefaultOptions = PredictOptions{ |
||||
Model: "gpt2", |
||||
MaxTokens: 200, |
||||
Temperature: 0.96, |
||||
StopWords: nil, |
||||
} |
||||
|
||||
type Predict struct { |
||||
Completion string |
||||
} |
||||
|
||||
func SetModel(model string) PredictOption { |
||||
return func(o *PredictOptions) { |
||||
o.Model = model |
||||
} |
||||
} |
||||
|
||||
func SetTemperature(temperature float64) PredictOption { |
||||
return func(o *PredictOptions) { |
||||
o.Temperature = temperature |
||||
} |
||||
} |
||||
|
||||
func SetMaxTokens(maxTokens int) PredictOption { |
||||
return func(o *PredictOptions) { |
||||
o.MaxTokens = maxTokens |
||||
} |
||||
} |
||||
|
||||
func SetStopWords(stopWords []string) PredictOption { |
||||
return func(o *PredictOptions) { |
||||
o.StopWords = stopWords |
||||
} |
||||
} |
||||
|
||||
// NewPredictOptions Create a new PredictOptions object with the given options.
|
||||
func NewPredictOptions(opts ...PredictOption) PredictOptions { |
||||
p := DefaultOptions |
||||
for _, opt := range opts { |
||||
opt(&p) |
||||
} |
||||
return p |
||||
} |
Loading…
Reference in new issue