-
Notifications
You must be signed in to change notification settings - Fork 541
Fix: Support Chat Template Tokenization with vLLM Parameters in Prefix Cache Router #2002
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
+430
−14
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a89b986
fix: support chat template tokenization with vLLM parameters in prefi…
pengfei913 af90f5b
Fix: add some unittest
pengfei913 05a5c4f
refactor: improve code clarity and efficiency in tokenization
pengfei913 0f0e8f3
fix: add error checking in types_test.go and use 'any' type
pengfei913 3f06b2a
docs: use file URL instead of line numbers in references
pengfei913 2642472
fix: add nil check for message content in buildTokenizeInputFromChatR…
pengfei913 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| Copyright 2025 The Aibrix Team. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "github.com/openai/openai-go" | ||
| ) | ||
|
|
||
| // ChatCompletionRequest extends OpenAI's ChatCompletionNewParams with vLLM-specific parameters | ||
| // Reference: https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/openai/chat_completion/protocol.py | ||
| type ChatCompletionRequest struct { | ||
| openai.ChatCompletionNewParams | ||
|
|
||
| // vLLM-specific parameters | ||
|
|
||
| // AddGenerationPrompt controls whether to add the generation prompt to the chat template. | ||
| // Default: true (vLLM default) | ||
| // Reference: protocol.py line 217-224 | ||
penfree marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| AddGenerationPrompt *bool `json:"add_generation_prompt,omitempty"` | ||
|
|
||
| // AddSpecialTokens controls whether to add special tokens (e.g. BOS) on top of | ||
| // what is added by the chat template. | ||
| // Default: false (vLLM default) - chat template handles special tokens | ||
| // Reference: protocol.py line 235-244 | ||
| AddSpecialTokens *bool `json:"add_special_tokens,omitempty"` | ||
|
|
||
| // ReturnTokenStrings controls whether to return token strings in tokenization results. | ||
| // This is used for debugging and verification purposes. | ||
| // Default: false | ||
| ReturnTokenStrings *bool `json:"return_token_strs,omitempty"` | ||
| } | ||
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,135 @@ | ||
| /* | ||
| Copyright 2025 The Aibrix Team. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package tokenizer | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/bytedance/sonic" | ||
| ) | ||
|
|
||
| func TestSonicSerializationWithRawMessage(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| message ChatMessage | ||
| }{ | ||
| { | ||
| name: "string content", | ||
| message: ChatMessage{ | ||
| Role: "user", | ||
| Content: json.RawMessage(`"Hello world"`), | ||
| }, | ||
| }, | ||
| { | ||
| name: "multimodal array content", | ||
| message: ChatMessage{ | ||
| Role: "user", | ||
| Content: json.RawMessage(`[ | ||
| {"type":"text","text":"What's in this image?"}, | ||
| {"type":"image_url","image_url":{"url":"https://example.com/img.jpg"}} | ||
| ]`), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Test sonic.Marshal | ||
| data, err := sonic.Marshal(tt.message) | ||
| if err != nil { | ||
| t.Fatalf("sonic.Marshal failed: %v", err) | ||
| } | ||
|
|
||
| // Verify it can be unmarshaled back | ||
| var result ChatMessage | ||
| if err := json.Unmarshal(data, &result); err != nil { | ||
| t.Fatalf("json.Unmarshal failed: %v", err) | ||
| } | ||
|
|
||
| if result.Role != tt.message.Role { | ||
| t.Errorf("Role mismatch: got %v, want %v", result.Role, tt.message.Role) | ||
| } | ||
|
|
||
| // Content should be preserved as-is | ||
| if string(result.Content) != string(tt.message.Content) { | ||
| t.Errorf("Content mismatch:\ngot: %s\nwant: %s", string(result.Content), string(tt.message.Content)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestVLLMRequestSerialization(t *testing.T) { | ||
| // Simulate what happens in vLLM adapter | ||
| messages := []ChatMessage{ | ||
| { | ||
| Role: "user", | ||
| Content: json.RawMessage(`[{"type":"text","text":"test"},{"type":"image_url","image_url":{"url":"http://example.com/img.jpg"}}]`), | ||
| }, | ||
| } | ||
|
|
||
| addSpecialTokens := false | ||
| addGenerationPrompt := true | ||
| returnTokenStrs := false | ||
|
|
||
| request := struct { | ||
| Messages []ChatMessage `json:"messages"` | ||
| AddSpecialTokens *bool `json:"add_special_tokens,omitempty"` | ||
| AddGenerationPrompt *bool `json:"add_generation_prompt,omitempty"` | ||
| ReturnTokenStrs *bool `json:"return_token_strs,omitempty"` | ||
| }{ | ||
| Messages: messages, | ||
| AddSpecialTokens: &addSpecialTokens, | ||
| AddGenerationPrompt: &addGenerationPrompt, | ||
| ReturnTokenStrs: &returnTokenStrs, | ||
| } | ||
|
|
||
| // This is what the HTTP client does | ||
| jsonData, err := sonic.Marshal(request) | ||
| if err != nil { | ||
| t.Fatalf("Failed to marshal: %v", err) | ||
| } | ||
|
|
||
| t.Logf("Serialized request:\n%s", string(jsonData)) | ||
|
|
||
| // Verify the output is valid JSON and content is preserved | ||
| var result map[string]interface{} | ||
| if err := json.Unmarshal(jsonData, &result); err != nil { | ||
| t.Fatalf("Failed to unmarshal result: %v", err) | ||
| } | ||
|
|
||
| // Check messages array | ||
| messagesArray, ok := result["messages"].([]interface{}) | ||
| if !ok { | ||
| t.Fatal("messages should be an array") | ||
| } | ||
|
|
||
| if len(messagesArray) != 1 { | ||
| t.Fatalf("Expected 1 message, got %d", len(messagesArray)) | ||
| } | ||
|
|
||
| // Check first message has content as array | ||
| firstMsg := messagesArray[0].(map[string]interface{}) | ||
| content, ok := firstMsg["content"].([]interface{}) | ||
| if !ok { | ||
| t.Fatal("content should be an array for multimodal message") | ||
| } | ||
|
|
||
| if len(content) != 2 { | ||
| t.Fatalf("Expected 2 content parts, got %d", len(content)) | ||
| } | ||
| } |
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.
the project adopts
github.com/bytedance/sonicoverencoding/jsonto optimize the performance, let's move to sonic if the scenario is supported.