|
@@ -548,6 +548,56 @@ func (s *Server) EmbeddingsHandler(c *gin.Context) {
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
}
|
|
|
|
|
|
+func (s *Server) TokenizeHandler(c *gin.Context) {
|
|
|
+ var req api.TokenizeRequest
|
|
|
+ if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
|
|
|
+ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
|
|
|
+ return
|
|
|
+ } else if err != nil {
|
|
|
+ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ r, _, _, err := s.scheduleRunner(c.Request.Context(), req.Model, []Capability{}, req.Options, req.KeepAlive)
|
|
|
+ if err != nil {
|
|
|
+ handleScheduleError(c, req.Model, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ tokens, err := r.Tokenize(c.Request.Context(), req.Prompt)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ c.JSON(http.StatusOK, api.TokenizeResponse{Model: req.Model, Tokens: tokens})
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Server) DetokenizeHandler(c *gin.Context) {
|
|
|
+ var req api.DetokenizeRequest
|
|
|
+ if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
|
|
|
+ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
|
|
|
+ return
|
|
|
+ } else if err != nil {
|
|
|
+ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ r, _, _, err := s.scheduleRunner(c.Request.Context(), req.Model, []Capability{}, req.Options, req.KeepAlive)
|
|
|
+ if err != nil {
|
|
|
+ handleScheduleError(c, req.Model, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ text, err := r.Detokenize(c.Request.Context(), req.Tokens)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ c.JSON(http.StatusOK, api.DetokenizeResponse{Model: req.Model, Text: text})
|
|
|
+}
|
|
|
+
|
|
|
func (s *Server) PullHandler(c *gin.Context) {
|
|
|
var req api.PullRequest
|
|
|
err := c.ShouldBindJSON(&req)
|
|
@@ -1214,6 +1264,8 @@ func (s *Server) GenerateRoutes() http.Handler {
|
|
|
r.POST("/api/chat", s.ChatHandler)
|
|
|
r.POST("/api/embed", s.EmbedHandler)
|
|
|
r.POST("/api/embeddings", s.EmbeddingsHandler)
|
|
|
+ r.POST("/api/tokenize", s.TokenizeHandler)
|
|
|
+ r.POST("/api/detokenize", s.DetokenizeHandler)
|
|
|
r.POST("/api/create", s.CreateHandler)
|
|
|
r.POST("/api/push", s.PushHandler)
|
|
|
r.POST("/api/copy", s.CopyHandler)
|