Skip to content
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

Optimize the response of AI agent APIs #373

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
98 changes: 75 additions & 23 deletions server/routes/summary_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +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 response;
try {
response = await assistantClient.executeAgentByConfigName(agentConfigId, {
xluo-aws marked this conversation as resolved.
Show resolved Hide resolved
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 summary;
let insightAgentIdExists = false;
try {
Expand All @@ -78,6 +98,7 @@ export function registerSummaryAssistantRoutes(
}
})
);

gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
router.post(
{
path: SUMMARY_ASSISTANT_API.INSIGHT,
Expand Down Expand Up @@ -105,11 +126,31 @@ 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) {
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 {
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
return res.ok({ body: response.body.inference_results[0].output[0].result });
} catch (e) {
Expand Down Expand Up @@ -156,21 +197,32 @@ 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 });
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
Loading