Skip to content

Commit

Permalink
Optimize the response of AI agent APIs (#373)
Browse files Browse the repository at this point in the history
* Optimize the response of executing AI agents

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

* Add change log

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

* Optimize the code

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

* Optimize the response

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

* Optimize the code

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

---------

Signed-off-by: gaobinlong <[email protected]>
  • Loading branch information
gaobinlong authored Dec 2, 2024
1 parent 22c1dd9 commit 581a7ca
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 26 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
### Refactoring
15 changes: 14 additions & 1 deletion server/routes/agent_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.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,
});
}
}
})
);
Expand Down
112 changes: 90 additions & 22 deletions server/routes/summary_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,33 @@ 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 summary;

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) {
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 insightAgentIdExists = false;
try {
if (req.body.insightType) {
Expand All @@ -65,19 +84,24 @@ 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,
});
}
})
);

router.post(
{
path: SUMMARY_ASSISTANT_API.INSIGHT,
Expand Down Expand Up @@ -105,15 +129,38 @@ 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,
});

try {
return res.ok({ body: response.body.inference_results[0].output[0].result });
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) {
return res.badRequest({ body: e });
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,
});
}
}
})
);
Expand Down Expand Up @@ -167,10 +214,31 @@ export function registerData2SummaryRoutes(
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) {
return res.badRequest({ body: e });
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,
});
}
}
})
);
Expand Down
30 changes: 28 additions & 2 deletions server/routes/text2viz_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist
}
return res.badRequest();
} catch (e) {
return res.badRequest();
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,
});
}
}
})
);
Expand Down Expand Up @@ -115,7 +128,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.badRequest();
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,
});
}
}
})
);
Expand Down

0 comments on commit 581a7ca

Please sign in to comment.