-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate regenerate API (#58)
* feat: integrate regenerate API Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize code Signed-off-by: SuZhou-Joe <[email protected]> * fix: unit test error Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize code Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize code Signed-off-by: SuZhou-Joe <[email protected]> --------- Signed-off-by: SuZhou-Joe <[email protected]>
- Loading branch information
1 parent
e73dcaa
commit 11e5779
Showing
9 changed files
with
278 additions
and
65 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
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,176 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { OllyChatService } from './olly_chat_service'; | ||
import { CoreRouteHandlerContext } from '../../../../../src/core/server/core_route_handler_context'; | ||
import { coreMock, httpServerMock } from '../../../../../src/core/server/mocks'; | ||
import { loggerMock } from '../../../../../src/core/server/logging/logger.mock'; | ||
|
||
describe('OllyChatService', () => { | ||
const ollyChatService = new OllyChatService(); | ||
const coreContext = new CoreRouteHandlerContext( | ||
coreMock.createInternalStart(), | ||
httpServerMock.createOpenSearchDashboardsRequest() | ||
); | ||
const mockedTransport = coreContext.opensearch.client.asCurrentUser.transport | ||
.request as jest.Mock; | ||
const contextMock = { | ||
core: coreContext, | ||
assistant_plugin: { | ||
logger: loggerMock.create(), | ||
}, | ||
}; | ||
beforeEach(() => { | ||
mockedTransport.mockClear(); | ||
}); | ||
it('requestLLM should invoke client call with correct params', async () => { | ||
mockedTransport.mockImplementationOnce(() => { | ||
return { | ||
body: { | ||
inference_results: [ | ||
{ | ||
output: [ | ||
{ | ||
name: 'memory_id', | ||
result: 'foo', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
}); | ||
const result = await ollyChatService.requestLLM( | ||
{ | ||
messages: [], | ||
input: { | ||
type: 'input', | ||
contentType: 'text', | ||
content: 'content', | ||
}, | ||
sessionId: '', | ||
rootAgentId: 'rootAgentId', | ||
}, | ||
contextMock | ||
); | ||
expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
Object { | ||
"body": Object { | ||
"parameters": Object { | ||
"question": "content", | ||
"verbose": true, | ||
}, | ||
}, | ||
"method": "POST", | ||
"path": "/_plugins/_ml/agents/rootAgentId/_execute", | ||
}, | ||
Object { | ||
"maxRetries": 0, | ||
"requestTimeout": 300000, | ||
}, | ||
], | ||
] | ||
`); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"memoryId": "foo", | ||
"messages": Array [], | ||
} | ||
`); | ||
}); | ||
|
||
it('requestLLM should throw error when transport.request throws error', async () => { | ||
mockedTransport.mockImplementationOnce(() => { | ||
throw new Error('error'); | ||
}); | ||
expect( | ||
ollyChatService.requestLLM( | ||
{ | ||
messages: [], | ||
input: { | ||
type: 'input', | ||
contentType: 'text', | ||
content: 'content', | ||
}, | ||
sessionId: '', | ||
rootAgentId: 'rootAgentId', | ||
}, | ||
contextMock | ||
) | ||
).rejects.toMatchInlineSnapshot(`[Error: error]`); | ||
}); | ||
|
||
it('regenerate should invoke client call with correct params', async () => { | ||
mockedTransport.mockImplementationOnce(() => { | ||
return { | ||
body: { | ||
inference_results: [ | ||
{ | ||
output: [ | ||
{ | ||
name: 'memory_id', | ||
result: 'foo', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
}); | ||
const result = await ollyChatService.regenerate( | ||
{ | ||
sessionId: 'sessionId', | ||
rootAgentId: 'rootAgentId', | ||
interactionId: 'interactionId', | ||
}, | ||
contextMock | ||
); | ||
expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
Object { | ||
"body": Object { | ||
"parameters": Object { | ||
"memory_id": "sessionId", | ||
"regenerate_interaction_id": "interactionId", | ||
"verbose": true, | ||
}, | ||
}, | ||
"method": "POST", | ||
"path": "/_plugins/_ml/agents/rootAgentId/_execute", | ||
}, | ||
Object { | ||
"maxRetries": 0, | ||
"requestTimeout": 300000, | ||
}, | ||
], | ||
] | ||
`); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"memoryId": "foo", | ||
"messages": Array [], | ||
} | ||
`); | ||
}); | ||
|
||
it('regenerate should throw error when transport.request throws error', async () => { | ||
mockedTransport.mockImplementationOnce(() => { | ||
throw new Error('error'); | ||
}); | ||
expect( | ||
ollyChatService.regenerate( | ||
{ | ||
sessionId: 'sessionId', | ||
rootAgentId: 'rootAgentId', | ||
interactionId: 'interactionId', | ||
}, | ||
contextMock | ||
) | ||
).rejects.toMatchInlineSnapshot(`[Error: error]`); | ||
}); | ||
}); |
Oops, something went wrong.