-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat: add visualization card in customized parser #16
Changes from all commits
a74ff7f
b240c47
85b5146
2be2bb4
13b799b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
}, | ||
]); | ||
}); | ||
}); |
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', | ||
}, | ||
]); | ||
}); | ||
}); |
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); | ||||||||
}; | ||||||||
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. could you add the TODO comment
theoretically id can contain any character, i haven't tested whether also should dashboards-assistant/server/olly/utils/output_builders/__tests__/saved_objects.test.ts Line 11 in a74ff7f
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. I am using And sure, I migrate the test cases in save_objects.test.ts and add some edge cases. |
||||||||
|
||||||||
export const VisualizationCardParser = { | ||||||||
id: 'core_visualization', | ||||||||
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. should there be an 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. If order is not specified, default order value will be 999. |
||||||||
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; | ||||||||
}, | ||||||||
}; |
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([]); | ||
}); | ||
}); |
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); | ||
}); | ||
}); | ||
}; |
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.
Looks like a good place to add some unit tests for the parser :)
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.
Yes, added cases accordingly.