Create internal/apiserver/router.go
Browse files- internal/apiserver/router.go +83 -0
internal/apiserver/router.go
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package apiserver
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"encoding/json"
|
| 5 |
+
"log"
|
| 6 |
+
"monica-proxy/internal/middleware"
|
| 7 |
+
"monica-proxy/internal/monica"
|
| 8 |
+
"monica-proxy/internal/types"
|
| 9 |
+
"net/http"
|
| 10 |
+
|
| 11 |
+
"github.com/labstack/echo/v4"
|
| 12 |
+
"github.com/sashabaranov/go-openai"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
// RegisterRoutes 注册 Echo 路由
|
| 16 |
+
func RegisterRoutes(e *echo.Echo) {
|
| 17 |
+
// 添加Bearer Token认证中间件
|
| 18 |
+
e.Use(middleware.BearerAuth())
|
| 19 |
+
|
| 20 |
+
// ChatGPT 风格的请求转发到 /v1/chat/completions
|
| 21 |
+
e.POST("/hf/v1/chat/completions", handleChatCompletion)
|
| 22 |
+
// 获取支持的模型列表
|
| 23 |
+
e.GET("/hf/v1/models", handleListModels)
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// handleChatCompletion 接收 ChatGPT 形式的对话请求并转发给 Monica
|
| 27 |
+
func handleChatCompletion(c echo.Context) error {
|
| 28 |
+
var req openai.ChatCompletionRequest
|
| 29 |
+
if err := c.Bind(&req); err != nil {
|
| 30 |
+
return c.JSON(http.StatusBadRequest, map[string]interface{}{
|
| 31 |
+
"error": "Invalid request payload",
|
| 32 |
+
})
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// 检查请求是否包含消息
|
| 36 |
+
if len(req.Messages) == 0 {
|
| 37 |
+
return c.JSON(http.StatusBadRequest, map[string]interface{}{
|
| 38 |
+
"error": "No messages found",
|
| 39 |
+
})
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
marshalIndent, err := json.MarshalIndent(req, "", " ")
|
| 43 |
+
if err != nil {
|
| 44 |
+
return err
|
| 45 |
+
}
|
| 46 |
+
log.Printf("Received completion request: \n%s\n", marshalIndent)
|
| 47 |
+
// 将 ChatGPTRequest 转换为 MonicaRequest
|
| 48 |
+
monicaReq, err := types.ChatGPTToMonica(req)
|
| 49 |
+
if err != nil {
|
| 50 |
+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
| 51 |
+
"error": err.Error(),
|
| 52 |
+
})
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// 调用 Monica 并获取 SSE Stream
|
| 56 |
+
stream, err := monica.SendMonicaRequest(c.Request().Context(), monicaReq)
|
| 57 |
+
if err != nil {
|
| 58 |
+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
| 59 |
+
"error": err.Error(),
|
| 60 |
+
})
|
| 61 |
+
}
|
| 62 |
+
// Resty 不会自动关闭 Body,需要我们自己来处理
|
| 63 |
+
defer stream.RawBody().Close()
|
| 64 |
+
|
| 65 |
+
// 这里直接用流式方式把 SSE 数据返回
|
| 66 |
+
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
|
| 67 |
+
c.Response().Header().Set("Cache-Control", "no-cache")
|
| 68 |
+
c.Response().Header().Set("Transfer-Encoding", "chunked")
|
| 69 |
+
c.Response().WriteHeader(http.StatusOK)
|
| 70 |
+
|
| 71 |
+
// 将 Monica 的 SSE 数据逐行读出,再以 SSE 格式返回给调用方
|
| 72 |
+
if err := monica.StreamMonicaSSEToClient(req.Model, c.Response().Writer, stream.RawBody()); err != nil {
|
| 73 |
+
return err
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return nil
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// handleListModels 返回支持的模型列表
|
| 80 |
+
func handleListModels(c echo.Context) error {
|
| 81 |
+
models := types.GetSupportedModels()
|
| 82 |
+
return c.JSON(http.StatusOK, models)
|
| 83 |
+
}
|