|
package apiserver |
|
|
|
import ( |
|
"encoding/json" |
|
"log" |
|
"monica-proxy/internal/middleware" |
|
"monica-proxy/internal/monica" |
|
"monica-proxy/internal/types" |
|
"net/http" |
|
|
|
"github.com/labstack/echo/v4" |
|
"github.com/sashabaranov/go-openai" |
|
) |
|
|
|
|
|
func RegisterRoutes(e *echo.Echo) { |
|
|
|
e.Use(middleware.BearerAuth()) |
|
|
|
|
|
e.POST("/hf/v1/chat/completions", handleChatCompletion) |
|
|
|
e.GET("/hf/v1/models", handleListModels) |
|
} |
|
|
|
|
|
func handleChatCompletion(c echo.Context) error { |
|
var req openai.ChatCompletionRequest |
|
if err := c.Bind(&req); err != nil { |
|
return c.JSON(http.StatusBadRequest, map[string]interface{}{ |
|
"error": "Invalid request payload", |
|
}) |
|
} |
|
|
|
|
|
if len(req.Messages) == 0 { |
|
return c.JSON(http.StatusBadRequest, map[string]interface{}{ |
|
"error": "No messages found", |
|
}) |
|
} |
|
|
|
marshalIndent, err := json.MarshalIndent(req, "", " ") |
|
if err != nil { |
|
return err |
|
} |
|
log.Printf("Received completion request: \n%s\n", marshalIndent) |
|
|
|
monicaReq, err := types.ChatGPTToMonica(req) |
|
if err != nil { |
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{ |
|
"error": err.Error(), |
|
}) |
|
} |
|
|
|
|
|
stream, err := monica.SendMonicaRequest(c.Request().Context(), monicaReq) |
|
if err != nil { |
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{ |
|
"error": err.Error(), |
|
}) |
|
} |
|
|
|
defer stream.RawBody().Close() |
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream") |
|
c.Response().Header().Set("Cache-Control", "no-cache") |
|
c.Response().Header().Set("Transfer-Encoding", "chunked") |
|
c.Response().WriteHeader(http.StatusOK) |
|
|
|
|
|
if err := monica.StreamMonicaSSEToClient(req.Model, c.Response().Writer, stream.RawBody()); err != nil { |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|
|
|
|
func handleListModels(c echo.Context) error { |
|
models := types.GetSupportedModels() |
|
return c.JSON(http.StatusOK, models) |
|
} |
|
|