-
Notifications
You must be signed in to change notification settings - Fork 0
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
Test: tests for chat-gpt.ts #47
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c8a45b
updated jest config to add env var for tests
jasnoo 5656b2d
test for chat-gpt.ts
jasnoo 4390b42
moved jest setup file
jasnoo 7aa04e9
added newline
jasnoo 033a6ee
fixed userContent value
jasnoo f36dce7
fixed linting errors
jasnoo 6e79f7a
removed extra newline
jasnoo 2657b1f
removed unnecessary parameter assertion and changed test.each to it.each
jasnoo 3f6248a
return early when no comments are passed into getSummary
jasnoo 2b49be3
ggetSummary test for being passed 0 comments
jasnoo 84c97e1
remove console.log statement
jasnoo 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 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,4 @@ | ||
process.env['AZURE_OPENAI_ENDPOINT'] = 'test_endpoint'; | ||
process.env['AZURE_OPENAI_KEY'] = 'test_api_key'; | ||
process.env['CLIENT_EMAIL'] = '[email protected]'; | ||
process.env['GOOGLE_PRIVATE_KEY'] = 'test_private_key'; |
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,3 +1,4 @@ | ||
{ | ||
"preset": "ts-jest" | ||
} | ||
"preset": "ts-jest", | ||
"setupFiles": ["<rootDir>/jest-setup.ts"] | ||
} |
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,88 @@ | ||
import { getSummary } from './chat-gpt'; | ||
import { OpenAIClient } from '@azure/openai'; | ||
|
||
const MOCK_CHAT_COMPLETIONS = jest.fn().mockImplementation(() => ({ | ||
choices: [{ message: { content: 'mocked response' } }] | ||
})); | ||
|
||
jest.mock('@azure/openai', () => { | ||
const originalModule = jest.requireActual('@azure/openai'); | ||
return { | ||
...originalModule, | ||
OpenAIClient: jest.fn().mockImplementation(() => ({ | ||
getChatCompletions: MOCK_CHAT_COMPLETIONS | ||
})) | ||
}; | ||
}); | ||
|
||
describe('getSummary', () => { | ||
const comments = ['Test Comment 1', 'Test Comment 2']; | ||
const userContent = `---\nTest Comment 1\nTest Comment 2---`; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should use the expected arguments and return a string response', async () => { | ||
const pageURL = 'https://uistatus.dol.state.nj.us/'; | ||
const programName = 'Unemployment Insurance benefits'; | ||
const systemContent = `You are an assistant designed to find the most common themes in a large dataset of free text. Users will send a list of comments written by residents of New Jersey about their experience applying for ${programName}, where each line represents one comment. You will find the 10 most common themes in the data, and for each theme, you will include a theme title, theme description, and 3 real comments (actually in the dataset, not generated) that fit the given theme. Your output will be in the following structured valid JSON format: {"themes":[{"title":"title 1","description":"description 1","actualComments":["real comment 1","real comment 2","real comment 3"]},{"title":"title 2","description":"description 2","actualComments":["real comment 1","real comment 2","real comment 3"]}, ...]}. Make sure that the 3 comments are in the user-provided list of comments, not generated. Make sure the output is in valid JSON format, and do not add trailing commas.`; | ||
const deployment_id = 'gpt-35-turbo-16k'; | ||
const prompt = [ | ||
{ role: 'system', content: systemContent }, | ||
{ role: 'user', content: userContent } | ||
]; | ||
const result = await getSummary(comments, pageURL); | ||
expect(MOCK_CHAT_COMPLETIONS).toHaveBeenCalledTimes(1); | ||
const mockChatCompletionsArgs = MOCK_CHAT_COMPLETIONS.mock.calls[0]; | ||
expect(Array.isArray(mockChatCompletionsArgs)).toBe(true); | ||
expect(mockChatCompletionsArgs[0]).toBe(deployment_id); | ||
expect(mockChatCompletionsArgs[1][0]).toMatchObject(prompt[0]); | ||
expect(mockChatCompletionsArgs[1][1]).toMatchObject(prompt[1]); | ||
expect(result).toBe('mocked response'); | ||
}); | ||
|
||
it('should return "{}" and not call OpenAIClient when there are no comments', async () => { | ||
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. Test to check empty comment array behavior |
||
const comments = []; | ||
const pageURL = 'https://uistatus.dol.state.nj.us/'; | ||
const result = await getSummary(comments, pageURL); | ||
expect(OpenAIClient).toHaveBeenCalledTimes(0); | ||
expect(result).toBe('{}'); | ||
}); | ||
|
||
describe('getSummary with different URLs', () => { | ||
const cases = [ | ||
['Unemployment Insurance benefits', 'https://uistatus.dol.state.nj.us/'], | ||
[ | ||
'Temporary Disability Insurance and Family Leave Insurance benefits', | ||
'https://www.nj.gov/labor/myleavebenefits/' | ||
], | ||
['', 'https://www.example.com'] | ||
]; | ||
|
||
it.each(cases)( | ||
'should utilize a prompt referencing "%s" benefits when page URL is %s', | ||
async (programName, pageURL) => { | ||
const systemContent = `You are an assistant designed to find the most common themes in a large dataset of free text. Users will send a list of comments written by residents of New Jersey about their experience applying for ${programName}, where each line represents one comment. You will find the 10 most common themes in the data, and for each theme, you will include a theme title, theme description, and 3 real comments (actually in the dataset, not generated) that fit the given theme. Your output will be in the following structured valid JSON format: {"themes":[{"title":"title 1","description":"description 1","actualComments":["real comment 1","real comment 2","real comment 3"]},{"title":"title 2","description":"description 2","actualComments":["real comment 1","real comment 2","real comment 3"]}, ...]}. Make sure that the 3 comments are in the user-provided list of comments, not generated. Make sure the output is in valid JSON format, and do not add trailing commas.`; | ||
jasnoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await getSummary(comments, pageURL); | ||
expect(MOCK_CHAT_COMPLETIONS.mock.calls[0][1][0].content).toEqual( | ||
systemContent | ||
); | ||
} | ||
); | ||
}); | ||
|
||
it('should throw an error on failure', async () => { | ||
const testErrorMessage = 'Test Error'; | ||
(OpenAIClient as jest.Mock).mockImplementation(() => ({ | ||
getChatCompletions: jest | ||
.fn() | ||
.mockRejectedValue(new Error(testErrorMessage)) | ||
})); | ||
const expectedErrorMessage = `Azure OpenAI getChatCompletions failed with error: ${testErrorMessage}`; | ||
const pageURL = 'https://www.nj.gov/labor/myleavebenefits/'; | ||
await expect(getSummary(comments, pageURL)).rejects.toThrow( | ||
expectedErrorMessage | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -17,6 +17,9 @@ const PARAMETERS = { | |
const DEPLOYMENT_ID = 'gpt-35-turbo-16k'; | ||
|
||
export async function getSummary(comments: string[], pageURL: string) { | ||
if (comments.length === 0) { | ||
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. updated to handle empty array comment here |
||
return '{}'; | ||
} | ||
const client = new OpenAIClient(ENDPOINT, new AzureKeyCredential(API_KEY), { | ||
apiVersion: API_VERSION | ||
}); | ||
|
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.
I think it'd be worth adding logic into the function itself that will check for empty comments array and return early if they're missing
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.
Good call!
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.
@namanaman Do you think it would be better to put this behavior directly into the summary handler (rather than in getSummary) so that getSummary is only being called when there are any relevant comments? Something like the below:
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.
Actually, I think this behavior could belong in both places, so I will put this in the getSummary for now, and when I work on the summary handler file, I can think about adding it in as well.
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.
Good call! Yea I think it makes sense in both places as well. I could see us using the ChatGPT function separately from the summary handler in our next work, so having a stopgap there is nice too