-
Notifications
You must be signed in to change notification settings - Fork 592
feat(routing): Implement in-tree keyword-based routing #538
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac50816
fix:No module named 'tests' error (#515)
OneZero-Y 66a3c16
webiste: add scroll top btn (#535)
yuluo-yx 3acf420
feat(routing): Implement in-tree keyword-based routing
srini-abhiram 373df66
feat(routing): Enhance keyword-based routing with logging and benchmarks
srini-abhiram 68d1c3b
chore: Improve keyword classifier error handling and tests
srini-abhiram 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
54 changes: 54 additions & 0 deletions
54
src/semantic-router/pkg/utils/classification/benchmark_test.go
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,54 @@ | ||
| package classification | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/vllm-project/semantic-router/src/semantic-router/pkg/config" | ||
| ) | ||
|
|
||
| func BenchmarkKeywordClassifier(b *testing.B) { | ||
| rules := []config.KeywordRule{ | ||
| { | ||
| Category: "test-category-1", | ||
| Operator: "AND", | ||
| Keywords: []string{"keyword1", "keyword2"}, | ||
| }, | ||
| { | ||
| Category: "test-category-2", | ||
| Operator: "OR", | ||
| Keywords: []string{"keyword3", "keyword4"}, | ||
| CaseSensitive: true, | ||
| }, | ||
| { | ||
| Category: "test-category-3", | ||
| Operator: "NOR", | ||
| Keywords: []string{"keyword5", "keyword6"}, | ||
| }, | ||
| } | ||
|
|
||
| classifier := NewKeywordClassifier(rules) | ||
|
|
||
| b.Run("AND match", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _, _, _ = classifier.Classify("this text contains keyword1 and keyword2") | ||
| } | ||
| }) | ||
|
|
||
| b.Run("OR match", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _, _, _ = classifier.Classify("this text contains keyword3") | ||
| } | ||
| }) | ||
|
|
||
| b.Run("NOR match", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _, _, _ = classifier.Classify("this text is clean") | ||
| } | ||
| }) | ||
|
|
||
| b.Run("No match", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _, _, _ = classifier.Classify("this text contains keyword5") | ||
| } | ||
| }) | ||
| } |
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
101 changes: 101 additions & 0 deletions
101
src/semantic-router/pkg/utils/classification/keyword_classifier.go
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,101 @@ | ||
| package classification | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/vllm-project/semantic-router/src/semantic-router/pkg/config" | ||
| "github.com/vllm-project/semantic-router/src/semantic-router/pkg/observability" | ||
| ) | ||
|
|
||
| // KeywordClassifier implements keyword-based classification logic. | ||
| type KeywordClassifier struct { | ||
| rules []config.KeywordRule | ||
| } | ||
|
|
||
| // NewKeywordClassifier creates a new KeywordClassifier. | ||
| func NewKeywordClassifier(rules []config.KeywordRule) *KeywordClassifier { | ||
| return &KeywordClassifier{rules: rules} | ||
| } | ||
|
|
||
| // Classify performs keyword-based classification on the given text. | ||
| func (c *KeywordClassifier) Classify(text string) (string, float64, error) { | ||
| for _, rule := range c.rules { | ||
| if matched, keywords := c.matches(text, rule); matched { | ||
| if len(keywords) > 0 { | ||
| observability.Infof( | ||
| "Keyword-based classification matched category %q with keywords: %v", | ||
| rule.Category, keywords, | ||
| ) | ||
| } else { | ||
| observability.Infof( | ||
| "Keyword-based classification matched category %q with a NOR rule.", | ||
| rule.Category, | ||
| ) | ||
| } | ||
| return rule.Category, 1.0, nil | ||
| } | ||
| } | ||
| return "", 0.0, nil | ||
| } | ||
|
|
||
| // matches checks if the text matches the given keyword rule. | ||
| func (c *KeywordClassifier) matches(text string, rule config.KeywordRule) (bool, []string) { | ||
| // Default to case-insensitive matching if not specified | ||
| caseSensitive := rule.CaseSensitive | ||
| var matchedKeywords []string | ||
|
|
||
| // Prepare text for matching | ||
| preparedText := text | ||
| if !caseSensitive { | ||
| preparedText = strings.ToLower(text) | ||
| } | ||
|
|
||
| // Check for matches based on the operator | ||
| switch rule.Operator { | ||
| case "AND": | ||
| for _, keyword := range rule.Keywords { | ||
| preparedKeyword := keyword | ||
| if !caseSensitive { | ||
| preparedKeyword = strings.ToLower(keyword) | ||
| } | ||
| if !strings.Contains(preparedText, preparedKeyword) { | ||
| return false, nil | ||
| } | ||
| matchedKeywords = append(matchedKeywords, keyword) | ||
| } | ||
| return true, matchedKeywords | ||
|
|
||
| case "OR": | ||
| for _, keyword := range rule.Keywords { | ||
| preparedKeyword := keyword | ||
| if !caseSensitive { | ||
| preparedKeyword = strings.ToLower(keyword) | ||
| } | ||
| if strings.Contains(preparedText, preparedKeyword) { | ||
| // For OR, we can return on the first match. | ||
| return true, []string{keyword} | ||
| } | ||
| } | ||
| return false, nil | ||
|
|
||
| case "NOR": | ||
| for _, keyword := range rule.Keywords { | ||
| preparedKeyword := keyword | ||
| if !caseSensitive { | ||
| preparedKeyword = strings.ToLower(keyword) | ||
| } | ||
| if strings.Contains(preparedText, preparedKeyword) { | ||
| return false, nil | ||
| } | ||
| } | ||
| // Return true with an empty slice | ||
| return true, matchedKeywords | ||
|
|
||
| default: | ||
| observability.Warnf( | ||
| "KeywordClassifier: unsupported operator %q in rule for category %q. Returning no match.", | ||
| rule.Operator, rule.Category, | ||
| ) | ||
| return false, nil | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
src/semantic-router/pkg/utils/classification/keyword_classifier_test.go
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,92 @@ | ||
| package classification | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/vllm-project/semantic-router/src/semantic-router/pkg/config" | ||
| ) | ||
|
|
||
| func TestKeywordClassifier(t *testing.T) { | ||
| rules := []config.KeywordRule{ | ||
| { | ||
| Category: "test-category-1", | ||
| Operator: "AND", | ||
| Keywords: []string{"keyword1", "keyword2"}, | ||
| }, | ||
| { | ||
| Category: "test-category-2", | ||
| Operator: "OR", | ||
| Keywords: []string{"keyword3", "keyword4"}, | ||
| CaseSensitive: true, | ||
| }, | ||
| { | ||
| Category: "test-category-3", | ||
| Operator: "NOR", | ||
| Keywords: []string{"keyword5", "keyword6"}, | ||
| }, | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| text string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "AND match", | ||
| text: "this text contains keyword1 and keyword2", | ||
| expected: "test-category-1", | ||
| }, | ||
| { | ||
| name: "AND no match", | ||
| // This text does not match the AND rule. It also does not contain "keyword5" or "keyword6", | ||
| // so the NOR rule will match as a fallback. | ||
| text: "this text contains keyword1 but not the other", | ||
| expected: "test-category-3", | ||
| }, | ||
| { | ||
| name: "OR match", | ||
| text: "this text contains keyword3", | ||
| expected: "test-category-2", | ||
| }, | ||
| { | ||
| name: "OR no match", | ||
| // This text does not match the OR rule. It also does not contain "keyword5" or "keyword6", | ||
| // so the NOR rule will match as a fallback. | ||
| text: "this text contains nothing of interest", | ||
| expected: "test-category-3", | ||
| }, | ||
| { | ||
| name: "NOR match", | ||
| text: "this text is clean", | ||
| expected: "test-category-3", | ||
| }, | ||
| { | ||
| name: "NOR no match", | ||
| // This text contains "keyword5", so the NOR rule will NOT match. | ||
| // Since no other rules match, the result should be empty. | ||
| text: "this text contains keyword5", | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "Case sensitive no match", | ||
| // This text does not match the case-sensitive OR rule. It also does not contain "keyword5" or "keyword6", | ||
| // so the NOR rule will match as a fallback. | ||
| text: "this text contains KEYWORD3", | ||
| expected: "test-category-3", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create a new classifier for each test to ensure a clean slate | ||
| classifier := NewKeywordClassifier(rules) | ||
| category, _, err := classifier.Classify(tt.text) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if category != tt.expected { | ||
| t.Errorf("expected category %q, but got %q", tt.expected, category) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,58 @@ | ||
| import React, { useEffect, useState } from 'react' | ||
| import styles from './styles.module.css' | ||
|
|
||
| export default function ScrollToTop(): React.ReactElement { | ||
| const [isVisible, setIsVisible] = useState(false) | ||
|
|
||
| useEffect(() => { | ||
| const toggleVisibility = () => { | ||
| if (window.pageYOffset > 300) { | ||
| setIsVisible(true) | ||
| } | ||
| else { | ||
| setIsVisible(false) | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener('scroll', toggleVisibility) | ||
|
|
||
| return () => { | ||
| window.removeEventListener('scroll', toggleVisibility) | ||
| } | ||
| }, []) | ||
|
|
||
| const scrollToTop = () => { | ||
| window.scrollTo({ | ||
| top: 0, | ||
| behavior: 'smooth', | ||
| }) | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {isVisible && ( | ||
| <button | ||
| onClick={scrollToTop} | ||
| className={styles.scrollToTop} | ||
| aria-label="Scroll to top" | ||
| > | ||
| <svg | ||
| width="24" | ||
| height="24" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| d="M12 19V5M12 5L5 12M12 5L19 12" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| /> | ||
| </svg> | ||
| </button> | ||
| )} | ||
| </> | ||
| ) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.