2024-04-20 23:19:57 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-10-02 09:16:11 +00:00
|
|
|
"github.com/gofiber/websocket/v2"
|
2024-12-08 12:50:33 +00:00
|
|
|
"github.com/mudler/LocalAI/core/application"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/http/endpoints/localai"
|
|
|
|
"github.com/mudler/LocalAI/core/http/endpoints/openai"
|
2024-04-20 23:19:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterOpenAIRoutes(app *fiber.App,
|
2024-12-08 12:50:33 +00:00
|
|
|
application *application.Application) {
|
2024-04-20 23:19:57 +00:00
|
|
|
// openAI compatible API endpoint
|
|
|
|
|
2024-10-02 09:16:11 +00:00
|
|
|
// realtime
|
|
|
|
app.Get("/v1/realtime", websocket.New(openai.RegisterRealtime(cl, ml, appConfig)))
|
|
|
|
|
2024-04-20 23:19:57 +00:00
|
|
|
// chat
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/chat/completions",
|
|
|
|
openai.ChatEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
app.Post("/chat/completions",
|
|
|
|
openai.ChatEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// edit
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/edits",
|
|
|
|
openai.EditEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
app.Post("/edits",
|
|
|
|
openai.EditEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// assistant
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Get("/v1/assistants", openai.ListAssistantsEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/assistants", openai.ListAssistantsEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/v1/assistants", openai.CreateAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/assistants", openai.CreateAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/v1/assistants/:assistant_id", openai.DeleteAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/assistants/:assistant_id", openai.DeleteAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/assistants/:assistant_id", openai.GetAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/assistants/:assistant_id", openai.GetAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/v1/assistants/:assistant_id", openai.ModifyAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/assistants/:assistant_id", openai.ModifyAssistantEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/assistants/:assistant_id/files", openai.ListAssistantFilesEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/assistants/:assistant_id/files", openai.ListAssistantFilesEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/v1/assistants/:assistant_id/files", openai.CreateAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/assistants/:assistant_id/files", openai.CreateAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/v1/assistants/:assistant_id/files/:file_id", openai.DeleteAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/assistants/:assistant_id/files/:file_id", openai.DeleteAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/assistants/:assistant_id/files/:file_id", openai.GetAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/assistants/:assistant_id/files/:file_id", openai.GetAssistantFileEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// files
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/files", openai.UploadFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/files", openai.UploadFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/files", openai.ListFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/files", openai.ListFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/files/:file_id", openai.GetFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/files/:file_id", openai.GetFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/v1/files/:file_id", openai.DeleteFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Delete("/files/:file_id", openai.DeleteFilesEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/v1/files/:file_id/content", openai.GetFilesContentsEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
|
|
|
app.Get("/files/:file_id/content", openai.GetFilesContentsEndpoint(application.BackendLoader(), application.ApplicationConfig()))
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// completion
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/completions",
|
|
|
|
openai.CompletionEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
app.Post("/completions",
|
|
|
|
openai.CompletionEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
app.Post("/v1/engines/:model/completions",
|
|
|
|
openai.CompletionEndpoint(
|
|
|
|
application.BackendLoader(),
|
|
|
|
application.ModelLoader(),
|
|
|
|
application.TemplatesEvaluator(),
|
|
|
|
application.ApplicationConfig(),
|
|
|
|
),
|
|
|
|
)
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// embeddings
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/embeddings", openai.EmbeddingsEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/embeddings", openai.EmbeddingsEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/v1/engines/:model/embeddings", openai.EmbeddingsEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// audio
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/audio/transcriptions", openai.TranscriptEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
|
|
|
app.Post("/v1/audio/speech", localai.TTSEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
2024-04-20 23:19:57 +00:00
|
|
|
|
|
|
|
// images
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Post("/v1/images/generations", openai.ImageEndpoint(application.BackendLoader(), application.ModelLoader(), application.ApplicationConfig()))
|
2024-04-20 23:19:57 +00:00
|
|
|
|
2024-12-08 12:50:33 +00:00
|
|
|
if application.ApplicationConfig().ImageDir != "" {
|
|
|
|
app.Static("/generated-images", application.ApplicationConfig().ImageDir)
|
2024-04-20 23:19:57 +00:00
|
|
|
}
|
|
|
|
|
2024-12-08 12:50:33 +00:00
|
|
|
if application.ApplicationConfig().AudioDir != "" {
|
|
|
|
app.Static("/generated-audio", application.ApplicationConfig().AudioDir)
|
2024-04-20 23:19:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 13:28:39 +00:00
|
|
|
// List models
|
2024-12-08 12:50:33 +00:00
|
|
|
app.Get("/v1/models", openai.ListModelsEndpoint(application.BackendLoader(), application.ModelLoader()))
|
|
|
|
app.Get("/models", openai.ListModelsEndpoint(application.BackendLoader(), application.ModelLoader()))
|
2024-04-20 23:19:57 +00:00
|
|
|
}
|