Skip to content

Commit 123a5a2

Browse files
authored
feat(swagger): Add swagger API doc (#1926)
* makefile(build): add minimal and api build target * feat(swagger): Add swagger
1 parent ab2f403 commit 123a5a2

File tree

17 files changed

+2264
-25
lines changed

17 files changed

+2264
-25
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ build: prepare backend-assets grpcs ## Build the project
307307
$(info ${GREEN}I LD_FLAGS: ${YELLOW}$(LD_FLAGS)${RESET})
308308
CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o $(BINARY_NAME) ./
309309

310+
build-minimal:
311+
BUILD_GRPC_FOR_BACKEND_LLAMA=true GRPC_BACKENDS=backend-assets/grpc/llama-cpp GO_TAGS=none $(MAKE) build
312+
313+
build-api:
314+
BUILD_GRPC_FOR_BACKEND_LLAMA=true BUILD_API_ONLY=true GO_TAGS=none $(MAKE) build
315+
310316
dist: build
311317
mkdir -p release
312318
cp $(BINARY_NAME) release/$(BINARY_NAME)-$(BUILD_ID)-$(OS)-$(ARCH)
@@ -584,3 +590,7 @@ docker-image-intel-xpu:
584590
--build-arg IMAGE_TYPE=$(IMAGE_TYPE) \
585591
--build-arg GO_TAGS="none" \
586592
--build-arg BUILD_TYPE=sycl_f32 -t $(DOCKER_IMAGE) .
593+
594+
.PHONY: swagger
595+
swagger:
596+
swag init -g core/http/api.go --output swagger

core/http/api.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/go-skynet/LocalAI/pkg/utils"
10+
"github.com/gofiber/swagger" // swagger handler
1011

1112
"github.com/go-skynet/LocalAI/core/http/endpoints/elevenlabs"
1213
"github.com/go-skynet/LocalAI/core/http/endpoints/localai"
@@ -42,6 +43,20 @@ func readAuthHeader(c *fiber.Ctx) string {
4243
return authHeader
4344
}
4445

46+
// @title LocalAI API
47+
// @version 2.0.0
48+
// @description The LocalAI Rest API.
49+
// @termsOfService
50+
// @contact.name LocalAI
51+
// @contact.url https://localai.io
52+
// @license.name MIT
53+
// @license.url https://raw.githubusercontent.com/mudler/LocalAI/master/LICENSE
54+
// @host localhost:8080
55+
// @BasePath /
56+
// @securityDefinitions.apikey BearerAuth
57+
// @in header
58+
// @name Authorization
59+
4560
func App(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) (*fiber.App, error) {
4661
// Return errors as JSON responses
4762
app := fiber.New(fiber.Config{
@@ -170,6 +185,8 @@ func App(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *confi
170185
utils.LoadConfig(appConfig.ConfigsDir, openai.AssistantsConfigFile, &openai.Assistants)
171186
utils.LoadConfig(appConfig.ConfigsDir, openai.AssistantsFileConfigFile, &openai.AssistantFiles)
172187

188+
app.Get("/swagger/*", swagger.HandlerDefault) // default
189+
173190
welcomeRoute(
174191
app,
175192
cl,

core/http/endpoints/elevenlabs/tts.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/rs/zerolog/log"
1212
)
1313

14+
// TTSEndpoint is the OpenAI Speech API endpoint https://platform.openai.com/docs/api-reference/audio/createSpeech
15+
// @Summary Generates audio from the input text.
16+
// @Param voice-id path string true "Account ID"
17+
// @Param request body schema.TTSRequest true "query params"
18+
// @Success 200 {string} binary "Response"
19+
// @Router /v1/text-to-speech/{voice-id} [post]
1420
func TTSEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
1521
return func(c *fiber.Ctx) error {
1622

core/http/endpoints/localai/tts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"github.com/rs/zerolog/log"
1212
)
1313

14+
// TTSEndpoint is the OpenAI Speech API endpoint https://platform.openai.com/docs/api-reference/audio/createSpeech
15+
// @Summary Generates audio from the input text.
16+
// @Param request body schema.TTSRequest true "query params"
17+
// @Success 200 {string} binary "Response"
18+
// @Router /v1/audio/speech [post]
1419
func TTSEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
1520
return func(c *fiber.Ctx) error {
1621

core/http/endpoints/openai/assistant.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package openai
22

33
import (
44
"fmt"
5-
"github.com/go-skynet/LocalAI/core/config"
6-
model "github.com/go-skynet/LocalAI/pkg/model"
7-
"github.com/go-skynet/LocalAI/pkg/utils"
8-
"github.com/gofiber/fiber/v2"
9-
"github.com/rs/zerolog/log"
105
"net/http"
116
"sort"
127
"strconv"
138
"strings"
149
"sync/atomic"
1510
"time"
11+
12+
"github.com/go-skynet/LocalAI/core/config"
13+
model "github.com/go-skynet/LocalAI/pkg/model"
14+
"github.com/go-skynet/LocalAI/pkg/utils"
15+
"github.com/gofiber/fiber/v2"
16+
"github.com/rs/zerolog/log"
1617
)
1718

1819
// ToolType defines a type for tool options
@@ -65,6 +66,11 @@ type AssistantRequest struct {
6566
Metadata map[string]string `json:"metadata,omitempty"`
6667
}
6768

69+
// CreateAssistantEndpoint is the OpenAI Assistant API endpoint https://platform.openai.com/docs/api-reference/assistants/createAssistant
70+
// @Summary Create an assistant with a model and instructions.
71+
// @Param request body AssistantRequest true "query params"
72+
// @Success 200 {object} Assistant "Response"
73+
// @Router /v1/assistants [post]
6874
func CreateAssistantEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
6975
return func(c *fiber.Ctx) error {
7076
request := new(AssistantRequest)

core/http/endpoints/openai/chat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import (
2020
"github.com/valyala/fasthttp"
2121
)
2222

23+
// ChatEndpoint is the OpenAI Completion API endpoint https://platform.openai.com/docs/api-reference/chat/create
24+
// @Summary Generate a chat completions for a given prompt and model.
25+
// @Param request body schema.OpenAIRequest true "query params"
26+
// @Success 200 {object} schema.OpenAIResponse "Response"
27+
// @Router /v1/chat/completions [post]
2328
func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, startupOptions *config.ApplicationConfig) func(c *fiber.Ctx) error {
2429
emptyMessage := ""
2530
id := uuid.New().String()

core/http/endpoints/openai/completion.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import (
2020
"github.com/valyala/fasthttp"
2121
)
2222

23-
// https://platform.openai.com/docs/api-reference/completions
23+
// CompletionEndpoint is the OpenAI Completion API endpoint https://platform.openai.com/docs/api-reference/completions
24+
// @Summary Generate completions for a given prompt and model.
25+
// @Param request body schema.OpenAIRequest true "query params"
26+
// @Success 200 {object} schema.OpenAIResponse "Response"
27+
// @Router /v1/completions [post]
2428
func CompletionEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
2529
id := uuid.New().String()
2630
created := int(time.Now().Unix())

core/http/endpoints/openai/embeddings.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import (
1616
"github.com/rs/zerolog/log"
1717
)
1818

19-
// https://platform.openai.com/docs/api-reference/embeddings
19+
// EmbeddingsEndpoint is the OpenAI Embeddings API endpoint https://platform.openai.com/docs/api-reference/embeddings
20+
// @Summary Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
21+
// @Param request body schema.OpenAIRequest true "query params"
22+
// @Success 200 {object} schema.OpenAIResponse "Response"
23+
// @Router /v1/embeddings [post]
2024
func EmbeddingsEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
2125
return func(c *fiber.Ctx) error {
2226
model, input, err := readRequest(c, ml, appConfig, true)

core/http/endpoints/openai/image.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func downloadFile(url string) (string, error) {
4444
return out.Name(), err
4545
}
4646

47-
// https://platform.openai.com/docs/api-reference/images/create
47+
//
4848

4949
/*
5050
*
@@ -59,6 +59,11 @@ func downloadFile(url string) (string, error) {
5959
6060
*
6161
*/
62+
// ImageEndpoint is the OpenAI Image generation API endpoint https://platform.openai.com/docs/api-reference/images/create
63+
// @Summary Creates an image given a prompt.
64+
// @Param request body schema.OpenAIRequest true "query params"
65+
// @Success 200 {object} schema.OpenAIResponse "Response"
66+
// @Router /v1/images/generations [post]
6267
func ImageEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
6368
return func(c *fiber.Ctx) error {
6469
m, input, err := readRequest(c, ml, appConfig, false)

core/http/endpoints/openai/transcription.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import (
1616
"github.com/rs/zerolog/log"
1717
)
1818

19-
// https://platform.openai.com/docs/api-reference/audio/create
19+
// TranscriptEndpoint is the OpenAI Whisper API endpoint https://platform.openai.com/docs/api-reference/audio/create
20+
// @Summary Transcribes audio into the input language.
21+
// @accept multipart/form-data
22+
// @Param model formData string true "model"
23+
// @Param file formData file true "file"
24+
// @Success 200 {object} map[string]string "Response"
25+
// @Router /v1/audio/transcriptions [post]
2026
func TranscriptEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
2127
return func(c *fiber.Ctx) error {
2228
m, input, err := readRequest(c, ml, appConfig, false)

core/http/views/partials/navbar.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div>
1010
<a href="/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fas fa-home pr-2"></i>Home</a>
1111
<a href="https://localai.io" class="text-gray-400 hover:text-white px-3 py-2 rounded" target="_blank" ><i class="fas fa-book-reader pr-2"></i> Documentation</a>
12+
<a href="/swagger/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fas fa-code pr-2"></i> API</a>
1213
</div>
1314
</div>
1415
</div>

go.mod

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ require (
1212
github.com/go-audio/wav v1.1.0
1313
github.com/go-skynet/go-bert.cpp v0.0.0-20230716133540-6abe312cded1
1414
github.com/go-skynet/go-llama.cpp v0.0.0-20231009155254-aeba71ee8428
15-
github.com/gofiber/fiber/v2 v2.50.0
15+
github.com/gofiber/fiber/v2 v2.52.0
1616
github.com/gofiber/template/html/v2 v2.1.1
1717
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47
18-
github.com/google/uuid v1.3.1
18+
github.com/google/uuid v1.5.0
1919
github.com/hashicorp/go-multierror v1.1.1
2020
github.com/hpcloud/tail v1.0.0
2121
github.com/imdario/mergo v0.3.16
@@ -32,10 +32,10 @@ require (
3232
github.com/rs/zerolog v1.31.0
3333
github.com/sashabaranov/go-openai v1.20.4
3434
github.com/schollz/progressbar/v3 v3.13.1
35-
github.com/stretchr/testify v1.8.4
35+
github.com/stretchr/testify v1.9.0
3636
github.com/tmc/langchaingo v0.0.0-20231019140956-c636b3da7701
37-
github.com/urfave/cli/v2 v2.25.7
38-
github.com/valyala/fasthttp v1.50.0
37+
github.com/urfave/cli/v2 v2.27.1
38+
github.com/valyala/fasthttp v1.51.0
3939
go.opentelemetry.io/otel v1.19.0
4040
go.opentelemetry.io/otel/exporters/prometheus v0.42.0
4141
go.opentelemetry.io/otel/metric v1.19.0
@@ -59,10 +59,13 @@ require (
5959

6060
require (
6161
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
62+
github.com/KyleBanks/depth v1.2.1 // indirect
6263
github.com/Masterminds/goutils v1.1.1 // indirect
6364
github.com/Masterminds/semver/v3 v3.2.0 // indirect
6465
github.com/Microsoft/go-winio v0.6.0 // indirect
6566
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
67+
github.com/PuerkitoBio/purell v1.2.1 // indirect
68+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
6669
github.com/alecthomas/chroma v0.10.0 // indirect
6770
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
6871
github.com/aymerick/douceur v0.2.0 // indirect
@@ -78,6 +81,11 @@ require (
7881
github.com/docker/go-units v0.4.0 // indirect
7982
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
8083
github.com/go-logr/stdr v1.2.2 // indirect
84+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
85+
github.com/go-openapi/jsonreference v0.21.0 // indirect
86+
github.com/go-openapi/spec v0.21.0 // indirect
87+
github.com/go-openapi/swag v0.23.0 // indirect
88+
github.com/gofiber/swagger v1.0.0 // indirect
8189
github.com/gofiber/template v1.8.3 // indirect
8290
github.com/gofiber/utils v1.1.0 // indirect
8391
github.com/gogo/protobuf v1.3.2 // indirect
@@ -86,8 +94,10 @@ require (
8694
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8795
github.com/gorilla/css v1.0.0 // indirect
8896
github.com/huandu/xstrings v1.3.3 // indirect
97+
github.com/josharian/intern v1.0.0 // indirect
8998
github.com/klauspost/pgzip v1.2.5 // indirect
9099
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
100+
github.com/mailru/easyjson v0.7.7 // indirect
91101
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
92102
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
93103
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
@@ -113,6 +123,8 @@ require (
113123
github.com/shopspring/decimal v1.2.0 // indirect
114124
github.com/sirupsen/logrus v1.8.1 // indirect
115125
github.com/spf13/cast v1.3.1 // indirect
126+
github.com/swaggo/files/v2 v2.0.0 // indirect
127+
github.com/swaggo/swag v1.16.3 // indirect
116128
github.com/ulikunitz/xz v0.5.9 // indirect
117129
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
118130
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
@@ -122,36 +134,37 @@ require (
122134
github.com/yuin/goldmark-emoji v1.0.1 // indirect
123135
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
124136
go.opentelemetry.io/otel/trace v1.19.0 // indirect
125-
golang.org/x/crypto v0.14.0 // indirect
126-
golang.org/x/mod v0.12.0 // indirect
127-
golang.org/x/term v0.13.0 // indirect
137+
golang.org/x/crypto v0.21.0 // indirect
138+
golang.org/x/mod v0.16.0 // indirect
139+
golang.org/x/term v0.18.0 // indirect
128140
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
129141
gopkg.in/fsnotify.v1 v1.4.7 // indirect
130142
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
143+
sigs.k8s.io/yaml v1.4.0 // indirect
131144
)
132145

133146
require (
134147
github.com/andybalholm/brotli v1.0.5 // indirect
135-
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
148+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
136149
github.com/go-audio/audio v1.0.0 // indirect
137150
github.com/go-audio/riff v1.0.0 // indirect
138151
github.com/go-logr/logr v1.2.4 // indirect
139152
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
140153
github.com/google/go-cmp v0.6.0 // indirect
141154
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
142155
github.com/hashicorp/errwrap v1.0.0 // indirect
143-
github.com/klauspost/compress v1.16.7 // indirect
156+
github.com/klauspost/compress v1.17.0 // indirect
144157
github.com/mattn/go-colorable v0.1.13 // indirect
145-
github.com/mattn/go-isatty v0.0.19 // indirect
158+
github.com/mattn/go-isatty v0.0.20 // indirect
146159
github.com/mattn/go-runewidth v0.0.15 // indirect
147160
github.com/mudler/go-piper v0.0.0-20230621222733-56b8a81b4760
148161
github.com/rivo/uniseg v0.2.0 // indirect
149162
github.com/russross/blackfriday/v2 v2.1.0 // indirect
150163
github.com/valyala/bytebufferpool v1.0.0 // indirect
151164
github.com/valyala/tcplisten v1.0.0 // indirect
152-
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
153-
golang.org/x/net v0.17.0 // indirect
154-
golang.org/x/sys v0.17.0 // indirect
155-
golang.org/x/text v0.13.0 // indirect
156-
golang.org/x/tools v0.12.0 // indirect
165+
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
166+
golang.org/x/net v0.22.0 // indirect
167+
golang.org/x/sys v0.18.0 // indirect
168+
golang.org/x/text v0.14.0 // indirect
169+
golang.org/x/tools v0.19.0 // indirect
157170
)

0 commit comments

Comments
 (0)