Skip to content

Commit

Permalink
postprocessing data2summary prompt (#436)
Browse files Browse the repository at this point in the history
* postprocessing data2summary prompt

Signed-off-by: hutiechuan <[email protected]>

* adding unit tests for function postprocessing

Signed-off-by: hutiechuan <[email protected]>

* modify for camel cases

Signed-off-by: hutiechuan <[email protected]>

* updating the function name

Signed-off-by: hutiechuan <[email protected]>

* add changelog

Signed-off-by: hutiechuan <[email protected]>

* fixing unit test issues

Signed-off-by: hutiechuan <[email protected]>

* fixing unit test

Signed-off-by: hutiechuan <[email protected]>

* adding more unit test

Signed-off-by: hutiechuan <[email protected]>

* fixing unit test issues

Signed-off-by: hutiechuan <[email protected]>

* clean up the code

Signed-off-by: hutiechuan <[email protected]>

* clean up code and replace regex

Signed-off-by: hutiechuan <[email protected]>

---------

Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: Hailong Cui <[email protected]>
Co-authored-by: hutiechuan <[email protected]>
Co-authored-by: Hailong Cui <[email protected]>
  • Loading branch information
3 people authored Feb 20, 2025
1 parent 2819352 commit 3c73361
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- expose chatEnabled flag to capabilities ([#398](https://github.com/opensearch-project/dashboards-assistant/pull/398))
- update chatbot UI to align with new look ([#435](https://github.com/opensearch-project/dashboards-assistant/pull/435))
- add flag to control if display conversation list ([#438](https://github.com/opensearch-project/dashboards-assistant/pull/438))
- add data to summary response post processing ([#436](https://github.com/opensearch-project/dashboards-assistant/pull/436))


### Enhancements

Expand All @@ -20,6 +22,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Improve alert summary with backend log pattern experience ([#389](https://github.com/opensearch-project/dashboards-assistant/pull/389))
- fixed in context feature returning 500 error if workspace is invalid to returning 4XX [#429](https://github.com/opensearch-project/dashboards-assistant/pull/429)


### Infrastructure

### Documentation
Expand Down
73 changes: 73 additions & 0 deletions server/routes/summary_routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { SUMMARY_ASSISTANT_API } from '../../common/constants/llm';
import { registerData2SummaryRoutes, registerSummaryAssistantRoutes } from './summary_routes';
import { AssistantClient } from '../services/assistant_client';
import { RequestHandlerContext } from '../../../../src/core/server';
import { postProcessing } from './summary_routes';

import * as AgentHelpers from './get_agent';
const mockedLogger = loggerMock.create();

Expand Down Expand Up @@ -326,4 +328,75 @@ describe('test summary route', () => {
}
`);
});

describe('postprocessing', () => {
it('returns combined summarization and final insights when all tags exist', () => {
const input = `
Some text <summarization>This is the summary </summarization>
random info <final insights> These are the insights </final insights>
extra text
`;
const output = postProcessing(input);

expect(output).toEqual(`This is the summary\nThese are the insights`);
});

it('returns original output if <summarization> tag is missing', () => {
const input = `Hello world <final insights>Insights here</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(input);
});

it('returns original output if </summarization> closing tag is missing', () => {
const input = `<summarization>Partial Summarization <final insights>Insights</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(input);
});

it('returns original output if <final insights> or </final insights> tags are missing', () => {
const input = `<summarization>Summary only</summarization>`;
const output = postProcessing(input);
expect(output).toEqual(input);
});

it('handles empty summarization or empty insights gracefully', () => {
const inputWithEmptySummary = `<summarization></summarization><final insights>Some insights</final insights>`;
const outputWithEmptySummary = postProcessing(inputWithEmptySummary);
expect(outputWithEmptySummary).toEqual(`\nSome insights`);

const inputWithEmptyInsights = `<summarization>Some summary</summarization><final insights></final insights>`;
const outputWithEmptyInsights = postProcessing(inputWithEmptyInsights);
expect(outputWithEmptyInsights).toEqual(`Some summary\n`);
});

it('returns original output if none of the special tags are found', () => {
const input = `Just a normal string without any special tags.`;
const output = postProcessing(input);
expect(output).toEqual(input);
});

it('handles malformed or mixed case tags by returning the original string', () => {
const input = `<Summarization>Summary</summarization><FINAL INSIGHTS>Insights</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(input);
});

it('handles special characters in content', () => {
const input = `<summarization>Summary with <>&"'</summarization><final insights>Insights with <>& "'</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(`Summary with <>&"'\nInsights with <>& "'`);
});

it('handles unicode characters correctly', () => {
const input = `<summarization>Summary with emoji 😊</summarization><final insights>Insights with unicode 你好</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(`Summary with emoji 😊\nInsights with unicode 你好`);
});

it('handles invisible characters', () => {
const input = `<summarization>Sum\u200Bmary</summarization><final insights>In\u200Bsights</final insights>`;
const output = postProcessing(input);
expect(output).toEqual(`Sum\u200Bmary\nIn\u200Bsights`);
});
});
});
17 changes: 16 additions & 1 deletion server/routes/summary_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ const SUMMARY_AGENT_CONFIG_ID = 'os_summary';
const LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID = 'os_summary_with_log_pattern';
const DATA2SUMMARY_AGENT_CONFIG_ID = 'os_data2summary';

export function postProcessing(output: string) {
const pattern = /<summarization>(.*?)<\/summarization>.*?<final insights>(.*?)<\/final insights>/s;
const match = output.match(pattern);
if (match) {
const [, summarization, finalInsights] = match;
const processedOutput = `${summarization.trim()}\n${finalInsights.trim()}`;
return processedOutput;
}
return output;
}

export function registerSummaryAssistantRoutes(
router: IRouter,
assistantService: AssistantServiceSetup
Expand Down Expand Up @@ -96,6 +107,7 @@ export function registerSummaryAssistantRoutes(
}),
},
},

router.handleLegacyErrors(async (context, req, res) => {
try {
const client = await getOpenSearchClientTransport({
Expand Down Expand Up @@ -190,7 +202,10 @@ export function registerData2SummaryRoutes(
}
);

const result = response.body.inference_results[0].output[0].result;
let result = response.body.inference_results[0].output[0].result;

result = postProcessing(result);

if (result) {
return res.ok({ body: result });
} else {
Expand Down

0 comments on commit 3c73361

Please sign in to comment.