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

BugFix: [AlertInsight] Remove the cache of insight agent id in node server #343

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- feat: report metrics for text to visualization([#312](https://github.com/opensearch-project/dashboards-assistant/pull/312))
- fix: Fix dynamic uses of i18n([#335](https://github.com/opensearch-project/dashboards-assistant/pull/335))
- fix: Fix unrecognized creating index pattern duplicate cases([#337](https://github.com/opensearch-project/dashboards-assistant/pull/337))
- fix: Remove the cache of insight agent id in node server([#343](https://github.com/opensearch-project/dashboards-assistant/pull/343))

### 📈 Features/Enhancements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const GeneratePopoverBody: React.FC<{
await httpSetup
?.post(SUMMARY_ASSISTANT_API.SUMMARIZE, {
body: JSON.stringify({
type: summaryType,
summaryType,
insightType,
question: summarizationQuestion,
context: contextContent,
Expand All @@ -122,7 +122,7 @@ export const GeneratePopoverBody: React.FC<{
.then((response) => {
const summaryContent = response.summary;
setSummary(summaryContent);
const insightAgentIdExists = insightType !== undefined && response.insightAgentIdExists;
const insightAgentIdExists = !!insightType && response.insightAgentIdExists;
setInsightAvailable(insightAgentIdExists);
if (insightAgentIdExists) {
onGenerateInsightBasedOnSummary(
Expand Down
71 changes: 34 additions & 37 deletions server/routes/summary_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
*/

import { schema } from '@osd/config-schema';
import { IRouter } from '../../../../src/core/server';
import { IRouter, OpenSearchClient, RequestHandlerContext } from '../../../../src/core/server';
import { SUMMARY_ASSISTANT_API } from '../../common/constants/llm';
import { getOpenSearchClientTransport } from '../utils/get_opensearch_client_transport';
import { getAgentIdByConfigName, searchAgent } from './get_agent';
import { AssistantServiceSetup } from '../services/assistant_service';

const SUMMARY_AGENT_CONFIG_ID = 'os_summary';
const LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID = 'os_summary_with_logPattern';
const LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID = 'os_summary_with_log_pattern';
const OS_INSIGHT_AGENT_CONFIG_ID = 'os_insight';
const DATA2SUMMARY_AGENT_CONFIG_ID = 'os_data2summary';
let osInsightAgentId: string | undefined;
let userInsightAgentId: string | undefined;

export function registerSummaryAssistantRoutes(
router: IRouter,
Expand All @@ -26,7 +24,7 @@ export function registerSummaryAssistantRoutes(
path: SUMMARY_ASSISTANT_API.SUMMARIZE,
validate: {
body: schema.object({
type: schema.string(),
summaryType: schema.string(),
insightType: schema.maybe(schema.string()),
question: schema.string(),
context: schema.maybe(schema.string()),
Expand Down Expand Up @@ -60,32 +58,23 @@ export function registerSummaryAssistantRoutes(
let insightAgentIdExists = false;
try {
if (req.body.insightType) {
// We have separate agent for os_insight and user_insight. And for user_insight, we can
// only get it by searching on name since it is not stored in agent config.
if (req.body.insightType === 'os_insight') {
if (!osInsightAgentId) {
osInsightAgentId = await getAgentIdByConfigName(OS_INSIGHT_AGENT_CONFIG_ID, client);
}
insightAgentIdExists = !!osInsightAgentId;
} else if (req.body.insightType === 'user_insight') {
if (req.body.type === 'alerts') {
if (!userInsightAgentId) {
userInsightAgentId = await searchAgent({ name: 'KB_For_Alert_Insight' }, client);
}
}
insightAgentIdExists = !!userInsightAgentId;
}
insightAgentIdExists = !!(await detectInsightAgentId(
req.body.insightType,
req.body.summaryType,
client
));
}
} catch (e) {
context.assistant_plugin.logger.info(
`Cannot find insight agent for ${req.body.insightType}`
context.assistant_plugin.logger.debug(
`Cannot find insight agent for ${req.body.insightType}`,
e
);
}
try {
summary = response.body.inference_results[0].output[0].result;
return res.ok({ body: { summary, insightAgentIdExists } });
} catch (e) {
return res.internalError();
return res.badRequest({ body: e });
}
})
);
Expand All @@ -110,15 +99,12 @@ export function registerSummaryAssistantRoutes(
context,
dataSourceId: req.query.dataSourceId,
});
const insightAgentId =
req.body.insightType === 'os_insight' ? osInsightAgentId : userInsightAgentId;
if (!insightAgentId) {
context.assistant_plugin.logger.info(
`Cannot find insight agent for ${req.body.insightType}`
);
return res.internalError();
}
const assistantClient = assistantService.getScopedClient(req, context);
const insightAgentId = await detectInsightAgentId(
req.body.insightType,
req.body.summaryType,
client
);
const response = await assistantClient.executeAgent(insightAgentId, {
context: req.body.context,
summary: req.body.summary,
Expand All @@ -127,16 +113,27 @@ export function registerSummaryAssistantRoutes(
try {
return res.ok({ body: response.body.inference_results[0].output[0].result });
} catch (e) {
return res.internalError();
} finally {
// Reset both agents id in case of update
userInsightAgentId = undefined;
osInsightAgentId = undefined;
return res.badRequest({ body: e });
}
})
);
}

function detectInsightAgentId(
insightType: string,
summaryType: string,
client: OpenSearchClient['transport']
) {
// We have separate agent for os_insight and user_insight. And for user_insight, we can
// only get it by searching on name since it is not stored in agent config.
if (insightType === 'os_insight') {
return getAgentIdByConfigName(OS_INSIGHT_AGENT_CONFIG_ID, client);
} else if (insightType === 'user_insight' && summaryType === 'alerts') {
return searchAgent({ name: 'KB_For_Alert_Insight' }, client);
}
qianheng-aws marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}

export function registerData2SummaryRoutes(
router: IRouter,
assistantService: AssistantServiceSetup
Expand Down Expand Up @@ -173,7 +170,7 @@ export function registerData2SummaryRoutes(
const result = response.body.inference_results[0].output[0].result;
return res.ok({ body: result });
} catch (e) {
return res.internalError();
return res.badRequest({ body: e });
}
})
);
Expand Down
Loading