Skip to content

Commit

Permalink
fix: migrate to @grafana/llm package for OpenAI functionality
Browse files Browse the repository at this point in the history
The @grafana/experimental package is being deprecated so development
of LLM-related functionality has been moved to a separate package.

The API is exactly the same so this _should_ just work.

I've had to fix the resolution of some Grafana packages which were
otherwise being duplicated because the @grafana/llm package has quite
a loose dependency range; without this, we had both version 10.4.1 and
11.3.0 of the various Grafana packages (and all of their
subdependencies) which was causing tests to fail.

Fixes #1051.
  • Loading branch information
sd2k committed Nov 5, 2024
1 parent 18124bd commit d9427ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"@grafana/azure-sdk": "0.0.3",
"@grafana/data": "10.4.1",
"@grafana/experimental": "^1.7.0",
"@grafana/llm": "^0.11.0",
"@grafana/runtime": "10.4.1",
"@grafana/schema": "10.4.1",
"@grafana/ui": "10.4.1",
Expand All @@ -100,7 +101,10 @@
"tslib": "2.6.2"
},
"resolutions": {
"**/@testing-library/dom": "7.31.2"
"**/@testing-library/dom": "7.31.2",
"@grafana/data": "10.4.1",
"@grafana/runtime": "10.4.1",
"@grafana/ui": "10.4.1"
},
"packageManager": "[email protected]"
}
8 changes: 4 additions & 4 deletions src/components/QueryEditor/OpenAIEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GrafanaTheme2, QueryEditorProps, SelectableValue } from '@grafana/data';
import { llms } from '@grafana/experimental';
import { openai } from '@grafana/llm';
import { getTemplateSrv, reportInteraction } from '@grafana/runtime';
import { Alert, Button, CodeEditor, Spinner, Monaco, MonacoEditor, useStyles2, TextArea, Stack } from '@grafana/ui';
import { AdxDataSource } from 'datasource';
Expand Down Expand Up @@ -41,7 +41,7 @@ export const OpenAIEditor: React.FC<RawQueryEditorProps> = (props) => {
const baselinePrompt = `You are an AI assistant that is fluent in KQL for querying Azure Data Explorer and you only respond with the correct KQL code snippets and no explanations. Generate a query that fulfills the following text.\nText:"""`;

useAsync(async () => {
const enabled = await llms.openai.enabled();
const enabled = await openai.enabled();
setEnabled(enabled);
});

Expand Down Expand Up @@ -87,15 +87,15 @@ export const OpenAIEditor: React.FC<RawQueryEditorProps> = (props) => {

const newGenerateQuery = () => {
reportInteraction('grafana_ds_adx_openai_query_generated_with_llm_plugin');
const stream = llms.openai
const stream = openai
.streamChatCompletions({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: baselinePrompt },
{ role: 'user', content: `${prompt}"""` },
],
})
.pipe(llms.openai.accumulateContent());
.pipe(openai.accumulateContent());
stream.subscribe({
next: (m) => {
setGeneratedQuery(m);
Expand Down
9 changes: 5 additions & 4 deletions src/components/QueryEditor/QueryHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
RadioButtonGroup,
useStyles2,
} from '@grafana/ui';
import { EditorHeader, FlexItem, InlineSelect, llms } from '@grafana/experimental';
import { EditorHeader, FlexItem, InlineSelect } from '@grafana/experimental';
import { openai } from '@grafana/llm';

import { AdxSchema, ClusterOption, defaultQuery, EditorMode, FormatOptions, KustoQuery } from '../../types';
import { AsyncState } from 'react-use/lib/useAsyncFn';
Expand Down Expand Up @@ -73,7 +74,7 @@ export const QueryHeader = (props: QueryEditorHeaderProps) => {
const styles = useStyles2(getStyles);

useAsync(async () => {
const enabled = await llms.openai.enabled();
const enabled = await openai.enabled();
setEnabled(enabled);
});

Expand Down Expand Up @@ -135,15 +136,15 @@ export const QueryHeader = (props: QueryEditorHeaderProps) => {
const showExplanation = () => {
setWaiting(true);
reportInteraction('grafana_ds_adx_openai_kql_query_explanation_generated_with_llm_plugin');
const stream = llms.openai
const stream = openai
.streamChatCompletions({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: baselinePrompt },
{ role: 'user', content: `${query.query}"""` },
],
})
.pipe(llms.openai.accumulateContent());
.pipe(openai.accumulateContent());
stream.subscribe({
next: (m) => {
setGeneratedExplanation(m);
Expand Down
64 changes: 32 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,7 @@
dependencies:
ts-jest "29.1.2"

"@grafana/[email protected]":
"@grafana/[email protected]", "@grafana/data@^10.4.0 ||^11":
version "10.4.1"
resolved "https://registry.yarnpkg.com/@grafana/data/-/data-10.4.1.tgz#bdd6a9de46cc310f35e38137b5eacb487eb06939"
integrity sha512-hgcFO0pBDddjIQ6NhyYR6I1+aLI0n8p+B7HioDLE4t5xvMDjZN8H3ItFmM9omjY5Qn+NsT/x2iaZrid1iR3kjQ==
Expand Down Expand Up @@ -2015,7 +2015,20 @@
ua-parser-js "^1.0.32"
web-vitals "^3.1.1"

"@grafana/[email protected]":
"@grafana/llm@^0.11.0":
version "0.11.0"
resolved "https://registry.yarnpkg.com/@grafana/llm/-/llm-0.11.0.tgz#4268a8255e3e60f0ce710f569852e98e6b12ba15"
integrity sha512-6IObi9ez1AunMwke/VFLmZCc4mBuqSGIOGcQ85JJmJgjaO900v2u9QzcF2yAZh24KGzLV2ILu39MtVvEOYfeCw==
dependencies:
"@grafana/data" "^10.4.0 ||^11"
"@grafana/runtime" "^10.4.0 || ^11"
react "^18"
react-use "^17.5.0"
rxjs "^7.8.1"
semver "^7.6.3"
uuid "^10.0.0"

"@grafana/[email protected]", "@grafana/runtime@^10.4.0 || ^11":
version "10.4.1"
resolved "https://registry.yarnpkg.com/@grafana/runtime/-/runtime-10.4.1.tgz#5c3b3f9d699d8e20887141b5d7c3a716b9634688"
integrity sha512-SoQxIQ6MtzP8FI3xB8v9Ethtjk73+K7xCxJgrKxdUbDlcs8MxYRjs4DxhX1B4u1CvULXRed6RQTkbCbi168P3A==
Expand Down Expand Up @@ -9862,7 +9875,7 @@ [email protected], react-use@^17.4.2:
ts-easing "^0.2.0"
tslib "^2.1.0"

react-use@^17.5.1:
react-use@^17.5.0, react-use@^17.5.1:
version "17.5.1"
resolved "https://registry.yarnpkg.com/react-use/-/react-use-17.5.1.tgz#19fc2ae079775d8450339e9fa8dbe25b17f2263c"
integrity sha512-LG/uPEVRflLWMwi3j/sZqR00nF6JGqTTDblkXK2nzXsIvij06hXl1V/MZIlwj1OKIQUtlh1l9jK8gLsRyCQxMg==
Expand Down Expand Up @@ -9897,6 +9910,13 @@ [email protected]:
dependencies:
loose-envify "^1.1.0"

react@^18:
version "18.3.1"
resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891"
integrity sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==
dependencies:
loose-envify "^1.1.0"

readable-stream@^1.1.7:
version "1.1.14"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.1.14.tgz#7cf4c54ef648e3813084c636dd2079e166c081d9"
Expand Down Expand Up @@ -10225,7 +10245,7 @@ rw@1, rw@^1.3.3:
resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4"
integrity sha512-PdhdWy89SiZogBLaw42zdeqtRJ//zFd2PgQavcICDUgJT5oW10QCRKbJ6bg4r0/UY2M6BWd5tkxuGFRvCkgfHQ==

[email protected]:
[email protected], rxjs@^7.8.1:
version "7.8.1"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.1.tgz#6f6f3d99ea8044291efd92e7c7fcf562c4057543"
integrity sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==
Expand Down Expand Up @@ -10757,16 +10777,7 @@ string-template@~0.2.1:
resolved "https://registry.yarnpkg.com/string-template/-/string-template-0.2.1.tgz#42932e598a352d01fc22ec3367d9d84eec6c9add"
integrity sha512-Yptehjogou2xm4UJbxJ4CxgZx12HBfeystp0y3x7s4Dj32ltVVG1Gg8YhKjHZkHicuKpZX/ffilA8505VbUbpw==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -10850,14 +10861,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -11481,6 +11485,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

uuid@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-10.0.0.tgz#5a95aa454e6e002725c79055fd42aaba30ca6294"
integrity sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==

uuid@^11.0.2:
version "11.0.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-11.0.2.tgz#a8d68ba7347d051e7ea716cc8dcbbab634d66875"
Expand Down Expand Up @@ -11768,7 +11777,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343"
integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -11786,15 +11795,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit d9427ac

Please sign in to comment.