add: rs-capi

This commit is contained in:
zeke-chin
2024-11-26 11:39:29 +08:00
parent 34ef211bdf
commit d97b157dc2
26 changed files with 4320 additions and 611 deletions

106
main.go
View File

@@ -1,97 +1,35 @@
package main
import (
"log"
"net/http"
"os"
"strings"
"go-capi/handlers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
// 在 main() 函数之前添加以下结构体定义
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("Warning: Error loading .env file")
}
// 加载环境变量
godotenv.Load()
r := gin.Default()
r.POST("/v1/chat/completions", handleChat)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Printf("服务器运行在端口 %s\n", port)
// 配置CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST",},
AllowHeaders: []string{"*"},
AllowCredentials: true,
}))
// 注册路由
r.POST("/v1/chat/completions", handlers.ChatCompletions)
r.GET("/models", handlers.GetModels)
// 获取端口号
// port := os.Getenv("PORT")
port := "3001"
r.Run(":" + port)
}
func handleChat(c *gin.Context) {
var req ChatRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 检查是否为 o1 开头的模型且请求流式输出
if strings.HasPrefix(req.Model, "o1-") && req.Stream {
c.JSON(http.StatusBadRequest, gin.H{"error": "Model not supported stream"})
return
}
// 获取并处理认证token
authHeader := c.GetHeader("Authorization")
authToken := strings.TrimPrefix(authHeader, "Bearer ")
if authToken == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Authorization is required",
})
return
}
// 处理消息
if len(req.Messages) == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Messages should be a non-empty array",
})
return
}
// 处理流式请求
if req.Stream {
handleStreamResponse(c, req)
return
}
// 处理非流式请求
handleNormalResponse(c, req)
}
// 在文件末尾添加这两个新函数
func handleStreamResponse(c *gin.Context, req ChatRequest) {
// TODO: 实现流式响应的逻辑
c.JSON(http.StatusNotImplemented, gin.H{
"error": "Stream response not implemented yet",
})
}
func handleNormalResponse(c *gin.Context, req ChatRequest) {
// TODO: 实现普通响应的逻辑
c.JSON(http.StatusNotImplemented, gin.H{
"error": "Normal response not implemented yet",
})
}
}