LLM 交互的前端功能
这是方便功能和组件的集合,使与Grafana的LLM功能的相互作用更加容易.
首先,增加最新版本 @grafana/llm 切换到软件包中的依赖性
{
"dependencies": {
"@grafana/llm": "0.22.0"
}
}
说明: 如果你正在写测试 从 @grafana/llm,您可能需要配置 Jest 来处理 ES 模块。 详见下方"杰斯"配置部分.
游戏配置
带有 Jest 的无害环境管理错误
写入从 @grafana/llm,你可能会遇到 Jest 错误像 SyntaxError: Cannot use import statement outside a module。这样做是因为 @grafana/llm 使用Jest需要改造的ES模块依赖性.
如果您正在使用Grafana的插件脚手架,请扩展你的Jest配置,以包括额外的ES模块. 为了方便, @grafana/llm 导出所需的模块列表 :
// jest.config.js
const { grafanaESModules, nodeModulesToTransform } = require('./.config/jest/utils');
const { grafanaLLMESModules } = require('@grafana/llm/jest');
module.exports = {
// Jest configuration provided by Grafana scaffolding
...require('./.config/jest.config'),
transformIgnorePatterns: [nodeModulesToTransform([...grafanaESModules, ...grafanaLLMESModules])],
};
MCP 功能性
如果您正在测试使用 MCP( 模式上下文协议) 特性的代码, 请添加 TransformStream 组合填充到您的 jest-setup.js数字 :
// jest-setup.js
// Jest setup provided by Grafana scaffolding
import './.config/jest-setup';
// Add this import and global for MCP functionality
import { TransformStream } from 'node:stream/web';
import { TextEncoder } from 'util';
// TextEncoder may already be present in your setup
global.TextEncoder = TextEncoder;
// Add this line for MCP TransformStream support
global.TransformStream = TransformStream;
使用量
然后在你的组件中,你可以使用 llm 对象从 @grafana/llm 像这样:
import React, { useState } from 'react';
import { useAsync } from 'react-use';
import { scan } from 'rxjs/operators';
import { openai } from '@grafana/llm';
import { PluginPage } from '@grafana/runtime';
import { Button, Input, Spinner } from '@grafana/ui';
const MyComponent = (): JSX.Element => {
const [input, setInput] = React.useState('');
const [message, setMessage] = React.useState('');
const [reply, setReply] = useState('');
const { loading, error } = useAsync(async () => {
const enabled = await openai.enabled();
if (!enabled) {
return false;
}
if (message === '') {
return;
}
// Stream the completions. Each element is the next stream chunk.
const stream = openai
.streamChatCompletions({
// model: openai.Model.LARGE, // defaults to BASE, use larger model for longer context and complex tasks
messages: [
{ role: 'system', content: 'You are a helpful assistant with deep knowledge of the Grafana, Prometheus and general observability ecosystem.' },
{ role: 'user', content: message },
],
})
.pipe(
// Accumulate the stream chunks into a single string.
scan((acc, delta) => acc + delta, '')
);
// Subscribe to the stream and update the state for each returned value.
return stream.subscribe(setReply);
}, [message]);
if (error) {
// TODO: handle errors.
return null;
}
return (
<div>
<Input value={input} onChange={(e) => setInput(e.currentTarget.value)} placeholder="Enter a message" />
<br />
<Button type="submit" onClick={() => setMessage(input)}>
Submit
</Button>
<br />
<div>{loading ? <Spinner /> : reply}</div>
</div>
);
};