-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from mrsteele/feature/dynamic
feat: Adding a dynamic wrapper
- Loading branch information
Showing
6 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
const { validateWrapper } = require('./validate') | ||
|
||
const dynamicWrapper = (originalBody = {}) => { | ||
const model = originalBody.model.find(a => validateWrapper({ ...originalBody, model: a }).valid) | ||
|
||
if (!model) { | ||
console.warn('openai-tokens[dynamic]: No valid model available. Either add larger models, adjust options, wrap it with the `truncateWrapper` or reduce prompt sizes.') | ||
} | ||
|
||
const { opts, ...body } = originalBody | ||
const results = { | ||
...body, | ||
model: model || model[body.model.length - 1] | ||
} | ||
|
||
return opts?.stringify ? JSON.stringify(results) : results | ||
} | ||
|
||
module.exports.dynamicWrapper = dynamicWrapper |
This file contains 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,50 @@ | ||
const { dynamicWrapper } = require('.') | ||
const ten = 'this is 10 tokens long for reference? ' | ||
|
||
const defaultRequest = { | ||
model: ['gpt-3.5-turbo', 'gpt-3.5-turbo-16k'] | ||
} | ||
|
||
describe('dynamicWrapper', () => { | ||
test('picks the first one when valid', () => { | ||
const result = dynamicWrapper({ | ||
...defaultRequest, | ||
messages: [{ | ||
role: 'user', | ||
content: 'This makes works fine' | ||
}] | ||
}) | ||
|
||
expect(result.model).toBe(defaultRequest.model[0]) | ||
}) | ||
|
||
test('picks the larger when exceeding the first one', () => { | ||
const result = dynamicWrapper({ | ||
...defaultRequest, | ||
messages: [{ | ||
role: 'user', | ||
content: ten.repeat(500) | ||
}] | ||
}) | ||
|
||
expect(result.model).toBe(defaultRequest.model[1]) | ||
}) | ||
|
||
test('should still stringify if we must', () => { | ||
const args = { | ||
...defaultRequest, | ||
messages: [{ | ||
role: 'user', | ||
content: 'This makes works fine' | ||
}], | ||
opts: { | ||
stringify: true, | ||
buffer: 1000 | ||
} | ||
} | ||
const result = dynamicWrapper(args) | ||
|
||
const { opts, model, ...body } = args | ||
expect(result.length).toBe(JSON.stringify({ ...body, model: defaultRequest.model[0] }).length) | ||
}) | ||
}) |
This file contains 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,9 +1,11 @@ | ||
const { validateMessage, validateWrapper } = require('./validate') | ||
const { truncateMessage, truncateWrapper } = require('./truncate') | ||
const { dynamicWrapper } = require('./dynamic') | ||
|
||
module.exports = { | ||
validateMessage, | ||
validateWrapper, | ||
truncateWrapper, | ||
truncateMessage | ||
truncateMessage, | ||
dynamicWrapper | ||
} |
This file contains 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 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