文档

集成

纯文本生成器

测试版功能

插件支持目前处于内部测试阶段。在此加入测试

生成器接收生成器控制器和当前的对话状态,开始生成,然后使用 ctl.fragmentGenerated 方法报告生成的文本。

以下是一个简单生成器的示例,它以每词 200 毫秒的延迟回显最后一条用户消息

import { Chat, GeneratorController } from "@lmstudio/sdk";

export async function generate(ctl: GeneratorController, chat: Chat) {
  // Just echo back the last message
  const lastMessage = chat.at(-1).getText();
  // Split the last message into words
  const words = lastMessage.split(/(?= )/);
  for (const word of words) {
    ctl.fragmentGenerated(word); // Send each word as a fragment
    ctl.abortSignal.throwIfAborted(); // Allow for cancellation
    await new Promise((resolve) => setTimeout(resolve, 200)); // Simulate some processing time
  }
}

自定义配置

您可以通过 ctl.getPluginConfigctl.getGlobalPluginConfig 访问自定义配置。有关更多详细信息,请参阅自定义配置

处理中断

当您的生成器仍在运行时,用户可能会中止预测。在这种情况下,您应该通过处理 ctl.abortSignal 来优雅地处理中止。

您可以在 MDN 文档中了解有关 AbortSignal 的更多信息。

此页面的源代码可在 GitHub 上获取