Update internal/apiserver/router.go
Browse files- internal/apiserver/router.go +62 -0
internal/apiserver/router.go
CHANGED
|
@@ -17,12 +17,74 @@ 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
|
|
|
|
| 17 |
// 添加Bearer Token认证中间件
|
| 18 |
e.Use(middleware.BearerAuth())
|
| 19 |
|
| 20 |
+
// 添加主页路由
|
| 21 |
+
e.GET("/", handleHome)
|
| 22 |
+
|
| 23 |
// ChatGPT 风格的请求转发到 /v1/chat/completions
|
| 24 |
e.POST("/hf/v1/chat/completions", handleChatCompletion)
|
| 25 |
// 获取支持的模型列表
|
| 26 |
e.GET("/hf/v1/models", handleListModels)
|
| 27 |
}
|
| 28 |
|
| 29 |
+
// handleHome 处理主页请求
|
| 30 |
+
func handleHome(c echo.Context) error {
|
| 31 |
+
html := `
|
| 32 |
+
<!DOCTYPE html>
|
| 33 |
+
<html>
|
| 34 |
+
<head>
|
| 35 |
+
<title>Monica Proxy API</title>
|
| 36 |
+
<style>
|
| 37 |
+
body {
|
| 38 |
+
font-family: system-ui, -apple-system, sans-serif;
|
| 39 |
+
max-width: 800px;
|
| 40 |
+
margin: 0 auto;
|
| 41 |
+
padding: 2rem;
|
| 42 |
+
line-height: 1.6;
|
| 43 |
+
}
|
| 44 |
+
code {
|
| 45 |
+
background: #f4f4f4;
|
| 46 |
+
padding: 0.2em 0.4em;
|
| 47 |
+
border-radius: 3px;
|
| 48 |
+
}
|
| 49 |
+
pre {
|
| 50 |
+
background: #f4f4f4;
|
| 51 |
+
padding: 1em;
|
| 52 |
+
border-radius: 5px;
|
| 53 |
+
overflow-x: auto;
|
| 54 |
+
}
|
| 55 |
+
</style>
|
| 56 |
+
</head>
|
| 57 |
+
<body>
|
| 58 |
+
<h1>Monica Proxy API</h1>
|
| 59 |
+
<p>This is a proxy service that converts Monica's service to ChatGPT-compatible API format.</p>
|
| 60 |
+
|
| 61 |
+
<h2>API Endpoints</h2>
|
| 62 |
+
<ul>
|
| 63 |
+
<li><code>POST /hf/v1/chat/completions</code> - Chat completion endpoint</li>
|
| 64 |
+
<li><code>GET /hf/v1/models</code> - List available models</li>
|
| 65 |
+
</ul>
|
| 66 |
+
|
| 67 |
+
<h2>Example Usage</h2>
|
| 68 |
+
<pre>
|
| 69 |
+
curl -X POST https://xxx-xxx.hf.space/hf/v1/chat/completions \
|
| 70 |
+
-H "Authorization: Bearer YOUR_BEARER_TOKEN" \
|
| 71 |
+
-H "Content-Type: application/json" \
|
| 72 |
+
-d '{
|
| 73 |
+
"model": "gpt-4o-mini",
|
| 74 |
+
"messages": [
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": "Hello!"
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"stream": true
|
| 81 |
+
}'</pre>
|
| 82 |
+
</body>
|
| 83 |
+
</html>`
|
| 84 |
+
|
| 85 |
+
return c.HTML(http.StatusOK, html)
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
// handleChatCompletion 接收 ChatGPT 形式的对话请求并转发给 Monica
|
| 89 |
func handleChatCompletion(c echo.Context) error {
|
| 90 |
var req openai.ChatCompletionRequest
|