-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add some shared llm-type plugin utilities (#5109)
* wip utilities * fixes * add tests * update comment * update * add math.random stubs to test hooks * fix
- Loading branch information
Showing
7 changed files
with
143 additions
and
72 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
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
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,35 @@ | ||
const Sampler = require('../../sampler') | ||
|
||
const RE_NEWLINE = /\n/g | ||
const RE_TAB = /\t/g | ||
|
||
function normalize (text, limit = 128) { | ||
if (!text) return | ||
if (typeof text !== 'string' || !text || (typeof text === 'string' && text.length === 0)) return | ||
|
||
text = text | ||
.replace(RE_NEWLINE, '\\n') | ||
.replace(RE_TAB, '\\t') | ||
|
||
if (text.length > limit) { | ||
return text.substring(0, limit) + '...' | ||
} | ||
|
||
return text | ||
} | ||
|
||
function isPromptCompletionSampled (sampler) { | ||
return sampler.isSampled() | ||
} | ||
|
||
module.exports = function (integrationName, tracerConfig) { | ||
const integrationConfig = tracerConfig[integrationName] || {} | ||
const { spanCharLimit, spanPromptCompletionSampleRate } = integrationConfig | ||
|
||
const sampler = new Sampler(spanPromptCompletionSampleRate ?? 1.0) | ||
|
||
return { | ||
normalize: str => normalize(str, spanCharLimit), | ||
isPromptCompletionSampled: () => isPromptCompletionSampled(sampler) | ||
} | ||
} |
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,80 @@ | ||
'use strict' | ||
|
||
require('../../setup/tap') | ||
|
||
const makeUtilities = require('../../../src/plugins/util/llm') | ||
|
||
describe('llm utils', () => { | ||
let utils | ||
|
||
describe('with default configuration', () => { | ||
beforeEach(() => { | ||
utils = makeUtilities('langchain', {}) | ||
}) | ||
|
||
it('should normalize text to 128 characters', () => { | ||
const text = 'a'.repeat(256) | ||
expect(utils.normalize(text)).to.equal('a'.repeat(128) + '...') | ||
}) | ||
|
||
it('should return undefined for empty text', () => { | ||
expect(utils.normalize('')).to.be.undefined | ||
}) | ||
|
||
it('should return undefined for a non-string', () => { | ||
expect(utils.normalize(42)).to.be.undefined | ||
}) | ||
|
||
it('should replace special characters', () => { | ||
expect(utils.normalize('a\nb\tc')).to.equal('a\\nb\\tc') | ||
}) | ||
|
||
it('should always sample prompt completion', () => { | ||
expect(utils.isPromptCompletionSampled()).to.be.true | ||
}) | ||
}) | ||
|
||
describe('with custom configuration available', () => { | ||
beforeEach(() => { | ||
utils = makeUtilities('langchain', { | ||
langchain: { | ||
spanCharLimit: 100, | ||
spanPromptCompletionSampleRate: 0.6 | ||
} | ||
}) | ||
}) | ||
|
||
it('should normalize text to 100 characters', () => { | ||
const text = 'a'.repeat(256) | ||
expect(utils.normalize(text)).to.equal('a'.repeat(100) + '...') | ||
}) | ||
|
||
describe('with a random value greater than 0.6', () => { | ||
beforeEach(() => { | ||
sinon.stub(Math, 'random').returns(0.7) | ||
}) | ||
|
||
afterEach(() => { | ||
Math.random.restore() | ||
}) | ||
|
||
it('should not sample prompt completion', () => { | ||
expect(utils.isPromptCompletionSampled()).to.be.false | ||
}) | ||
}) | ||
|
||
describe('with a random value less than 0.6', () => { | ||
beforeEach(() => { | ||
sinon.stub(Math, 'random').returns(0.5) | ||
}) | ||
|
||
afterEach(() => { | ||
Math.random.restore() | ||
}) | ||
|
||
it('should sample prompt completion', () => { | ||
expect(utils.isPromptCompletionSampled()).to.be.true | ||
}) | ||
}) | ||
}) | ||
}) |