@grafana/llm

中文文档
1.0.9发布于 2026/06/12Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 10.1万

A library for working with LLMs in Grafana plugins

原始使用文档
文档目录4 节
  1. Jest Configuration
  2. ESM errors with Jest
  3. MCP Functionality
  4. Usage

Frontend functions for LLM interaction

This is a collection of convenience functions and components to make interacting with LLM functionality in Grafana easier.

First, add the latest version of @grafana/llm to your dependencies in package.json:

{
  "dependencies": {
    "@grafana/llm": "0.22.0"
  }
}

Note: If you're writing tests that import from @grafana/llm, you may need to configure Jest to handle ES modules. See the Jest Configuration section below for details.

Jest Configuration

ESM errors with Jest

When writing tests that import from @grafana/llm, you may encounter Jest errors like SyntaxError: Cannot use import statement outside a module. This happens because @grafana/llm uses ES module dependencies that Jest needs to transform.

If you're using Grafana's plugin scaffolding, extend your Jest configuration to include the additional ES modules. For convenience, @grafana/llm exports the required module list:

// 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 Functionality

If you're testing code that uses MCP (Model Context Protocol) features, add the TransformStream polyfill to your 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;

Usage

Then in your components you can use the llm object from @grafana/llm like so:

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>
  );
};

版本与发布记录

@grafana/llm 共发布 26 个版本,首次发布于 2024/03/13;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.9latest2026/06/125 个月前
1.0.82026/04/236 个月前
1.0.32026/02/069 个月前
1.0.22026/01/0710 个月前
1.0.12025/12/1211 个月前
0.22.12025/06/161.4 年前

关于 @grafana/llm 的常见问题

@grafana/llm 是什么?

A library for working with LLMs in Grafana plugins

如何安装 @grafana/llm?

在项目目录运行 npm install @grafana/llm,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxjs(^7.8.2)、react(^18)、@grafana/data(^10.4.0 ||^11 || ^12)、@grafana/runtime(^10.4.0 || ^11 || ^12)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@grafana/llm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@grafana/llm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@grafana/llm 可以商用吗?

@grafana/llm 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@grafana/llm 的下载量有多少?

根据 npm 官方下载统计,@grafana/llm 在 2026/10/17 – 2026/11/15 这 30 天内被下载 100,718 次,日均约 3,357 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@grafana/llm 还在维护吗?

最新版本 1.0.9 发布于 2026/06/12(5 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 5 个;首次发布于 2024/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。