From 92a761c0a567bc6fd16545cf3627724ccc1695d5 Mon Sep 17 00:00:00 2001 From: Jacob Payne Date: Mon, 5 Jun 2023 15:25:21 -0700 Subject: [PATCH 1/2] documented remaining endpoints --- api/api.go | 2 +- api/openai.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index e47fe24e0ff5..7d28801e8851 100644 --- a/api/api.go +++ b/api/api.go @@ -3,7 +3,7 @@ package api import ( "errors" - _ "github.com/go-skynet/LocalAI/docs/swagger" + _ "github.com/go-skynet/LocalAI/docs" model "github.com/go-skynet/LocalAI/pkg/model" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" diff --git a/api/openai.go b/api/openai.go index c106be5cf51a..af2db9594681 100644 --- a/api/openai.go +++ b/api/openai.go @@ -440,6 +440,22 @@ func chatEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, thread } } +// OpenAI CHAT API endpoint for editing model parameters +// editEndpoint mimics the /edits endpoint for local models +// @Summary Modify model parameters +// @Description Alter model parameters for text completion models. +// @Tags Chat +// @Accept json +// @Produce json +// @Param model body string true "Define which model to modify." +// @Param instruction body string true "Define the initial prompt for the model." +// @Param input body string true "Initial input prompt for model." +// @Param stop body string true "Define stop words for the model to reply after." +// @Success 200 {object} OpenAIResponse +// @Failure 400 {object} APIError +// @Failure 401 {object} APIError +// @Failure 500 {object} APIError +// @Router /edits [post] func editEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, threads, ctx int, f16 bool) func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error { config, input, err := readConfig(cm, c, loader, debug, threads, ctx, f16) @@ -487,6 +503,17 @@ func editEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, thread } } +// OpenAI CHAT API endpoint for listing loaded models +// listModels mimics the /models endpoint for listing loaded local models +// @Summary Get a list of all currently loaded models +// @Description List all currently loaded models. +// @Tags Chat +// @Produce json +// @Success 200 {object} OpenAIResponse +// @Failure 400 {object} APIError +// @Failure 401 {object} APIError +// @Failure 500 {object} APIError +// @Router /models [get] func listModels(loader *model.ModelLoader, cm ConfigMerger) func(ctx *fiber.Ctx) error { return func(c *fiber.Ctx) error { models, err := loader.ListModels() From 81621de4349cc602bc4c3853080b5de1f30ddfc4 Mon Sep 17 00:00:00 2001 From: Jacob Payne Date: Wed, 7 Jun 2023 09:35:54 -0700 Subject: [PATCH 2/2] swagger init --- docs/docs.go | 394 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 364 ++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 242 ++++++++++++++++++++++++++++ 3 files changed, 1000 insertions(+) create mode 100644 docs/docs.go create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 000000000000..692604ff8867 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,394 @@ +// Code generated by swaggo/swag. DO NOT EDIT. + +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": {}, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/edits": { + "post": { + "description": "Alter model parameters for text completion models.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Modify model parameters", + "parameters": [ + { + "description": "Define which model to modify.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Define the initial prompt for the model.", + "name": "instruction", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Initial input prompt for model.", + "name": "input", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Define stop words for the model to reply after.", + "name": "stop", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/models": { + "get": { + "description": "List all currently loaded models.", + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Get a list of all currently loaded models", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/v1/chat/completions": { + "post": { + "description": "Generates text completions based on the provided prompt and previous messages, using a pre-trained language model.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Generate Model-based Text Completions", + "parameters": [ + { + "description": "The name of the pre-trained language model to use for generating text completions.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The list of previous messages exchanged with the language model, including the user's messages and the model's responses.", + "name": "messages", + "in": "body", + "required": true, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/api.Message" + } + } + }, + { + "default": 0.5, + "description": "The sampling temperature to use when generating text completions. Must be between 0 and 1. Higher values result in more diverse completions, while lower values result in more conservative completions.", + "name": "temperature", + "in": "body", + "schema": { + "type": "number" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/v1/completions": { + "post": { + "description": "Allows to generate completions for a given prompt and model.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "root" + ], + "summary": "Chat completions.", + "parameters": [ + { + "description": "The prompt to generate completions for.", + "name": "prompt", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The ID of the model to use.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The maximum number of tokens to generate in the completion.", + "name": "max_tokens", + "in": "body", + "schema": { + "type": "integer" + } + }, + { + "description": "How many completions to generate for each prompt.", + "name": "n", + "in": "body", + "schema": { + "type": "integer" + } + }, + { + "description": "The sampling temperature to use when generating completions.", + "name": "temperature", + "in": "body", + "schema": { + "type": "number" + } + }, + { + "description": "Sequence where the API will stop generating further tokens", + "name": "stop", + "in": "body", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + } + }, + "definitions": { + "api.APIError": { + "description": "Error returned by the API", + "type": "object", + "properties": { + "code": {}, + "message": { + "type": "string" + }, + "param": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, + "api.Choice": { + "type": "object", + "properties": { + "delta": { + "$ref": "#/definitions/api.Message" + }, + "finish_reason": { + "type": "string" + }, + "index": { + "type": "integer" + }, + "message": { + "$ref": "#/definitions/api.Message" + }, + "text": { + "type": "string" + } + } + }, + "api.Message": { + "description": "Message with a content and a role", + "type": "object", + "properties": { + "content": { + "type": "string" + }, + "role": { + "type": "string" + } + } + }, + "api.OpenAIResponse": { + "type": "object", + "properties": { + "choices": { + "type": "array", + "items": { + "$ref": "#/definitions/api.Choice" + } + }, + "created": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "model": { + "type": "string" + }, + "object": { + "type": "string" + }, + "usage": { + "$ref": "#/definitions/api.OpenAIUsage" + } + } + }, + "api.OpenAIUsage": { + "type": "object", + "properties": { + "completion_tokens": { + "type": "integer" + }, + "prompt_tokens": { + "type": "integer" + }, + "total_tokens": { + "type": "integer" + } + } + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "", + Host: "", + BasePath: "", + Schemes: []string{}, + Title: "", + Description: "", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 000000000000..397524d3a460 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,364 @@ +{ + "swagger": "2.0", + "info": { + "contact": {} + }, + "paths": { + "/edits": { + "post": { + "description": "Alter model parameters for text completion models.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Modify model parameters", + "parameters": [ + { + "description": "Define which model to modify.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Define the initial prompt for the model.", + "name": "instruction", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Initial input prompt for model.", + "name": "input", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Define stop words for the model to reply after.", + "name": "stop", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/models": { + "get": { + "description": "List all currently loaded models.", + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Get a list of all currently loaded models", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/v1/chat/completions": { + "post": { + "description": "Generates text completions based on the provided prompt and previous messages, using a pre-trained language model.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Generate Model-based Text Completions", + "parameters": [ + { + "description": "The name of the pre-trained language model to use for generating text completions.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The list of previous messages exchanged with the language model, including the user's messages and the model's responses.", + "name": "messages", + "in": "body", + "required": true, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/api.Message" + } + } + }, + { + "default": 0.5, + "description": "The sampling temperature to use when generating text completions. Must be between 0 and 1. Higher values result in more diverse completions, while lower values result in more conservative completions.", + "name": "temperature", + "in": "body", + "schema": { + "type": "number" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.OpenAIResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/api.APIError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/api.APIError" + } + } + } + } + }, + "/v1/completions": { + "post": { + "description": "Allows to generate completions for a given prompt and model.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "root" + ], + "summary": "Chat completions.", + "parameters": [ + { + "description": "The prompt to generate completions for.", + "name": "prompt", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The ID of the model to use.", + "name": "model", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "The maximum number of tokens to generate in the completion.", + "name": "max_tokens", + "in": "body", + "schema": { + "type": "integer" + } + }, + { + "description": "How many completions to generate for each prompt.", + "name": "n", + "in": "body", + "schema": { + "type": "integer" + } + }, + { + "description": "The sampling temperature to use when generating completions.", + "name": "temperature", + "in": "body", + "schema": { + "type": "number" + } + }, + { + "description": "Sequence where the API will stop generating further tokens", + "name": "stop", + "in": "body", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + } + }, + "definitions": { + "api.APIError": { + "description": "Error returned by the API", + "type": "object", + "properties": { + "code": {}, + "message": { + "type": "string" + }, + "param": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, + "api.Choice": { + "type": "object", + "properties": { + "delta": { + "$ref": "#/definitions/api.Message" + }, + "finish_reason": { + "type": "string" + }, + "index": { + "type": "integer" + }, + "message": { + "$ref": "#/definitions/api.Message" + }, + "text": { + "type": "string" + } + } + }, + "api.Message": { + "description": "Message with a content and a role", + "type": "object", + "properties": { + "content": { + "type": "string" + }, + "role": { + "type": "string" + } + } + }, + "api.OpenAIResponse": { + "type": "object", + "properties": { + "choices": { + "type": "array", + "items": { + "$ref": "#/definitions/api.Choice" + } + }, + "created": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "model": { + "type": "string" + }, + "object": { + "type": "string" + }, + "usage": { + "$ref": "#/definitions/api.OpenAIUsage" + } + } + }, + "api.OpenAIUsage": { + "type": "object", + "properties": { + "completion_tokens": { + "type": "integer" + }, + "prompt_tokens": { + "type": "integer" + }, + "total_tokens": { + "type": "integer" + } + } + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 000000000000..51bbecfa5193 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,242 @@ +definitions: + api.APIError: + description: Error returned by the API + properties: + code: {} + message: + type: string + param: + type: string + type: + type: string + type: object + api.Choice: + properties: + delta: + $ref: '#/definitions/api.Message' + finish_reason: + type: string + index: + type: integer + message: + $ref: '#/definitions/api.Message' + text: + type: string + type: object + api.Message: + description: Message with a content and a role + properties: + content: + type: string + role: + type: string + type: object + api.OpenAIResponse: + properties: + choices: + items: + $ref: '#/definitions/api.Choice' + type: array + created: + type: integer + id: + type: string + model: + type: string + object: + type: string + usage: + $ref: '#/definitions/api.OpenAIUsage' + type: object + api.OpenAIUsage: + properties: + completion_tokens: + type: integer + prompt_tokens: + type: integer + total_tokens: + type: integer + type: object +info: + contact: {} +paths: + /edits: + post: + consumes: + - application/json + description: Alter model parameters for text completion models. + parameters: + - description: Define which model to modify. + in: body + name: model + required: true + schema: + type: string + - description: Define the initial prompt for the model. + in: body + name: instruction + required: true + schema: + type: string + - description: Initial input prompt for model. + in: body + name: input + required: true + schema: + type: string + - description: Define stop words for the model to reply after. + in: body + name: stop + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/api.OpenAIResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.APIError' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/api.APIError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/api.APIError' + summary: Modify model parameters + tags: + - Chat + /models: + get: + description: List all currently loaded models. + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/api.OpenAIResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.APIError' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/api.APIError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/api.APIError' + summary: Get a list of all currently loaded models + tags: + - Chat + /v1/chat/completions: + post: + consumes: + - application/json + description: Generates text completions based on the provided prompt and previous + messages, using a pre-trained language model. + parameters: + - description: The name of the pre-trained language model to use for generating + text completions. + in: body + name: model + required: true + schema: + type: string + - description: The list of previous messages exchanged with the language model, + including the user's messages and the model's responses. + in: body + name: messages + required: true + schema: + items: + $ref: '#/definitions/api.Message' + type: array + - default: 0.5 + description: The sampling temperature to use when generating text completions. + Must be between 0 and 1. Higher values result in more diverse completions, + while lower values result in more conservative completions. + in: body + name: temperature + schema: + type: number + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/api.OpenAIResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/api.APIError' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/api.APIError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/api.APIError' + summary: Generate Model-based Text Completions + tags: + - Chat + /v1/completions: + post: + consumes: + - application/json + description: Allows to generate completions for a given prompt and model. + parameters: + - description: The prompt to generate completions for. + in: body + name: prompt + required: true + schema: + type: string + - description: The ID of the model to use. + in: body + name: model + required: true + schema: + type: string + - description: The maximum number of tokens to generate in the completion. + in: body + name: max_tokens + schema: + type: integer + - description: How many completions to generate for each prompt. + in: body + name: "n" + schema: + type: integer + - description: The sampling temperature to use when generating completions. + in: body + name: temperature + schema: + type: number + - description: Sequence where the API will stop generating further tokens + in: body + name: stop + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + additionalProperties: true + type: object + summary: Chat completions. + tags: + - root +swagger: "2.0"