-
Notifications
You must be signed in to change notification settings - Fork 9
Fix: Correctly use context model for moderation #26
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db06b9f
Correctly use context model for moderation
steven10a 1a1c15c
Address copilot comments
steven10a b530cac
Better typing
steven10a de60e8e
better error handling
steven10a 1bab19a
More typing
steven10a bf8a028
Merge branch 'main' into dev/steven/api_key_bug
steven10a fa86851
fixed linting
steven10a 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -67,6 +67,86 @@ describe('moderation guardrail', () => { | |
| expect(result.tripwireTriggered).toBe(false); | ||
| expect(result.info?.error).toBe('Moderation API call failed'); | ||
| }); | ||
|
|
||
| it('uses context client when available', async () => { | ||
| // Track whether context client was used | ||
| let contextClientUsed = false; | ||
| const contextCreateMock = vi.fn().mockImplementation(async () => { | ||
| contextClientUsed = true; | ||
| return { | ||
| results: [ | ||
| { | ||
| categories: { | ||
| [Category.HATE]: false, | ||
| [Category.VIOLENCE]: false, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| }); | ||
|
|
||
| // Create a context with a guardrailLlm client | ||
| // We need to import OpenAI to create a proper instance | ||
| const OpenAI = (await import('openai')).default; | ||
| const contextClient = new OpenAI({ apiKey: 'test-context-key' }); | ||
| contextClient.moderations = { | ||
| create: contextCreateMock, | ||
| } as unknown as typeof contextClient.moderations; | ||
|
|
||
| const ctx = { guardrailLlm: contextClient }; | ||
| const cfg = ModerationConfig.parse({ categories: [Category.HATE] }); | ||
| const result = await moderationCheck(ctx, 'test text', cfg); | ||
|
|
||
| // Verify the context client was used | ||
| expect(contextClientUsed).toBe(true); | ||
| expect(contextCreateMock).toHaveBeenCalledWith({ | ||
| model: 'omni-moderation-latest', | ||
| input: 'test text', | ||
| safety_identifier: 'openai-guardrails-js', | ||
| }); | ||
| expect(result.tripwireTriggered).toBe(false); | ||
| }); | ||
|
|
||
| it('falls back to default client for third-party providers', async () => { | ||
| // Track whether fallback client was used | ||
| let fallbackUsed = false; | ||
|
|
||
| // The default mock from vi.mock will be used for the fallback | ||
| createMock.mockImplementation(async () => { | ||
| fallbackUsed = true; | ||
|
Comment on lines
+115
to
+116
|
||
| return { | ||
| results: [ | ||
| { | ||
| categories: { | ||
| [Category.HATE]: false, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| }); | ||
|
|
||
| // Create a context client that simulates a third-party provider | ||
| // When moderation is called, it should raise a 404 error | ||
| const contextCreateMock = vi.fn().mockRejectedValue({ | ||
| status: 404, | ||
| message: '404 page not found', | ||
| }); | ||
|
|
||
| const OpenAI = (await import('openai')).default; | ||
| const thirdPartyClient = new OpenAI({ apiKey: 'third-party-key', baseURL: 'https://localhost:8080/v1' }); | ||
| thirdPartyClient.moderations = { | ||
| create: contextCreateMock, | ||
| } as unknown as typeof thirdPartyClient.moderations; | ||
|
|
||
| const ctx = { guardrailLlm: thirdPartyClient }; | ||
| const cfg = ModerationConfig.parse({ categories: [Category.HATE] }); | ||
| const result = await moderationCheck(ctx, 'test text', cfg); | ||
|
|
||
| // Verify the fallback client was used (not the third-party one) | ||
| expect(contextCreateMock).toHaveBeenCalled(); | ||
| expect(fallbackUsed).toBe(true); | ||
| expect(result.tripwireTriggered).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('secret key guardrail', () => { | ||
|
|
||
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
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.
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.
[nitpick] The
contextClientUsedflag is unnecessary. You can check if the mock was called usingexpect(contextCreateMock).toHaveBeenCalled()instead of tracking this manually.