Rename project to LocalAI (#35)

Signed-off-by: mudler <mudler@c3os.io>
add/first-example v1.0
Ettore Di Giacinto 2 years ago committed by GitHub
parent 7fec26f5d3
commit 80f50e6ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .github/workflows/image.yml
  2. 5
      .gitignore
  3. 2
      .goreleaser.yaml
  4. 4
      Dockerfile
  5. 2
      Earthfile
  6. 2
      Makefile
  7. 115
      README.md
  8. 3
      api/api.go
  9. 16
      docker-compose.yaml
  10. 2
      go.mod
  11. 4
      kubernetes/deployment.yaml
  12. 246
      main.go

@ -19,7 +19,7 @@ jobs:
- name: Prepare - name: Prepare
id: prep id: prep
run: | run: |
DOCKER_IMAGE=quay.io/go-skynet/llama-cli DOCKER_IMAGE=quay.io/go-skynet/local-ai
VERSION=master VERSION=master
SHORTREF=${GITHUB_SHA::8} SHORTREF=${GITHUB_SHA::8}

5
.gitignore vendored

@ -2,8 +2,9 @@
go-llama go-llama
go-gpt4all-j go-gpt4all-j
# llama-cli build binary # LocalAI build binary
llama-cli LocalAI
local-ai
# Ignore models # Ignore models
models/*.bin models/*.bin

@ -1,5 +1,5 @@
# Make sure to check the documentation at http://goreleaser.com # Make sure to check the documentation at http://goreleaser.com
project_name: llama-cli project_name: local-ai
builds: builds:
- ldflags: - ldflags:
- -w -s - -w -s

@ -8,5 +8,5 @@ ARG BUILD_TYPE=
RUN make build${BUILD_TYPE} RUN make build${BUILD_TYPE}
FROM debian:$DEBIAN_VERSION FROM debian:$DEBIAN_VERSION
COPY --from=builder /build/llama-cli /usr/bin/llama-cli COPY --from=builder /build/local-ai /usr/bin/local-ai
ENTRYPOINT [ "/usr/bin/llama-cli" ] ENTRYPOINT [ "/usr/bin/local-ai" ]

@ -2,4 +2,4 @@ VERSION 0.7
build: build:
FROM DOCKERFILE -f Dockerfile . FROM DOCKERFILE -f Dockerfile .
SAVE ARTIFACT /usr/bin/llama-cli AS LOCAL llama-cli SAVE ARTIFACT /usr/bin/local-ai AS LOCAL local-ai

@ -1,7 +1,7 @@
GOCMD=go GOCMD=go
GOTEST=$(GOCMD) test GOTEST=$(GOCMD) test
GOVET=$(GOCMD) vet GOVET=$(GOCMD) vet
BINARY_NAME=llama-cli BINARY_NAME=local-ai
GOLLAMA_VERSION?=llama.cpp-5ecff35 GOLLAMA_VERSION?=llama.cpp-5ecff35
GREEN := $(shell tput -Txterm setaf 2) GREEN := $(shell tput -Txterm setaf 2)

@ -1,7 +1,8 @@
## :camel: llama-cli ## :camel: LocalAI
> :warning: This project has been renamed from `llama-cli` to `LocalAI` to reflect the fact that we are focusing on a fast drop-in OpenAI API rather on the CLI interface. We think that there are already many projects that can be used as a CLI interface already, for instance [llama.cpp](https://github.com/ggerganov/llama.cpp) and [gpt4all](https://github.com/nomic-ai/gpt4all). If you are were using `llama-cli` for CLI interactions and want to keep using it, use older versions or please open up an issue - contributions are welcome!
llama-cli is a straightforward, drop-in replacement API compatible with OpenAI for local CPU inferencing, based on [llama.cpp](https://github.com/ggerganov/llama.cpp), [gpt4all](https://github.com/nomic-ai/gpt4all) and [ggml](https://github.com/ggerganov/ggml), including support GPT4ALL-J which is Apache 2.0 Licensed and can be used for commercial purposes. LocalAI is a straightforward, drop-in replacement API compatible with OpenAI for local CPU inferencing, based on [llama.cpp](https://github.com/ggerganov/llama.cpp), [gpt4all](https://github.com/nomic-ai/gpt4all) and [ggml](https://github.com/ggerganov/ggml), including support GPT4ALL-J which is Apache 2.0 Licensed and can be used for commercial purposes.
- OpenAI compatible API - OpenAI compatible API
- Supports multiple-models - Supports multiple-models
@ -18,12 +19,15 @@ Note: You might need to convert older models to the new format, see [here](https
## Usage ## Usage
The easiest way to run llama-cli is by using `docker-compose`: > `LocalAI` comes by default as a container image. You can check out all the available images with corresponding tags [here](https://quay.io/repository/go-skynet/local-ai?tab=tags&tag=latest).
The easiest way to run LocalAI is by using `docker-compose`:
```bash ```bash
git clone https://github.com/go-skynet/llama-cli git clone https://github.com/go-skynet/LocalAI
cd llama-cli
cd LocalAI
# copy your models to models/ # copy your models to models/
cp your-model.bin models/ cp your-model.bin models/
@ -45,8 +49,11 @@ curl http://localhost:8080/v1/completions -H "Content-Type: application/json" -d
}' }'
``` ```
Note: The API doesn't inject a default prompt for talking to the model, while the CLI does. You have to use a prompt similar to what's described in the standford-alpaca docs: https://github.com/tatsu-lab/stanford_alpaca#data-release. ## Prompt templates
The API doesn't inject a default prompt for talking to the model. You have to use a prompt similar to what's described in the standford-alpaca docs: https://github.com/tatsu-lab/stanford_alpaca#data-release.
<details>
You can use a default template for every model present in your model path, by creating a corresponding file with the `.tmpl` suffix next to your model. For instance, if the model is called `foo.bin`, you can create a sibiling file, `foo.bin.tmpl` which will be used as a default prompt, for instance this can be used with alpaca: You can use a default template for every model present in your model path, by creating a corresponding file with the `.tmpl` suffix next to your model. For instance, if the model is called `foo.bin`, you can create a sibiling file, `foo.bin.tmpl` which will be used as a default prompt, for instance this can be used with alpaca:
``` ```
@ -58,70 +65,19 @@ Below is an instruction that describes a task. Write a response that appropriate
### Response: ### Response:
``` ```
See the [prompt-templates](https://github.com/go-skynet/llama-cli/tree/master/prompt-templates) directory in this repository for templates for most popular models. See the [prompt-templates](https://github.com/go-skynet/LocalAI/tree/master/prompt-templates) directory in this repository for templates for most popular models.
## Container images
`llama-cli` comes by default as a container image. You can check out all the available images with corresponding tags [here](https://quay.io/repository/go-skynet/llama-cli?tab=tags&tag=latest)
To begin, run:
```
docker run -ti --rm quay.io/go-skynet/llama-cli:latest --instruction "What's an alpaca?" --topk 10000 --model ...
```
Where `--model` is the path of the model you want to use.
Note: you need to mount a volume to the docker container in order to load a model, for instance:
```
# assuming your model is in /path/to/your/models/foo.bin
docker run -v /path/to/your/models:/models -ti --rm quay.io/go-skynet/llama-cli:latest --instruction "What's an alpaca?" --topk 10000 --model /models/foo.bin
```
You will receive a response like the following:
```
An alpaca is a member of the South American Camelid family, which includes the llama, guanaco and vicuña. It is a domesticated species that originates from the Andes mountain range in South America. Alpacas are used in the textile industry for their fleece, which is much softer than wool. Alpacas are also used for meat, milk, and fiber.
```
## Basic usage </details>
To use llama-cli, specify a pre-trained GPT-based model, an input text, and an instruction for text generation. llama-cli takes the following arguments when running from the CLI:
```
llama-cli --model <model_path> --instruction <instruction> [--input <input>] [--template <template_path>] [--tokens <num_tokens>] [--threads <num_threads>] [--temperature <temperature>] [--topp <top_p>] [--topk <top_k>]
```
| Parameter | Environment Variable | Default Value | Description |
| ------------ | -------------------- | ------------- | -------------------------------------- |
| template | TEMPLATE | | A file containing a template for output formatting (optional). |
| instruction | INSTRUCTION | | Input prompt text or instruction. "-" for STDIN. |
| input | INPUT | - | Path to text or "-" for STDIN. |
| model | MODEL | | The path to the pre-trained GPT-based model. |
| tokens | TOKENS | 128 | The maximum number of tokens to generate. |
| threads | THREADS | NumCPU() | The number of threads to use for text generation. |
| temperature | TEMPERATURE | 0.95 | Sampling temperature for model output. ( values between `0.1` and `1.0` ) |
| top_p | TOP_P | 0.85 | The cumulative probability for top-p sampling. |
| top_k | TOP_K | 20 | The number of top-k tokens to consider for text generation. |
| context-size | CONTEXT_SIZE | 512 | Default token context size. |
Here's an example of using `llama-cli`:
```
llama-cli --model ~/ggml-alpaca-7b-q4.bin --instruction "What's an alpaca?"
```
This will generate text based on the given model and instruction.
## API ## API
`llama-cli` also provides an API for running text generation as a service. The models once loaded the first time will be kept in memory. `LocalAI` provides an API for running text generation as a service, that follows the OpenAI reference and can be used as a drop-in. The models once loaded the first time will be kept in memory.
<details>
Example of starting the API with `docker`: Example of starting the API with `docker`:
```bash ```bash
docker run -p 8080:8080 -ti --rm quay.io/go-skynet/llama-cli:latest api --models-path /path/to/models --context-size 700 --threads 4 docker run -p 8080:8080 -ti --rm quay.io/go-skynet/local-api:latest --models-path /path/to/models --context-size 700 --threads 4
``` ```
And you'll see: And you'll see:
@ -136,15 +92,15 @@ And you'll see:
└───────────────────────────────────────────────────┘ └───────────────────────────────────────────────────┘
``` ```
Note: Models have to end up with `.bin`. Note: Models have to end up with `.bin` so can be listed by the `/models` endpoint.
You can control the API server options with command line arguments: You can control the API server options with command line arguments:
``` ```
llama-cli api --models-path <model_path> [--address <address>] [--threads <num_threads>] local-api --models-path <model_path> [--address <address>] [--threads <num_threads>]
``` ```
The API takes takes the following: The API takes takes the following parameters:
| Parameter | Environment Variable | Default Value | Description | | Parameter | Environment Variable | Default Value | Description |
| ------------ | -------------------- | ------------- | -------------------------------------- | | ------------ | -------------------- | ------------- | -------------------------------------- |
@ -155,6 +111,8 @@ The API takes takes the following:
Once the server is running, you can start making requests to it using HTTP, using the OpenAI API. Once the server is running, you can start making requests to it using HTTP, using the OpenAI API.
</details>
### Supported OpenAI API endpoints ### Supported OpenAI API endpoints
You can check out the [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create). You can check out the [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create).
@ -212,41 +170,34 @@ python 828bddec6162a023114ce19146cb2b82/gistfile1.txt models tokenizer.model
### Windows compatibility ### Windows compatibility
It should work, however you need to make sure you give enough resources to the container. See https://github.com/go-skynet/llama-cli/issues/2 It should work, however you need to make sure you give enough resources to the container. See https://github.com/go-skynet/LocalAI/issues/2
### Kubernetes ### Kubernetes
You can run the API directly in Kubernetes: You can run the API in Kubernetes, see an example deployment in [kubernetes](https://github.com/go-skynet/LocalAI/tree/master/kubernetes)
```bash
kubectl apply -f https://raw.githubusercontent.com/go-skynet/llama-cli/master/kubernetes/deployment.yaml
```
### Build locally ### Build locally
Pre-built images might fit well for most of the modern hardware, however you can and might need to build the images manually. Pre-built images might fit well for most of the modern hardware, however you can and might need to build the images manually.
In order to build the `llama-cli` container image locally you can use `docker`: In order to build the `LocalAI` container image locally you can use `docker`:
``` ```
# build the image as "alpaca-image" # build the image
docker build -t llama-cli . docker build -t LocalAI .
docker run llama-cli --instruction "What's an alpaca?" docker run LocalAI
``` ```
Or build the binary with: Or build the binary with `make`:
``` ```
# build the image as "alpaca-image" make build
docker run --privileged -v /var/run/docker.sock:/var/run/docker.sock --rm -t -v "$(pwd)":/workspace -v earthly-tmp:/tmp/earthly:rw earthly/earthly:v0.7.2 +build
# run the binary
./llama-cli --instruction "What's an alpaca?"
``` ```
## Short-term roadmap ## Short-term roadmap
- [x] Mimic OpenAI API (https://github.com/go-skynet/llama-cli/issues/10) - [x] Mimic OpenAI API (https://github.com/go-skynet/LocalAI/issues/10)
- Binary releases (https://github.com/go-skynet/llama-cli/issues/6) - Binary releases (https://github.com/go-skynet/LocalAI/issues/6)
- Upstream our golang bindings to llama.cpp (https://github.com/ggerganov/llama.cpp/issues/351) - Upstream our golang bindings to llama.cpp (https://github.com/ggerganov/llama.cpp/issues/351)
- [x] Multi-model support - [x] Multi-model support
- Have a webUI! - Have a webUI!

@ -5,8 +5,7 @@ import (
"strings" "strings"
"sync" "sync"
model "github.com/go-skynet/llama-cli/pkg/model" model "github.com/go-skynet/LocalAI/pkg/model"
gptj "github.com/go-skynet/go-gpt4all-j.cpp" gptj "github.com/go-skynet/go-gpt4all-j.cpp"
llama "github.com/go-skynet/go-llama.cpp" llama "github.com/go-skynet/go-llama.cpp"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"

@ -1,18 +1,8 @@
version: '3.6' version: '3.6'
services: services:
# chatgpt:
# image: ghcr.io/mckaywrigley/chatbot-ui:main
# # platform: linux/amd64
# ports:
# - 3000:3000
# environment:
# - 'OPENAI_API_KEY=sk-000000000000000'
# - 'OPENAI_API_HOST=http://api:8080'
api: api:
image: quay.io/go-skynet/llama-cli:latest image: quay.io/go-skynet/local-ai:latest
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
@ -25,6 +15,4 @@ services:
- CONTEXT_SIZE=$CONTEXT_SIZE - CONTEXT_SIZE=$CONTEXT_SIZE
- THREADS=$THREADS - THREADS=$THREADS
volumes: volumes:
- ./models:/models:cached - ./models:/models:cached
command: api

@ -1,4 +1,4 @@
module github.com/go-skynet/llama-cli module github.com/go-skynet/LocalAI
go 1.19 go 1.19

@ -23,9 +23,7 @@ spec:
spec: spec:
containers: containers:
- name: llama - name: llama
args: image: quay.io/go-skynet/local-ai:latest
- api
image: quay.io/go-skynet/llama-cli:latest
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service

@ -1,230 +1,76 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"runtime" "runtime"
"text/template"
llama "github.com/go-skynet/go-llama.cpp" api "github.com/go-skynet/LocalAI/api"
api "github.com/go-skynet/llama-cli/api" model "github.com/go-skynet/LocalAI/pkg/model"
model "github.com/go-skynet/llama-cli/pkg/model"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// Define the template string func main() {
var emptyInput string = `Below is an instruction that describes a task. Write a response that appropriately completes the request. path, err := os.Getwd()
### Instruction:
{{.Instruction}}
### Response:`
var nonEmptyInput string = `Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{{.Instruction}}
### Input:
{{.Input}}
### Response:
`
func templateString(t string, in interface{}) (string, error) {
// Parse the template
tmpl, err := template.New("prompt").Parse(t)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, in)
if err != nil { if err != nil {
return "", err fmt.Println(err)
os.Exit(1)
} }
return buf.String(), nil
}
var modelFlags = []cli.Flag{
&cli.StringFlag{
Name: "model",
EnvVars: []string{"MODEL"},
},
&cli.IntFlag{
Name: "tokens",
EnvVars: []string{"TOKENS"},
Value: 128,
},
&cli.IntFlag{
Name: "context-size",
EnvVars: []string{"CONTEXT_SIZE"},
Value: 512,
},
&cli.IntFlag{
Name: "threads",
EnvVars: []string{"THREADS"},
Value: runtime.NumCPU(),
},
&cli.Float64Flag{
Name: "temperature",
EnvVars: []string{"TEMPERATURE"},
Value: 0.95,
},
&cli.Float64Flag{
Name: "topp",
EnvVars: []string{"TOP_P"},
Value: 0.85,
},
&cli.IntFlag{
Name: "topk",
EnvVars: []string{"TOP_K"},
Value: 20,
},
}
func main() {
app := &cli.App{ app := &cli.App{
Name: "llama-cli", Name: "LocalAI",
Version: "0.1", Usage: "OpenAI compatible API for running LLaMA/GPT models locally on CPU with consumer grade hardware.",
Usage: "llama-cli --model ... --instruction 'What is an alpaca?'", Flags: []cli.Flag{
Flags: append(modelFlags, &cli.BoolFlag{
&cli.StringFlag{ Name: "f16",
Name: "template", EnvVars: []string{"F16"},
EnvVars: []string{"TEMPLATE"}, },
&cli.IntFlag{
Name: "threads",
DefaultText: "Number of threads used for parallel computation. Usage of the number of physical cores in the system is suggested.",
EnvVars: []string{"THREADS"},
Value: runtime.NumCPU(),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "instruction", Name: "models-path",
EnvVars: []string{"INSTRUCTION"}, DefaultText: "Path containing models used for inferencing",
EnvVars: []string{"MODELS_PATH"},
Value: path,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "input", Name: "address",
EnvVars: []string{"INPUT"}, DefaultText: "Bind address for the API server.",
}), EnvVars: []string{"ADDRESS"},
Description: `Run llama.cpp inference`, Value: ":8080",
UsageText: ` },
llama-cli --model ~/ggml-alpaca-7b-q4.bin --instruction "What's an alpaca?" &cli.IntFlag{
Name: "context-size",
DefaultText: "Default context size of the model",
EnvVars: []string{"CONTEXT_SIZE"},
Value: 512,
},
},
Description: `
LocalAI is a drop-in replacement OpenAI API which runs inference locally.
An Alpaca (Vicugna pacos) is a domesticated species of South American camelid, related to llamas and originally from Peru but now found throughout much of Andean region. They are bred for their fleeces which can be spun into wool or knitted items such as hats, sweaters, blankets etc Some of the models compatible are:
- Vicuna
echo "An Alpaca (Vicugna pacos) is a domesticated species of South American camelid, related to llamas and originally from Peru but now found throughout much of Andean region. They are bred for their fleeces which can be spun into wool or knitted items such as hats, sweaters, blankets etc" | llama-cli --model ~/ggml-alpaca-7b-q4.bin --instruction "Proofread, improving clarity and flow" --input "-" - Koala
- GPT4ALL
- GPT4ALL-J
- Alpaca
An Alpaca (Vicugna pacos) is a domesticated species from South America that's related to llamas. Originating in Peru but now found throughout the Andean region, they are bred for their fleeces which can be spun into wool or knitted items such as hats and sweatersblankets too! It uses llama.cpp and gpt4all as backend, supporting all the models supported by both.
`, `,
UsageText: `local-ai [options]`,
Copyright: "go-skynet authors", Copyright: "go-skynet authors",
Commands: []*cli.Command{
{
Name: "api",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "f16",
EnvVars: []string{"F16"},
},
&cli.IntFlag{
Name: "threads",
EnvVars: []string{"THREADS"},
Value: runtime.NumCPU(),
},
&cli.StringFlag{
Name: "models-path",
EnvVars: []string{"MODELS_PATH"},
},
&cli.StringFlag{
Name: "address",
EnvVars: []string{"ADDRESS"},
Value: ":8080",
},
&cli.IntFlag{
Name: "context-size",
EnvVars: []string{"CONTEXT_SIZE"},
Value: 512,
},
},
Action: func(ctx *cli.Context) error {
return api.Start(model.NewModelLoader(ctx.String("models-path")), ctx.String("address"), ctx.Int("threads"), ctx.Int("context-size"), ctx.Bool("f16"))
},
},
},
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
return api.Start(model.NewModelLoader(ctx.String("models-path")), ctx.String("address"), ctx.Int("threads"), ctx.Int("context-size"), ctx.Bool("f16"))
instruction := ctx.String("instruction")
input := ctx.String("input")
templ := ctx.String("template")
promptTemplate := ""
if input != "" {
promptTemplate = nonEmptyInput
} else {
promptTemplate = emptyInput
}
if templ != "" {
dat, err := os.ReadFile(templ)
if err != nil {
fmt.Printf("Failed reading file: %s", err.Error())
os.Exit(1)
}
promptTemplate = string(dat)
}
if instruction == "-" {
dat, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Printf("reading stdin failed: %s", err)
os.Exit(1)
}
instruction = string(dat)
}
if input == "-" {
dat, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Printf("reading stdin failed: %s", err)
os.Exit(1)
}
input = string(dat)
}
str, err := templateString(promptTemplate, struct {
Instruction string
Input string
}{Instruction: instruction, Input: input})
if err != nil {
fmt.Println("Templating the input failed:", err.Error())
os.Exit(1)
}
opts := []llama.ModelOption{llama.SetContext(ctx.Int("context-size"))}
l, err := llama.New(ctx.String("model"), opts...)
if err != nil {
fmt.Println("Loading the model failed:", err.Error())
os.Exit(1)
}
res, err := l.Predict(
str,
llama.SetTemperature(ctx.Float64("temperature")),
llama.SetTopP(ctx.Float64("topp")),
llama.SetTopK(ctx.Int("topk")),
llama.SetTokens(ctx.Int("tokens")),
llama.SetThreads(ctx.Int("threads")),
)
if err != nil {
fmt.Printf("predicting failed: %s", err)
os.Exit(1)
}
fmt.Println(res)
return nil
}, },
} }
err := app.Run(os.Args) err = app.Run(os.Args)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

Loading…
Cancel
Save