From d3a486a4f8f9d73677119ca8207161f03a015fd3 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Mon, 26 Jun 2023 15:12:43 +0200 Subject: [PATCH] feat: Add '/version' endpoint and display it in the CLI (#679) --- Makefile | 8 ++++++++ api/api.go | 7 +++++++ internal/version.go | 10 ++++++++++ main.go | 6 ++++-- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 internal/version.go diff --git a/Makefile b/Makefile index 4b7783e..f14f3d4 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,13 @@ CUDA_LIBPATH?=/usr/local/cuda/lib64/ STABLEDIFFUSION_VERSION?=d89260f598afb809279bc72aa0107b4292587632 GO_TAGS?= BUILD_ID?=git + +VERSION?=$(shell git describe --always --tags --dirty || echo "dev" ) +# go tool nm ./local-ai | grep Commit LD_FLAGS?= +override LD_FLAGS += -X "github.com/go-skynet/LocalAI/internal.Version=$(VERSION)" +override LD_FLAGS += -X "github.com/go-skynet/LocalAI/internal.Commit=$(shell git rev-parse HEAD)" + OPTIONAL_TARGETS?= ESPEAK_DATA?= @@ -244,6 +250,8 @@ build: prepare ## Build the project $(info ${GREEN}I local-ai build info:${RESET}) $(info ${GREEN}I BUILD_TYPE: ${YELLOW}$(BUILD_TYPE)${RESET}) $(info ${GREEN}I GO_TAGS: ${YELLOW}$(GO_TAGS)${RESET}) + $(info ${GREEN}I LD_FLAGS: ${YELLOW}$(LD_FLAGS)${RESET}) + CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH=${C_INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} $(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o $(BINARY_NAME) ./ ifeq ($(BUILD_TYPE),metal) cp go-llama/build/bin/ggml-metal.metal . diff --git a/api/api.go b/api/api.go index d570e76..e4aac2f 100644 --- a/api/api.go +++ b/api/api.go @@ -3,6 +3,7 @@ package api import ( "errors" + "github.com/go-skynet/LocalAI/internal" "github.com/go-skynet/LocalAI/pkg/assets" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -105,6 +106,12 @@ func App(opts ...AppOption) (*fiber.App, error) { applier := newGalleryApplier(options.loader.ModelPath) applier.start(options.context, cm) + app.Get("/version", func(c *fiber.Ctx) error { + return c.JSON(struct { + Version string `json:"version"` + }{Version: internal.PrintableVersion()}) + }) + app.Post("/models/apply", applyModelGallery(options.loader.ModelPath, cm, applier.C, options.galleries)) app.Get("/models/available", listModelFromGallery(options.galleries, options.loader.ModelPath)) app.Get("/models/jobs/:uuid", getOpStatus(applier)) diff --git a/internal/version.go b/internal/version.go new file mode 100644 index 0000000..12246c2 --- /dev/null +++ b/internal/version.go @@ -0,0 +1,10 @@ +package internal + +import "fmt" + +var Version = "" +var Commit = "" + +func PrintableVersion() string { + return fmt.Sprintf("LocalAI %s (%s)", Version, Commit) +} diff --git a/main.go b/main.go index fbdc390..12b129c 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "path/filepath" api "github.com/go-skynet/LocalAI/api" + "github.com/go-skynet/LocalAI/internal" "github.com/go-skynet/LocalAI/pkg/gallery" model "github.com/go-skynet/LocalAI/pkg/model" "github.com/rs/zerolog" @@ -24,8 +25,9 @@ func main() { } app := &cli.App{ - Name: "LocalAI", - Usage: "OpenAI compatible API for running LLaMA/GPT models locally on CPU with consumer grade hardware.", + Name: "LocalAI", + Version: internal.PrintableVersion(), + Usage: "OpenAI compatible API for running LLaMA/GPT models locally on CPU with consumer grade hardware.", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "f16",