LocalAI
LocalAI copied to clipboard
feat: add LangChainGo Huggingface backend
Description
Greetings to the authors of the LocalAI project, first of all, thank you very much for this wonderful project!
In the process of using it, I needed to implement a couple of solutions that I want to share with the community. First solution is backend langchain-huggingface
.
In details, this PR adds the LangChainGo library to the project and implements a driver for working with the HuggingFace Inference API using it (and other, but this PR implemented only HF LLM). I agree that this PR violates the principle NO Internet access is required either.
but maybe for some users like me, this is not critical.
The solution includes an example of use and a small instruction, see the directory examples/langchain-huggingface
.
It also disables adding .git folder to final Docker-image and other small tunes of ignore files.
Notes for Reviewers
For testing this PR you need to add into a .env
file string like: HUGGINGFACEHUB_API_TOKEN=hf_123456
Token can be generated here https://huggingface.co/settings/tokens
If the PR is approved, in the future I want to use the LangChainGo library to implement the execution of scripts. This will solve the problem with GPU without the need to add it to .cpp binding. Users will simply be able to write their own scripts in Python or any other programming language and just specify the path to them in config.
Signed commits
- [x] Yes, I signed my commits.
I'm not totally against it, as it wouldn't really break our promise to not require internet - at the end this backend should be enabled explicitly from the user - however, I'd be afraid to start to include all the possible remote implementation for different providers. On the other hand, I think this is something that the community might find useful, as we have also already a request for OpenAI here: #328 .
Do you have any plans to attach embeddings to it too? or just wiring up at the completion endpoints?
@mudler hi! In my plans add ability to execute binary files or scripts to call more complex or non-typical solutions, but in any case i can implement support of OpenAI API too, it's not too hard, especially with help of LangChainGo package.
(btw my current local solution works with ruGPT-3 models via calls of python scripts in custom Docker-container with CUDA)
@mudler hi! In my plans add ability to execute binary files or scripts to call more complex or non-typical solutions,
I'm not sure if I do follow 100%, but feel free to open up a separate issue for discussion
but in any case i can implement support of OpenAI API too, it's not too hard, especially with help of LangChainGo package.
Great! keep in mind that there are also other OpenAI clients that we can use to allow a more closer feature mapping. https://github.com/sashabaranov/go-openai for instance supports also image/audio
I'm not sure if I do follow 100%, but feel free to open up a separate issue for discussion
LangChainGo supports local llm https://github.com/tmc/langchaingo/blob/main/llms/local/localllm.go
In general it's just a binary files caller, eg you have some executable python script which will receive some text as argument and return some text to stdout, this script will be executed via os/exec call.
https://github.com/tmc/langchaingo/blob/main/llms/local/internal/localclient/completions.go#L21
After that output will be returned back to user as completion of text.
Thanks, ill check what https://github.com/sashabaranov/go-openai can do
I'm not sure if I do follow 100%, but feel free to open up a separate issue for discussion
LangChainGo supports local llm https://github.com/tmc/langchaingo/blob/main/llms/local/localllm.go
In general it's just a binary files caller, eg you have some executable python script which will receive some text as argument and return some text to stdout, this script will be executed via os/exec call.
https://github.com/tmc/langchaingo/blob/main/llms/local/internal/localclient/completions.go#L21
After that output will be returned back to user as completion of text.
I see, interesting! I think a similar approach came up in #69 , opening up a wide range of possibilities. Let's just make sure we document it properly so anyone can benefit from this.
@mudler okay! I'll done with it after couple hours, need to complete a work day on my job first :)
CI seems to fail, but nothing really serious:
/config.yaml MODELS_PATH=/home/runner/work/LocalAI/LocalAI/test-models go run github.com/onsi/ginkgo/v2/ginkgo --flakeAttempts 5 -v -r ./api ./pkg
missing go.sum entry for module providing package github.com/onsi/ginkgo/v2/ginkgo; to add:
go mod download github.com/onsi/ginkgo/v2
@mudler hi! I've deep dived into a docs, never used Hugo before, but it looks interesting.
I've followed your recommendations and created a PR.
Also added a small fix to naming of variables in $this PR, eg. LangChainHugginFace changed to LCHuggingFaceBackend so that everything looks in uniform style.
@mudler hooray :) thanks! Glad to help, soon i'll done with langchain-local
and back to here )