Spaces:
Sleeping
Sleeping
Download internal/handler/chat.go from wzxwhxcz/zendercode: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/wzxwhxcz/zendercode/resolve/main/internal/handler/chat.go
- Command line
-
hf download hf://spaces/wzxwhxcz/zendercode/internal/handler/chat.go
-
curl -L -o chat.go https://huggingface.co/spaces/wzxwhxcz/zendercode/resolve/main/internal/handler/chat.go
1.06 kB
| package handler | |
| import ( | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| "zencoder-2api/internal/model" | |
| "zencoder-2api/internal/service" | |
| ) | |
| type ChatHandler struct { | |
| svc *service.APIService | |
| } | |
| func NewChatHandler() *ChatHandler { | |
| return &ChatHandler{svc: service.NewAPIService()} | |
| } | |
| func (h *ChatHandler) ChatCompletions(c *gin.Context) { | |
| var req model.ChatCompletionRequest | |
| if err := c.ShouldBindJSON(&req); err != nil { | |
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
| return | |
| } | |
| if req.Stream { | |
| h.handleStream(c, &req) | |
| return | |
| } | |
| resp, err := h.svc.Chat(&req) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | |
| return | |
| } | |
| c.JSON(http.StatusOK, resp) | |
| } | |
| func (h *ChatHandler) handleStream(c *gin.Context, req *model.ChatCompletionRequest) { | |
| c.Writer.Header().Set("Content-Type", "text/event-stream") | |
| c.Writer.Header().Set("Cache-Control", "no-cache") | |
| c.Writer.Header().Set("Connection", "keep-alive") | |
| if err := h.svc.ChatStream(req, c.Writer); err != nil { | |
| c.SSEvent("error", err.Error()) | |
| } | |
| } | |