-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
828 streamline chat model configuration info message network call (#875)
- Loading branch information
1 parent
81ee06c
commit ae133b7
Showing
12 changed files
with
277 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { Request } from 'express'; | ||
|
||
import { CHAT_MODELS, ChatModelConfiguration } from '@src/models/chat'; | ||
import { CHAT_MODELS, ChatModelConfigurations } from '@src/models/chat'; | ||
|
||
export type OpenAiSetModelRequest = Request< | ||
never, | ||
never, | ||
{ | ||
model?: CHAT_MODELS; | ||
configuration?: ChatModelConfiguration; | ||
configuration?: ChatModelConfigurations; | ||
}, | ||
never | ||
>; |
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,156 @@ | ||
import { expect, jest, test, describe } from '@jest/globals'; | ||
import { Response } from 'express'; | ||
|
||
import { handleConfigureModel } from '@src/controller/modelController'; | ||
import { OpenAiConfigureModelRequest } from '@src/models/api/OpenAiConfigureModelRequest'; | ||
import { modelConfigIds } from '@src/models/chat'; | ||
import { ChatMessage } from '@src/models/chatMessage'; | ||
import { LEVEL_NAMES, LevelState } from '@src/models/level'; | ||
|
||
function responseMock() { | ||
return { | ||
send: jest.fn(), | ||
status: jest.fn().mockReturnThis(), | ||
} as unknown as Response; | ||
} | ||
|
||
describe('handleConfigureModel', () => { | ||
test('WHEN passed sensible parameters THEN configures model AND adds info message to chat history AND responds with info message', () => { | ||
const req = { | ||
body: { | ||
configId: 'topP', | ||
value: 0.5, | ||
}, | ||
session: { | ||
chatModel: { | ||
configuration: { | ||
temperature: 0.0, | ||
topP: 0.0, | ||
presencePenalty: 0.0, | ||
frequencyPenalty: 0.0, | ||
}, | ||
}, | ||
levelState: [{}, {}, {}, { chatHistory: [] } as unknown as LevelState], | ||
}, | ||
} as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(req.session.chatModel.configuration.topP).toBe(0.5); | ||
|
||
const expectedInfoMessage = { | ||
infoMessage: 'changed topP to 0.5', | ||
chatMessageType: 'GENERIC_INFO', | ||
} as ChatMessage; | ||
expect( | ||
req.session.levelState[LEVEL_NAMES.SANDBOX].chatHistory.at(-1) | ||
).toEqual(expectedInfoMessage); | ||
expect(res.send).toHaveBeenCalledWith({ | ||
chatInfoMessage: expectedInfoMessage, | ||
}); | ||
}); | ||
|
||
test('WHEN missing configId THEN does not configure model', () => { | ||
const req = { | ||
body: { | ||
value: 0.5, | ||
}, | ||
} as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith('Missing configId'); | ||
}); | ||
|
||
test('WHEN configId is invalid THEN does not configure model', () => { | ||
const req = { | ||
body: { | ||
configId: 'invalid config id', | ||
value: 0.5, | ||
}, | ||
} as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith('Invalid configId'); | ||
}); | ||
|
||
test('WHEN value is missing THEN does not configure model', () => { | ||
const req = { | ||
body: { | ||
configId: 'topP', | ||
}, | ||
} as unknown as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith('Missing or invalid value'); | ||
}); | ||
|
||
test('WHEN value is invalid THEN does not configure model', () => { | ||
const req = { | ||
body: { | ||
configId: 'topP', | ||
value: 'invalid value', | ||
}, | ||
} as unknown as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith('Missing or invalid value'); | ||
}); | ||
|
||
test.each(modelConfigIds)( | ||
'GIVEN configId is %s WHEN value is below range THEN does not configure model', | ||
(configId) => { | ||
const req = { | ||
body: { | ||
configId, | ||
value: -1, | ||
}, | ||
} as unknown as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
const expectedMaxValue = configId === 'topP' ? 1 : 2; | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith( | ||
`Value should be between 0 and ${expectedMaxValue} for ${configId}` | ||
); | ||
} | ||
); | ||
|
||
test.each(modelConfigIds)( | ||
'GIVEN configId is %s WHEN value is above range THEN does not configure model', | ||
(configId) => { | ||
const expectedMaxValue = configId === 'topP' ? 1 : 2; | ||
|
||
const req = { | ||
body: { | ||
configId, | ||
value: expectedMaxValue + 1, | ||
}, | ||
} as unknown as OpenAiConfigureModelRequest; | ||
const res = responseMock(); | ||
|
||
handleConfigureModel(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.send).toHaveBeenCalledWith( | ||
`Value should be between 0 and ${expectedMaxValue} for ${configId}` | ||
); | ||
} | ||
); | ||
}); |
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
Oops, something went wrong.