-
Notifications
You must be signed in to change notification settings - Fork 140
Basic NJS module to extract model name #3877
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
sjberman
merged 2 commits into
nginx:feat/inference-extension
from
sjberman:feat/extract-model
Sep 9, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 22 | ||
| 24 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // This file contains the methods to get an AI workload endpoint from the EndpointPicker (EPP). | ||
|
|
||
| // TODO (sberman): this module will need to be enhanced to include the following: | ||
| // - function that sends the subrequest to the Go middleware application (to get the endpoint from EPP) | ||
| // - if a user has specified an Exact matching condition for a model name, extract the model name from | ||
| // the request body, and if it matches that condition, set the proper value in the X-Gateway-Model-Name header | ||
| // (based on if we do a redirect or traffic split (see design doc)) in the subrequest. If the client request | ||
| // already has this header set, then I don't think we need to extract the model from the body, just pass | ||
| // through the existing header. | ||
| // I believe we have to use js_content to call the NJS functionality. Because this takes over | ||
| // the request, we will likely have to finish the NJS functionality with an internalRedirect to an internal | ||
| // location that proxy_passes to the chosen endpoint. | ||
|
|
||
| // extractModel extracts the model name from the request body. | ||
| function extractModel(r) { | ||
| try { | ||
| var body = JSON.parse(r.requestText); | ||
salonichf5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (body && body.model !== undefined) { | ||
| return String(body.model); | ||
| } | ||
| } catch (e) { | ||
| r.error(`error parsing request body for model name: ${e.message}`); | ||
| return ''; | ||
| } | ||
| r.error('request body does not contain model parameter'); | ||
| return ''; | ||
| } | ||
sjberman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default { extractModel }; | ||
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,52 @@ | ||
| import { default as epp } from '../src/epp.js'; | ||
| import { expect, describe, it } from 'vitest'; | ||
|
|
||
| function makeRequest(body) { | ||
| let r = { | ||
| // Test mocks | ||
| error(msg) { | ||
| r.variables.error = msg; | ||
| }, | ||
| requestText: body, | ||
| variables: {}, | ||
| }; | ||
|
|
||
| return r; | ||
| } | ||
|
|
||
| describe('extractModel', () => { | ||
| const tests = [ | ||
| { | ||
| name: 'returns the model value', | ||
| body: '{"model":"gpt-4"}', | ||
| model: 'gpt-4', | ||
| error: undefined, | ||
| }, | ||
| { | ||
| name: 'returns empty string if model is missing', | ||
| body: '{"foo":1}', | ||
| model: '', | ||
| error: 'request body does not contain model parameter', | ||
| }, | ||
| { | ||
| name: 'returns empty string for invalid JSON', | ||
| body: 'not-json', | ||
| model: '', | ||
| error: `error parsing request body for model name: Unexpected token 'o', "not-json" is not valid JSON`, | ||
| }, | ||
| { | ||
| name: 'empty request body', | ||
| body: '', | ||
| model: '', | ||
| error: 'error parsing request body for model name: Unexpected end of JSON input', | ||
| }, | ||
| ]; | ||
|
|
||
| tests.forEach((test) => { | ||
| it(test.name, () => { | ||
| let r = makeRequest(test.body); | ||
| expect(epp.extractModel(r)).to.equal(test.model); | ||
| expect(r.variables.error).to.equal(test.error); | ||
| }); | ||
| }); | ||
| }); |
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.