-
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: add visualization card in customized parser (#16)
* feat: add implement visualization in customized parser Signed-off-by: SuZhou-Joe <[email protected]> * feat: add csv-parser lib Signed-off-by: SuZhou-Joe <[email protected]> * feat: add csv-parser lib Signed-off-by: SuZhou-Joe <[email protected]> * feat: add test cases Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize Signed-off-by: SuZhou-Joe <[email protected]> --------- Signed-off-by: SuZhou-Joe <[email protected]>
- Loading branch information
1 parent
b82c17a
commit 789db2b
Showing
9 changed files
with
254 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { BasicInputOutputParser } from './basic_input_output_parser'; | ||
|
||
describe('BasicInputOutputParser', () => { | ||
it('return input and output', async () => { | ||
expect( | ||
await BasicInputOutputParser.parserProvider({ | ||
input: 'input', | ||
response: 'response', | ||
conversation_id: '', | ||
interaction_id: 'interaction_id', | ||
create_time: '', | ||
}) | ||
).toEqual([ | ||
{ | ||
type: 'input', | ||
contentType: 'text', | ||
content: 'input', | ||
}, | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'response', | ||
traceId: 'interaction_id', | ||
}, | ||
]); | ||
}); | ||
}); |
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,116 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { VisualizationCardParser } from './visualization_card_parser'; | ||
|
||
describe('VisualizationCardParser', () => { | ||
it('return visualizations when there is VisualizationTool.output', async () => { | ||
expect( | ||
await VisualizationCardParser.parserProvider({ | ||
input: 'input', | ||
response: 'response', | ||
conversation_id: '', | ||
interaction_id: 'interaction_id', | ||
create_time: '', | ||
additional_info: { | ||
'VisualizationTool.output': [ | ||
'row_number,Id,title\n' + | ||
'1,id1,[Flights] Total Flights\n' + | ||
'2,id2,[Flights] Controls\n' + | ||
'3,id3,[Flights] Airline Carrier', | ||
], | ||
}, | ||
}) | ||
).toEqual([ | ||
{ | ||
content: 'id1', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
{ | ||
content: 'id2', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
{ | ||
content: 'id3', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
]); | ||
}); | ||
|
||
it('return visualizations when there are multiple VisualizationTool.outputs', async () => { | ||
expect( | ||
await VisualizationCardParser.parserProvider({ | ||
input: 'input', | ||
response: 'response', | ||
conversation_id: '', | ||
interaction_id: 'interaction_id', | ||
create_time: '', | ||
additional_info: { | ||
'VisualizationTool.output': [ | ||
'row_number,Id,title\n' + '1,id1,[Flights] Total Flights\n', | ||
'row_number,Id,title\n' + '2,id2,[Flights] Controls\n', | ||
], | ||
}, | ||
}) | ||
).toEqual([ | ||
{ | ||
content: 'id1', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
{ | ||
content: 'id2', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
]); | ||
}); | ||
|
||
it('do not return visualizations when VisualizationTool.output is null', async () => { | ||
expect( | ||
await VisualizationCardParser.parserProvider({ | ||
input: 'input', | ||
response: 'response', | ||
conversation_id: '', | ||
interaction_id: 'interaction_id', | ||
create_time: '', | ||
additional_info: {}, | ||
}) | ||
).toEqual([]); | ||
}); | ||
|
||
it('do not return visualizations when VisualizationTool.output is not in correct format', async () => { | ||
expect( | ||
await VisualizationCardParser.parserProvider({ | ||
input: 'input', | ||
response: 'response', | ||
conversation_id: '', | ||
interaction_id: 'interaction_id', | ||
create_time: '', | ||
additional_info: { | ||
'VisualizationTool.output': [ | ||
'row_number\n' + '1', | ||
'row_number,Id,title\n' + '2,id2,[Flights] Controls\n', | ||
], | ||
}, | ||
}) | ||
).toEqual([ | ||
{ | ||
content: 'id2', | ||
contentType: 'visualization', | ||
suggestedActions: [{ actionType: 'view_in_dashboards', message: 'View in Visualize' }], | ||
type: 'output', | ||
}, | ||
]); | ||
}); | ||
}); |
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,48 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { IMessage, Interaction } from '../../common/types/chat_saved_object_attributes'; | ||
import { getJsonFromString } from '../utils/csv-parser-helper'; | ||
|
||
const extractIdsFromCsvString = async (csv: string) => { | ||
const lines = (await getJsonFromString(csv)) as Array<{ Id: string }>; | ||
return lines | ||
.map((line) => line.Id) | ||
.filter(<T>(v: T | null | undefined): v is T => v !== null && v !== undefined); | ||
}; | ||
|
||
export const VisualizationCardParser = { | ||
id: 'core_visualization', | ||
async parserProvider(interaction: Interaction) { | ||
const visualizationOutputs = interaction.additional_info?.['VisualizationTool.output'] as | ||
| string[] | ||
| undefined; | ||
if (!visualizationOutputs) { | ||
return []; | ||
} | ||
const visualizationIds = ( | ||
await Promise.all(visualizationOutputs.map((output) => extractIdsFromCsvString(output))) | ||
).flatMap((id) => id); | ||
|
||
const visOutputs: IMessage[] = visualizationIds | ||
/** | ||
* Empty id will be filtered | ||
*/ | ||
.filter((id) => id) | ||
.map((id) => ({ | ||
type: 'output', | ||
content: id, | ||
contentType: 'visualization', | ||
suggestedActions: [ | ||
{ | ||
message: 'View in Visualize', | ||
actionType: 'view_in_dashboards', | ||
}, | ||
], | ||
})); | ||
|
||
return visOutputs; | ||
}, | ||
}; |
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,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { getJsonFromString } from './csv-parser-helper'; | ||
|
||
describe('getJsonFromString', () => { | ||
it('return correct answer', async () => { | ||
expect(await getJsonFromString('title,id\n1,2')).toEqual([ | ||
{ | ||
title: '1', | ||
id: '2', | ||
}, | ||
]); | ||
}); | ||
|
||
it('return empty array when string is not in correct format', async () => { | ||
expect(await getJsonFromString('1,2')).toEqual([]); | ||
}); | ||
}); |
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,25 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Readable } from 'stream'; | ||
import csvParser from 'csv-parser'; | ||
|
||
export const getJsonFromString = ( | ||
csvString: string, | ||
options?: csvParser.Options | ||
): Promise<Array<Record<string, string>> | string[][]> => { | ||
const results: string[][] | Array<Record<string, string>> = []; | ||
return new Promise((resolve, reject) => { | ||
Readable.from(csvString) | ||
.pipe(csvParser(options)) | ||
.on('data', (data) => results.push(data)) | ||
.on('end', () => { | ||
resolve(results); | ||
}) | ||
.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}; |
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