forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Logs Shared] Extract AI Assistant into reusable component (elastic#1…
…70496) ## 📓 Summary Part of elastic#169506 The reason behind exposing this component is that we'll use the same configuration and prompts to generate insights about log entries on different touchpoints: - Currently implemented, show AI insights on the LogStream flyout detail - To implement (follow up PR), show AI insights on the Log Explorer flyout detail These changes expose a new LogAIAssistant component in 2 ways: - Consume the component from the `logs-shared` plugin start contract. - Import the component from the plugin bundle. In both ways the component come lazy-loaded, the main difference is that consuming it from the start contract will pre-inject the aiAssistant dependency in the component. ```ts // Usage from plugin contract const {services} = useKibana() const { LogAIAssistant } = services.logsShared <LogAIAssistant doc={logEntry} /> // Usage from component import import { LogAIAssistant } from '@kbn/logs-shared-plugin/public'; const {services} = useKibana() <LogAIAssistant aiAssistant={services.observabilityAIAssistant} doc={logEntry} /> ``` To avoid mixing the registration of external components into the Log Explorer, I decided to split this work into different PRs to keep the changes scoped. --------- Co-authored-by: Marco Antonio Ghiani <[email protected]>
- Loading branch information
1 parent
1f2b07c
commit 09ff2c4
Showing
7 changed files
with
166 additions
and
94 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
x-pack/plugins/logs_shared/public/components/log_ai_assistant/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import React from 'react'; | ||
import { Optional } from '@kbn/utility-types'; | ||
import { dynamic } from '../../../common/dynamic'; | ||
import type { LogAIAssistantProps } from './log_ai_assistant'; | ||
|
||
export const LogAIAssistant = dynamic(() => import('./log_ai_assistant')); | ||
|
||
interface LogAIAssistantFactoryDeps { | ||
observabilityAIAssistant: LogAIAssistantProps['aiAssistant']; | ||
} | ||
|
||
export function createLogAIAssistant({ observabilityAIAssistant }: LogAIAssistantFactoryDeps) { | ||
return ({ | ||
aiAssistant = observabilityAIAssistant, | ||
...props | ||
}: Optional<LogAIAssistantProps, 'aiAssistant'>) => ( | ||
<LogAIAssistant aiAssistant={aiAssistant} {...props} /> | ||
); | ||
} |
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/logs_shared/public/components/log_ai_assistant/log_ai_assistant.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useMemo } from 'react'; | ||
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; | ||
import { | ||
ContextualInsight, | ||
type Message, | ||
ObservabilityAIAssistantPluginStart, | ||
MessageRole, | ||
} from '@kbn/observability-ai-assistant-plugin/public'; | ||
import { LogEntryField } from '../../../common'; | ||
import { explainLogMessageTitle, similarLogMessagesTitle } from './translations'; | ||
|
||
export interface LogAIAssistantDocument { | ||
fields: LogEntryField[]; | ||
} | ||
|
||
export interface LogAIAssistantProps { | ||
aiAssistant: ObservabilityAIAssistantPluginStart; | ||
doc: LogAIAssistantDocument | undefined; | ||
} | ||
|
||
export function LogAIAssistant({ aiAssistant, doc }: LogAIAssistantProps) { | ||
const explainLogMessageMessages = useMemo<Message[] | undefined>(() => { | ||
if (!doc) { | ||
return undefined; | ||
} | ||
|
||
const now = new Date().toISOString(); | ||
|
||
return [ | ||
{ | ||
'@timestamp': now, | ||
message: { | ||
role: MessageRole.User, | ||
content: `I'm looking at a log entry. Can you explain me what the log message means? Where it could be coming from, whether it is expected and whether it is an issue. Here's the context, serialized: ${JSON.stringify( | ||
{ logEntry: { fields: doc.fields } } | ||
)} `, | ||
}, | ||
}, | ||
]; | ||
}, [doc]); | ||
|
||
const similarLogMessageMessages = useMemo<Message[] | undefined>(() => { | ||
if (!doc) { | ||
return undefined; | ||
} | ||
|
||
const now = new Date().toISOString(); | ||
|
||
const message = doc.fields.find((field) => field.field === 'message')?.value[0]; | ||
|
||
return [ | ||
{ | ||
'@timestamp': now, | ||
message: { | ||
role: MessageRole.User, | ||
content: `I'm looking at a log entry. Can you construct a Kibana KQL query that I can enter in the search bar that gives me similar log entries, based on the \`message\` field: ${message}`, | ||
}, | ||
}, | ||
]; | ||
}, [doc]); | ||
|
||
return ( | ||
<EuiFlexGroup direction="column" gutterSize="m"> | ||
{aiAssistant.isEnabled() && explainLogMessageMessages ? ( | ||
<EuiFlexItem grow={false}> | ||
<ContextualInsight title={explainLogMessageTitle} messages={explainLogMessageMessages} /> | ||
</EuiFlexItem> | ||
) : null} | ||
{aiAssistant.isEnabled() && similarLogMessageMessages ? ( | ||
<EuiFlexItem grow={false}> | ||
<ContextualInsight title={similarLogMessagesTitle} messages={similarLogMessageMessages} /> | ||
</EuiFlexItem> | ||
) : null} | ||
</EuiFlexGroup> | ||
); | ||
} | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default LogAIAssistant; |
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/logs_shared/public/components/log_ai_assistant/translations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
export const explainLogMessageTitle = i18n.translate( | ||
'xpack.logsShared.logFlyout.explainLogMessageTitle', | ||
{ | ||
defaultMessage: "What's this message?", | ||
} | ||
); | ||
|
||
export const similarLogMessagesTitle = i18n.translate( | ||
'xpack.logsShared.logFlyout.similarLogMessagesTitle', | ||
{ | ||
defaultMessage: 'How do I find similar log messages?', | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters