-
Notifications
You must be signed in to change notification settings - Fork 37
feat: wire evaluations tracking chain in ManagedModel.run() #1333
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
base: jb/aic-2388/js-managed-result
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { ManagedModel } from '../src/api/ManagedModel'; | ||
| import { LDAIConfigTracker } from '../src/api/config/LDAIConfigTracker'; | ||
| import { LDAICompletionConfig } from '../src/api/config/types'; | ||
| import { Evaluator } from '../src/api/judge/Evaluator'; | ||
| import { LDJudgeResult } from '../src/api/judge/types'; | ||
| import { RunnerResult } from '../src/api/model/types'; | ||
| import { Runner } from '../src/api/providers/Runner'; | ||
|
|
||
| describe('ManagedModel.run() evaluations', () => { | ||
| let mockRunner: jest.Mocked<Runner>; | ||
| let mockTracker: jest.Mocked<LDAIConfigTracker>; | ||
| let aiConfig: LDAICompletionConfig; | ||
|
|
||
| const runnerResult: RunnerResult = { | ||
| content: 'AI response content', | ||
| metrics: { success: true }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockRunner = { | ||
| run: jest.fn().mockResolvedValue(runnerResult), | ||
| }; | ||
|
|
||
| mockTracker = { | ||
| trackMetricsOf: jest.fn().mockImplementation(async (_extractor: any, func: any) => func()), | ||
| trackJudgeResult: jest.fn(), | ||
| resumptionToken: 'test-resumption-token', | ||
| getTrackData: jest.fn().mockReturnValue({}), | ||
| trackDuration: jest.fn(), | ||
| trackTokens: jest.fn(), | ||
| trackSuccess: jest.fn(), | ||
| trackError: jest.fn(), | ||
| trackFeedback: jest.fn(), | ||
| trackTimeToFirstToken: jest.fn(), | ||
| trackDurationOf: jest.fn(), | ||
| trackOpenAIMetrics: jest.fn(), | ||
| trackBedrockConverseMetrics: jest.fn(), | ||
| trackVercelAIMetrics: jest.fn(), | ||
| getSummary: jest | ||
| .fn() | ||
| .mockReturnValue({ success: true, resumptionToken: 'test-resumption-token' }), | ||
| } as any; | ||
|
|
||
| aiConfig = { | ||
| key: 'test-config', | ||
| enabled: true, | ||
| messages: [{ role: 'system', content: 'You are helpful.' }], | ||
| model: { name: 'gpt-4' }, | ||
| provider: { name: 'openai' }, | ||
| createTracker: () => mockTracker, | ||
| evaluator: Evaluator.noop(), | ||
| }; | ||
| }); | ||
|
|
||
| it('returns before evaluations resolve', async () => { | ||
| let resolveEval!: (v: LDJudgeResult[]) => void; | ||
| const slowEvaluator = { | ||
| judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, | ||
| evaluate: jest.fn().mockReturnValue( | ||
| new Promise<LDJudgeResult[]>((resolve) => { | ||
| resolveEval = resolve; | ||
| }), | ||
| ), | ||
| judges: new Map(), | ||
| } as unknown as Evaluator; | ||
|
|
||
| const configWithEvaluator: LDAICompletionConfig = { | ||
| ...aiConfig, | ||
| evaluator: slowEvaluator, | ||
| }; | ||
|
|
||
| const model = new ManagedModel(configWithEvaluator, mockRunner); | ||
|
|
||
| let evaluationsResolved = false; | ||
| const result = await model.run('Hello'); | ||
|
|
||
| expect(result.content).toBe('AI response content'); | ||
|
|
||
| result.evaluations.then(() => { | ||
| evaluationsResolved = true; | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(evaluationsResolved).toBe(false); | ||
|
|
||
| resolveEval([{ success: true, sampled: true, score: 0.9 }]); | ||
| await result.evaluations; | ||
| expect(evaluationsResolved).toBe(true); | ||
| }); | ||
|
|
||
| it('awaiting evaluations guarantees tracking is complete', async () => { | ||
| const judgeResult: LDJudgeResult = { | ||
| success: true, | ||
| sampled: true, | ||
| score: 0.8, | ||
| metricKey: 'quality', | ||
| }; | ||
| const mockEvaluator = { | ||
| judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, | ||
| evaluate: jest.fn().mockResolvedValue([judgeResult]), | ||
| judges: new Map(), | ||
| } as unknown as Evaluator; | ||
|
|
||
| const configWithEvaluator: LDAICompletionConfig = { | ||
| ...aiConfig, | ||
| evaluator: mockEvaluator, | ||
| }; | ||
|
|
||
| const model = new ManagedModel(configWithEvaluator, mockRunner); | ||
| const result = await model.run('Hello'); | ||
|
|
||
| await result.evaluations; | ||
| expect(mockTracker.trackJudgeResult).toHaveBeenCalledWith(judgeResult); | ||
| }); | ||
|
|
||
| it('builds ManagedResult with correct content and metrics', async () => { | ||
| const model = new ManagedModel(aiConfig, mockRunner); | ||
| const result = await model.run('test prompt'); | ||
|
|
||
| expect(result.content).toBe('AI response content'); | ||
| expect(result.metrics.success).toBe(true); | ||
| expect(result.metrics.resumptionToken).toBe('test-resumption-token'); | ||
| expect(result.evaluations).toBeInstanceOf(Promise); | ||
| }); | ||
|
|
||
| it('resolves to empty evaluations when evaluator is noop', async () => { | ||
| const configWithNoop: LDAICompletionConfig = { | ||
| ...aiConfig, | ||
| evaluator: Evaluator.noop(), | ||
| }; | ||
| const model = new ManagedModel(configWithNoop, mockRunner); | ||
| const result = await model.run('Hello'); | ||
| const evaluations = await result.evaluations; | ||
| expect(evaluations).toEqual([]); | ||
| }); | ||
|
|
||
| it('passes the prompt to evaluator.evaluate as input', async () => { | ||
| const judgeResult: LDJudgeResult = { | ||
| success: true, | ||
| sampled: true, | ||
| score: 1.0, | ||
| }; | ||
| const mockEvaluator = { | ||
| judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, | ||
| evaluate: jest.fn().mockResolvedValue([judgeResult]), | ||
| judges: new Map(), | ||
| } as unknown as Evaluator; | ||
|
|
||
| const configWithEvaluator: LDAICompletionConfig = { | ||
| ...aiConfig, | ||
| evaluator: mockEvaluator, | ||
| }; | ||
|
|
||
| const model = new ManagedModel(configWithEvaluator, mockRunner); | ||
| const result = await model.run('user prompt here'); | ||
| await result.evaluations; | ||
|
|
||
| expect(mockEvaluator.evaluate).toHaveBeenCalledWith('user prompt here', 'AI response content'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,12 @@ | ||
| import { LDLogger } from '@launchdarkly/js-server-sdk-common'; | ||
|
|
||
| import { LDAICompletionConfig } from './config/types'; | ||
| import { LDJudgeResult } from './judge/types'; | ||
| import { ManagedResult, RunnerResult } from './model/types'; | ||
| import { Runner } from './providers/Runner'; | ||
|
|
||
| /** | ||
| * ManagedModel provides chat-completion invocation with automatic tracking and | ||
| * (in a future PR) automatic judge evaluation. | ||
| * automatic judge evaluation. | ||
| * | ||
| * The class is stateless: each `run()` call sends the prompt directly to the | ||
| * underlying `Runner` and returns a `ManagedResult`. Conversation history, | ||
|
|
@@ -42,11 +41,16 @@ export class ManagedModel { | |
|
|
||
| const metrics = tracker.getSummary(); | ||
|
|
||
| // Evaluations are wired in a follow-up PR. For now, resolve empty. | ||
| const evaluations: Promise<LDJudgeResult[]> = Promise.resolve([]); | ||
| const output = result.content; | ||
| const evaluations = this.aiConfig.evaluator.evaluate(prompt, output).then((results) => { | ||
| results.forEach((judgeResult) => { | ||
| tracker.trackJudgeResult(judgeResult); | ||
| }); | ||
| return results; | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unhandled rejection risk on fire-and-forget evaluations promiseMedium Severity The Reviewed by Cursor Bugbot for commit f7cd796. Configure here. |
||
|
|
||
| return { | ||
| content: result.content, | ||
| content: output, | ||
| metrics, | ||
| raw: result.raw, | ||
| parsed: result.parsed, | ||
|
|
||


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.
Missing null check crashes when no evaluator configured
High Severity
this.aiConfig.evaluator!.evaluate(prompt, output)uses a non-null assertion onevaluator, which is declared as optional (evaluator?: Evaluator) inLDAICompletionConfig. When no evaluator is configured, this throws aTypeErrorat runtime. The previous code correctly fell back toPromise.resolve([])for the no-evaluator case, but this replacement removed that guard entirely. The test "resolves to empty evaluations when no evaluator configured" confirms this is an expected usage path that will now crash.Reviewed by Cursor Bugbot for commit 14ffe34. Configure here.