From 8fa052d442bad6fa96a955bd260cafeb94f7251d Mon Sep 17 00:00:00 2001 From: gaobinlong Date: Tue, 19 Nov 2024 17:05:27 +0800 Subject: [PATCH 1/5] Optimize the response of executing AI agents Signed-off-by: gaobinlong --- server/routes/agent_routes.ts | 15 ++++++- server/routes/summary_routes.ts | 71 +++++++++++++++++++++----------- server/routes/text2viz_routes.ts | 12 +++++- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/server/routes/agent_routes.ts b/server/routes/agent_routes.ts index 57e85e66..2d351145 100644 --- a/server/routes/agent_routes.ts +++ b/server/routes/agent_routes.ts @@ -39,7 +39,20 @@ export function registerAgentRoutes(router: IRouter, assistantService: Assistant ); return res.ok({ body: response }); } catch (e) { - return res.badRequest(); + context.assistant_plugin.logger.debug('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } }) ); diff --git a/server/routes/summary_routes.ts b/server/routes/summary_routes.ts index 4300246c..8c8fc3e2 100644 --- a/server/routes/summary_routes.ts +++ b/server/routes/summary_routes.ts @@ -47,13 +47,24 @@ export function registerSummaryAssistantRoutes( req.body.index && req.body.dsl && req.body.topNLogPatternData ? LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID : SUMMARY_AGENT_CONFIG_ID; - const response = await assistantClient.executeAgentByConfigName(agentConfigId, { - context: req.body.context, - question: req.body.question, - index: req.body.index, - input: req.body.dsl, - topNLogPatternData: req.body.topNLogPatternData, - }); + + let response; + try { + response = await assistantClient.executeAgentByConfigName(agentConfigId, { + context: req.body.context, + question: req.body.question, + index: req.body.index, + input: req.body.dsl, + topNLogPatternData: req.body.topNLogPatternData, + }); + } catch (e) { + return res.customError({ + body: e.body || 'execute agent failed', + statusCode: e.statusCode || 500, + headers: e.headers, + }); + } + let summary; let insightAgentIdExists = false; try { @@ -78,6 +89,7 @@ export function registerSummaryAssistantRoutes( } }) ); + router.post( { path: SUMMARY_ASSISTANT_API.INSIGHT, @@ -105,11 +117,22 @@ export function registerSummaryAssistantRoutes( req.body.summaryType, client ); - const response = await assistantClient.executeAgent(insightAgentId, { - context: req.body.context, - summary: req.body.summary, - question: req.body.question, - }); + + let response; + try { + response = await assistantClient.executeAgent(insightAgentId, { + context: req.body.context, + summary: req.body.summary, + question: req.body.question, + }); + } catch (e) { + return res.customError({ + body: e.body || 'execute agent failed', + statusCode: e.statusCode || 500, + headers: e.headers, + }); + } + try { return res.ok({ body: response.body.inference_results[0].output[0].result }); } catch (e) { @@ -156,21 +179,23 @@ export function registerData2SummaryRoutes( }, router.handleLegacyErrors(async (context, req, res) => { const assistantClient = assistantService.getScopedClient(req, context); + let response; try { - const response = await assistantClient.executeAgentByConfigName( - DATA2SUMMARY_AGENT_CONFIG_ID, - { - sample_data: req.body.sample_data, - total_count: req.body.total_count, - sample_count: req.body.sample_count, - ppl: req.body.ppl, - question: req.body.question, - } - ); + response = await assistantClient.executeAgentByConfigName(DATA2SUMMARY_AGENT_CONFIG_ID, { + sample_data: req.body.sample_data, + total_count: req.body.total_count, + sample_count: req.body.sample_count, + ppl: req.body.ppl, + question: req.body.question, + }); const result = response.body.inference_results[0].output[0].result; return res.ok({ body: result }); } catch (e) { - return res.badRequest({ body: e }); + return res.customError({ + body: e.body || 'execute agent failed', + statusCode: e.statusCode || 500, + headers: e.headers, + }); } }) ); diff --git a/server/routes/text2viz_routes.ts b/server/routes/text2viz_routes.ts index 308808c5..c038546a 100644 --- a/server/routes/text2viz_routes.ts +++ b/server/routes/text2viz_routes.ts @@ -86,7 +86,11 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist } return res.badRequest(); } catch (e) { - return res.badRequest(); + return res.customError({ + body: e.body || 'execute agent failed', + statusCode: e.statusCode || 500, + headers: e.headers, + }); } }) ); @@ -115,7 +119,11 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist const result = JSON.parse(response.body.inference_results[0].output[0].result); return res.ok({ body: result }); } catch (e) { - return res.badRequest(); + return res.customError({ + body: e.body || 'execute agent failed', + statusCode: e.statusCode || 500, + headers: e.headers, + }); } }) ); From 63d6060634d73d074bff6befdef9080fa0e30ffa Mon Sep 17 00:00:00 2001 From: gaobinlong Date: Tue, 19 Nov 2024 17:16:22 +0800 Subject: [PATCH 2/5] Add change log Signed-off-by: gaobinlong --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b742e0..844385b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - feat: Hide navigate to discover button if alert is not from visual editor monitor([#368](https://github.com/opensearch-project/dashboards-assistant/pull/368)) ### Bug Fixes +- Optimize the response of AI agent APIs ([#373](https://github.com/opensearch-project/dashboards-assistant/pull/373)) + ### Infrastructure ### Documentation ### Maintenance -### Refactoring \ No newline at end of file +### Refactoring From fcbd5d2f67f08fabb3b6986dbfe9c257f4d0fe58 Mon Sep 17 00:00:00 2001 From: gaobinlong Date: Mon, 2 Dec 2024 14:27:08 +0800 Subject: [PATCH 3/5] Optimize the code Signed-off-by: gaobinlong --- server/routes/agent_routes.ts | 2 +- server/routes/summary_routes.ts | 57 +++++++++++++++++++++++--------- server/routes/text2viz_routes.ts | 19 ++++++++--- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/server/routes/agent_routes.ts b/server/routes/agent_routes.ts index 2d351145..c8a9f3eb 100644 --- a/server/routes/agent_routes.ts +++ b/server/routes/agent_routes.ts @@ -39,7 +39,7 @@ export function registerAgentRoutes(router: IRouter, assistantService: Assistant ); return res.ok({ body: response }); } catch (e) { - context.assistant_plugin.logger.debug('Execute agent failed!', e); + context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ body: e.body, diff --git a/server/routes/summary_routes.ts b/server/routes/summary_routes.ts index 8c8fc3e2..df093512 100644 --- a/server/routes/summary_routes.ts +++ b/server/routes/summary_routes.ts @@ -58,11 +58,20 @@ export function registerSummaryAssistantRoutes( topNLogPatternData: req.body.topNLogPatternData, }); } catch (e) { - return res.customError({ - body: e.body || 'execute agent failed', - statusCode: e.statusCode || 500, - headers: e.headers, - }); + context.assistant_plugin.logger.error('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } let summary; @@ -126,11 +135,20 @@ export function registerSummaryAssistantRoutes( question: req.body.question, }); } catch (e) { - return res.customError({ - body: e.body || 'execute agent failed', - statusCode: e.statusCode || 500, - headers: e.headers, - }); + context.assistant_plugin.logger.error('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } try { @@ -191,11 +209,20 @@ export function registerData2SummaryRoutes( const result = response.body.inference_results[0].output[0].result; return res.ok({ body: result }); } catch (e) { - return res.customError({ - body: e.body || 'execute agent failed', - statusCode: e.statusCode || 500, - headers: e.headers, - }); + context.assistant_plugin.logger.error('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } }) ); diff --git a/server/routes/text2viz_routes.ts b/server/routes/text2viz_routes.ts index c038546a..0b7c61f6 100644 --- a/server/routes/text2viz_routes.ts +++ b/server/routes/text2viz_routes.ts @@ -119,11 +119,20 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist const result = JSON.parse(response.body.inference_results[0].output[0].result); return res.ok({ body: result }); } catch (e) { - return res.customError({ - body: e.body || 'execute agent failed', - statusCode: e.statusCode || 500, - headers: e.headers, - }); + context.assistant_plugin.logger.error('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } }) ); From 035d6734138901d2877d181404947d3ca2123a87 Mon Sep 17 00:00:00 2001 From: gaobinlong Date: Mon, 2 Dec 2024 14:33:04 +0800 Subject: [PATCH 4/5] Optimize the response Signed-off-by: gaobinlong --- server/routes/text2viz_routes.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/server/routes/text2viz_routes.ts b/server/routes/text2viz_routes.ts index 0b7c61f6..6db25c55 100644 --- a/server/routes/text2viz_routes.ts +++ b/server/routes/text2viz_routes.ts @@ -86,11 +86,20 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist } return res.badRequest(); } catch (e) { - return res.customError({ - body: e.body || 'execute agent failed', - statusCode: e.statusCode || 500, - headers: e.headers, - }); + context.assistant_plugin.logger.error('Execute agent failed!', e); + if (e.statusCode >= 400 && e.statusCode <= 499) { + return res.customError({ + body: e.body, + statusCode: e.statusCode, + headers: e.headers, + }); + } else { + return res.customError({ + body: 'Execute agent failed!', + statusCode: 500, + headers: e.headers, + }); + } } }) ); From a2c4fbfe3f481357d8dbd7b867071b32f0e9c75d Mon Sep 17 00:00:00 2001 From: gaobinlong Date: Mon, 2 Dec 2024 19:47:05 +0800 Subject: [PATCH 5/5] Optimize the code Signed-off-by: gaobinlong --- server/routes/summary_routes.ts | 62 +++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/server/routes/summary_routes.ts b/server/routes/summary_routes.ts index df093512..79907f27 100644 --- a/server/routes/summary_routes.ts +++ b/server/routes/summary_routes.ts @@ -74,7 +74,6 @@ export function registerSummaryAssistantRoutes( } } - let summary; let insightAgentIdExists = false; try { if (req.body.insightType) { @@ -85,16 +84,20 @@ export function registerSummaryAssistantRoutes( )); } } catch (e) { - context.assistant_plugin.logger.debug( + context.assistant_plugin.logger.error( `Cannot find insight agent for ${req.body.insightType}`, e ); } - try { - summary = response.body.inference_results[0].output[0].result; + + const summary = response.body.inference_results[0]?.output[0]?.result; + if (summary) { return res.ok({ body: { summary, insightAgentIdExists } }); - } catch (e) { - return res.badRequest({ body: e }); + } else { + return res.customError({ + body: 'Execute agent failed with empty response!', + statusCode: 500, + }); } }) ); @@ -127,13 +130,22 @@ export function registerSummaryAssistantRoutes( client ); - let response; try { - response = await assistantClient.executeAgent(insightAgentId, { + const response = await assistantClient.executeAgent(insightAgentId, { context: req.body.context, summary: req.body.summary, question: req.body.question, }); + + const insight = response.body.inference_results[0]?.output[0]?.result; + if (insight) { + return res.ok({ body: { insight } }); + } else { + return res.customError({ + body: 'Execute agent failed with empty response!', + statusCode: 500, + }); + } } catch (e) { context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { @@ -150,12 +162,6 @@ export function registerSummaryAssistantRoutes( }); } } - - try { - return res.ok({ body: response.body.inference_results[0].output[0].result }); - } catch (e) { - return res.badRequest({ body: e }); - } }) ); } @@ -197,17 +203,27 @@ export function registerData2SummaryRoutes( }, router.handleLegacyErrors(async (context, req, res) => { const assistantClient = assistantService.getScopedClient(req, context); - let response; try { - response = await assistantClient.executeAgentByConfigName(DATA2SUMMARY_AGENT_CONFIG_ID, { - sample_data: req.body.sample_data, - total_count: req.body.total_count, - sample_count: req.body.sample_count, - ppl: req.body.ppl, - question: req.body.question, - }); + const response = await assistantClient.executeAgentByConfigName( + DATA2SUMMARY_AGENT_CONFIG_ID, + { + sample_data: req.body.sample_data, + total_count: req.body.total_count, + sample_count: req.body.sample_count, + ppl: req.body.ppl, + question: req.body.question, + } + ); + const result = response.body.inference_results[0].output[0].result; - return res.ok({ body: result }); + if (result) { + return res.ok({ body: result }); + } else { + return res.customError({ + body: 'Execute agent failed with empty response!', + statusCode: 500, + }); + } } catch (e) { context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) {