|
@@ -30,6 +30,7 @@ import (
|
|
|
"github.com/ollama/ollama/api"
|
|
|
"github.com/ollama/ollama/discover"
|
|
|
"github.com/ollama/ollama/envconfig"
|
|
|
+ "github.com/ollama/ollama/llama"
|
|
|
"github.com/ollama/ollama/llm"
|
|
|
"github.com/ollama/ollama/openai"
|
|
|
"github.com/ollama/ollama/parser"
|
|
@@ -569,15 +570,43 @@ func (s *Server) TokenizeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- runner, _, _, err := s.scheduleRunner(r.Context(), req.Model, []Capability{}, nil, req.KeepAlive)
|
|
|
+ if req.Model == "" {
|
|
|
+ http.Error(w, "missing `model` for tokenization", http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ name := model.ParseName(req.Model)
|
|
|
+ if !name.IsValid() {
|
|
|
+ http.Error(w, fmt.Sprintf("model name `%q` is invalid", req.Model), http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ name, err := getExistingName(name)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, fmt.Sprintf("model `%s` not found", req.Model), http.StatusNotFound)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get local model path
|
|
|
+ modelPath, err := GetModel(name.String())
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, fmt.Sprintf("model `%s` not found", req.Model), http.StatusNotFound)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ model, err := llama.LoadModelFromFile(modelPath.ModelPath, llama.ModelParams{
|
|
|
+ VocabOnly: true,
|
|
|
+ UseMmap: true,
|
|
|
+ })
|
|
|
if err != nil {
|
|
|
- http.Error(w, fmt.Sprintf("model '%s' not found", req.Model), http.StatusNotFound)
|
|
|
+ http.Error(w, fmt.Sprintf("failed to load model: %v", err), http.StatusInternalServerError)
|
|
|
return
|
|
|
}
|
|
|
+ defer llama.FreeModel(model)
|
|
|
|
|
|
- tokens, err := runner.Tokenize(r.Context(), req.Text)
|
|
|
+ // Tokenize the text
|
|
|
+ tokens, err := model.Tokenize(req.Text, false, true)
|
|
|
if err != nil {
|
|
|
- http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ http.Error(w, fmt.Sprintf("failed to tokenize text: %v", err), http.StatusInternalServerError)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -611,17 +640,43 @@ func (s *Server) DetokenizeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- runner, _, _, err := s.scheduleRunner(r.Context(), req.Model, []Capability{}, nil, req.KeepAlive)
|
|
|
+ if req.Model == "" {
|
|
|
+ http.Error(w, "missing `model` for detokenization", http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ name := model.ParseName(req.Model)
|
|
|
+ if !name.IsValid() {
|
|
|
+ http.Error(w, fmt.Sprintf("model name `%q` is invalid", req.Model), http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ name, err := getExistingName(name)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, fmt.Sprintf("model `%s` not found", req.Model), http.StatusNotFound)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get local model path
|
|
|
+ modelPath, err := GetModel(name.String())
|
|
|
if err != nil {
|
|
|
- http.Error(w, fmt.Sprintf("model '%s' not found", req.Model), http.StatusNotFound)
|
|
|
+ http.Error(w, fmt.Sprintf("model `%s` not found", req.Model), http.StatusNotFound)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- text, err := runner.Detokenize(r.Context(), req.Tokens)
|
|
|
+ model, err := llama.LoadModelFromFile(modelPath.ModelPath, llama.ModelParams{
|
|
|
+ VocabOnly: true,
|
|
|
+ UseMmap: true,
|
|
|
+ })
|
|
|
if err != nil {
|
|
|
- http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ http.Error(w, fmt.Sprintf("failed to load model: %v", err), http.StatusInternalServerError)
|
|
|
return
|
|
|
}
|
|
|
+ defer llama.FreeModel(model)
|
|
|
+
|
|
|
+ var text string
|
|
|
+ for _, token := range req.Tokens {
|
|
|
+ text += model.TokenToPiece(token)
|
|
|
+ }
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
if err := json.NewEncoder(w).Encode(api.DetokenizeResponse{
|