-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(api): add /v1/detokenize endpoint #9620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e98a81
feat(api): add /v1/detokenize endpoint
Dennisadira 19a7dee
test(e2e): add mock backend tests for /v1/detokenize
Dennisadira 8db6ef6
fix(kokoros): implement detokenize in the Rust backend service
Dennisadira 29c9327
Merge branch 'master' into feat/detokenize-endpoint
localai-org-maint-bot 72d88d5
Merge branch 'master' into feat/detokenize-endpoint
localai-org-maint-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package backend | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/mudler/LocalAI/core/config" | ||
| "github.com/mudler/LocalAI/core/schema" | ||
| "github.com/mudler/LocalAI/core/trace" | ||
| "github.com/mudler/LocalAI/pkg/grpc" | ||
| pb "github.com/mudler/LocalAI/pkg/grpc/proto" | ||
| "github.com/mudler/LocalAI/pkg/model" | ||
| ) | ||
|
|
||
| func ModelDetokenize(tokens []int32, loader *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (schema.DetokenizeResponse, error) { | ||
|
|
||
| var inferenceModel grpc.Backend | ||
| var err error | ||
|
|
||
| opts := ModelOptions(modelConfig, appConfig) | ||
| inferenceModel, err = loader.Load(opts...) | ||
| if err != nil { | ||
| recordModelLoadFailure(appConfig, modelConfig.Name, modelConfig.Backend, err, nil) | ||
| return schema.DetokenizeResponse{}, err | ||
| } | ||
|
|
||
| var startTime time.Time | ||
| if appConfig.EnableTracing { | ||
| trace.InitBackendTracingIfEnabled(appConfig.TracingMaxItems, appConfig.TracingMaxBodyBytes) | ||
| startTime = time.Now() | ||
| } | ||
|
|
||
| resp, err := inferenceModel.Detokenize(appConfig.Context, &pb.DetokenizeRequest{Tokens: tokens}) | ||
|
|
||
| if appConfig.EnableTracing { | ||
| errStr := "" | ||
| if err != nil { | ||
| errStr = err.Error() | ||
| } | ||
|
|
||
| content := "" | ||
| if resp != nil { | ||
| content = resp.Content | ||
| } | ||
|
|
||
| trace.RecordBackendTrace(trace.BackendTrace{ | ||
| Timestamp: startTime, | ||
| Duration: time.Since(startTime), | ||
| Type: trace.BackendTraceTokenize, | ||
| ModelName: modelConfig.Name, | ||
| Backend: modelConfig.Backend, | ||
| Summary: trace.TruncateString(content, 200), | ||
| Error: errStr, | ||
| Data: map[string]any{ | ||
| "token_count": len(tokens), | ||
| "output_text": trace.TruncateString(content, 1000), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return schema.DetokenizeResponse{}, err | ||
| } | ||
|
|
||
| return schema.DetokenizeResponse{ | ||
| Content: resp.Content, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package localai | ||
|
|
||
| import ( | ||
| "github.com/labstack/echo/v4" | ||
| "github.com/mudler/LocalAI/core/backend" | ||
| "github.com/mudler/LocalAI/core/config" | ||
| "github.com/mudler/LocalAI/core/http/middleware" | ||
| "github.com/mudler/LocalAI/core/schema" | ||
| "github.com/mudler/LocalAI/pkg/model" | ||
| ) | ||
|
|
||
| // DetokenizeEndpoint exposes a REST API to convert token IDs back to text. | ||
| // @Summary Detokenize the input. | ||
| // @Tags tokenize | ||
| // @Param request body schema.DetokenizeRequest true "Request" | ||
| // @Success 200 {object} schema.DetokenizeResponse "Response" | ||
| // @Router /v1/detokenize [post] | ||
| func DetokenizeEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| input, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.DetokenizeRequest) | ||
| if !ok || input.Model == "" { | ||
| return echo.ErrBadRequest | ||
| } | ||
|
|
||
| cfg, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig) | ||
| if !ok || cfg == nil { | ||
| return echo.ErrBadRequest | ||
| } | ||
|
|
||
| resp, err := backend.ModelDetokenize(input.Tokens, ml, *cfg, appConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return c.JSON(200, resp) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this requires a test addition to our e2e-backend test suite where we exercise a mocked backend via api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added a
Detokenizemethod to the mock gRPC backend and two e2e tests in the MockBackend suite (0024a9c): one that POSTs known token IDs and asserts a non-emptycontentresponse, and a round-trip that tokenizes first then detokenizes the returned IDs.