wilsons 发表于 2025-11-7 06:54:41

LLMStream库,简单大模型请求,响应和实时渲染Markdown

本帖最后由 wilsons 于 2025-11-7 07:02 编辑

## 介绍
请求大模型并实时渲染Markdown非常常用,但每次都手动处理很麻烦,就让Claude4.5封装了一个库,简单测试了下还不错。

整理了两个版本:

1 https://scriptcat.org/zh-CN/script-show-page/4568 仅支持流式请求,更加轻量级(200+行代码),适合大多数情况使用。

2 https://scriptcat.org/zh-CN/script-show-page/4569 同时支持流式请求和非流式请求,更多功能(400+行代码),适合复杂场景使用。

使用示例:
```js
stream = new LLMStream({
    url: 'https://api-inference.modelscope.cn/v1/chat/completions',
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ms-xxxxxxxxx-xxxxxxx-xxxxxxx',
      'Content-Type': 'application/json'
    },
    body: {
      model: "Qwen/Qwen3-Coder-480B-A35B-Instruct",
      messages: [
      { role: "user", content: "请用 Markdown 写一个快速排序的 Python 实现,并解释步骤。" }
      ],
      stream: true,
      temperature: 0.7
    },
    target: '#output',
    markdown: true,
    onComplete: (content) => {
      console.log('完成!总字数:', content.length);
    },
    onError: (error) => {
      alert('错误: ' + error.message);
    }
});

stream.start();
```

更多使用方式请参考脚本库文档说明。

## 演示
为了更直观看到效果,演示一下吧

[!(https://b3logfile.com/file/2025/11/video-cover-2115-PixPin_2025-11-07_06-32-31-YlTYWhK.jpg)](https://b3logfile.com/file/2025/11/PixPin_2025-11-07_06-32-31-J4PZJDI.mp4)

---

## 吐槽

这里吐槽下:

可能是之前教本站被攻击的原因吧,现在更新限制的太死了,就更新3次,报错300秒更新次数太多,这。。。,然后一不小心算错时间了,立即变成3600秒更新次数太多,。。。好吧,我放弃更新了。

@王一之 这是否有点因噎废食了。5分钟3次,就是10次这也明显是人类操作,如果机器操作,都是1秒n次的。我们的目的是限制机器,不是限制人。更遑论4次后变成1小时呢,感觉有点太严格了。

王一之 发表于 2025-11-7 09:47:53

那我放宽一点吧
页: [1]
查看完整版本: LLMStream库,简单大模型请求,响应和实时渲染Markdown