-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add panel explainer plugin extension link
Adds an extension link to the plugin which appears in the panel menu and produces a modal explaining the panel, by sending the JSON representation to OpenAI.
- Loading branch information
Showing
8 changed files
with
113 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { panelExplainer } from './panelExplainer'; |
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,63 @@ | ||
import React from 'react'; | ||
|
||
import { PluginExtensionLinkConfig, PluginExtensionPanelContext, PluginExtensionPoints, PluginExtensionTypes } from "@grafana/data" | ||
import { getBackendSrv } from '@grafana/runtime'; | ||
import { useAsync } from 'react-use'; | ||
import { Spinner } from '@grafana/ui'; | ||
import { Dashboard } from '@grafana/schema'; | ||
import { chatCompletions } from 'utils/utils.api'; | ||
|
||
interface ExplainPanelModalProps { | ||
context: PluginExtensionPanelContext, | ||
} | ||
|
||
const panelExplainerPrompt = 'Given the following JSON representation of a Grafana panel, explain what it shows. Be fairly brief in your summary, and use the present tense.'; | ||
|
||
const ExplainPanelModal = ({ context }: ExplainPanelModalProps) => { | ||
const backendSrv = getBackendSrv(); | ||
const state = useAsync(async () => { | ||
const dashboardJSON = await backendSrv.get(`/api/dashboards/uid/${context.dashboard.uid}`) as { dashboard: Dashboard }; | ||
const panelJSON = dashboardJSON.dashboard.panels?.find( | ||
// @ts-ignore | ||
(panel) => panel.id === context.id | ||
); | ||
const userPrompt = JSON.stringify(panelJSON, null, 2); | ||
const response = await chatCompletions({ | ||
model: 'gpt-3.5-turbo', | ||
systemPrompt: panelExplainerPrompt, | ||
userPrompt, | ||
}); | ||
return response; | ||
}); | ||
if (state.loading) { | ||
return <Spinner /> | ||
} | ||
if (state.error || state.value === undefined) { | ||
return null; | ||
} | ||
return ( | ||
<div> | ||
{state.value} | ||
</div> | ||
) | ||
} | ||
|
||
export const panelExplainer: PluginExtensionLinkConfig<PluginExtensionPanelContext> = { | ||
title: 'Explain this panel', | ||
description: 'Explain this panel', | ||
type: PluginExtensionTypes.link, | ||
extensionPointId: PluginExtensionPoints.DashboardPanelMenu, | ||
onClick: (event, { context, openModal }) => { | ||
if (event !== undefined) { | ||
event.preventDefault(); | ||
} | ||
if (context === undefined) { | ||
return; | ||
} | ||
openModal({ | ||
title: 'Panel explanation', | ||
body: () => <ExplainPanelModal context={context} />, | ||
}); | ||
}, | ||
}; | ||
|
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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { AppPlugin } from '@grafana/data'; | ||
|
||
import { App } from './components/App'; | ||
import { AppConfig } from './components/AppConfig'; | ||
import { panelExplainer } from './extensions'; | ||
|
||
export const plugin = new AppPlugin<{}>().setRootPage(App).addConfigPage({ | ||
title: 'Configuration', | ||
icon: 'cog', | ||
body: AppConfig, | ||
id: 'configuration', | ||
}); | ||
export const plugin = new AppPlugin<{}>() | ||
.setRootPage(App) | ||
.addConfigPage({ | ||
title: 'Configuration', | ||
icon: 'cog', | ||
body: AppConfig, | ||
id: 'configuration', | ||
}) | ||
.configureExtensionLink(panelExplainer); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getBackendSrv } from "@grafana/runtime"; | ||
|
||
export interface ChatCompletionsProps { | ||
model: string; | ||
systemPrompt: string; | ||
userPrompt: string; | ||
} | ||
interface Choice { | ||
message: { | ||
content: string; | ||
} | ||
} | ||
|
||
interface ChatCompletionsResponse { | ||
choices: Choice[]; | ||
} | ||
|
||
export const chatCompletions = async ({ model, systemPrompt, userPrompt }: ChatCompletionsProps): Promise<string> => { | ||
const response = await getBackendSrv().post<ChatCompletionsResponse>('/api/plugins/grafana-llm-app/resources/openai/v1/chat/completions', { | ||
model, | ||
messages: [ | ||
{ role: 'system', content: systemPrompt }, | ||
{ role: 'user', content: userPrompt }, | ||
], | ||
}, { headers: { 'Content-Type': 'application/json' } }); | ||
return response.choices[0].message.content; | ||
} |