npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iflytek-spark-nodejs

v0.0.7

Published

讯飞星火认知大模型 Nodejs SDK

Downloads

14

Readme

Spark-Nodejs

讯飞星火认知大模型 Nodejs SDK & Web使用

install

npm i spark-nodejs

使用

const spark = new Spark({
  // 自行填入相关参数
  secret: "xxx",
  key: "xxx",
  appid: "xxx",
});
const answer = await spark.chat("你好");

构造参数

export interface ISparkOptions {
  secret: string;
  key: string;
  appid?: string;
  uid?: string;
  temperature?: number; // 取值为[0,1],默认为0.5	核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高
  maxTokens?: number; // 取值为[1,4096],默认为2048	模型回答的tokens的最大长度
  topK?: number; // 取值为[1,6],默认为4	从k个候选中随机选择⼀个(⾮等概率)
  chatId?: string; // 需要保障用户下的唯一性	用于关联用户会话
  useHistory?: boolean; // 是否需要使用历史对话记录,对token消耗会很快 default: false
}

API

generateUrl

生成websocket请求 url,可以下发给前端发起ws请求

const url = spark.generateUrl();

chat

直接使用node发起对话

const url = spark.chat({
  content: "你好",
  // onData 表示分段拿到返回结果
  onData({ content, start, end, seq }) {
    // content 表示分段的内容
    // start 表示是否是第一段
    // end 表示是否是最后一段
    // seq 表示序号
    console(content, start, end, seq);
  },
  onEnd({ content, token, questionTokens }) {
    // content 表示完整的返回
    // token 表示返回回答的token数
    // questionTokens 表示发起对话的token数
    console(content, start, end, seq);
  },
});

或者使用 chat的Promise返回

const answer = await spark.chat("你好");

函数声明

function chat(options: IQuestionOptions): Promise<string>;

interface IQuestionOptions {
  content: string;
  onData?(options: {
    content: string;
    start: boolean;
    end: boolean;
    seq: number;
  }): void;
  onEnd?(options: {
    content: string;
    tokens: number;
    questionTokens: number;
  }): void;
}

Web 端使用

spark-node中将消息部分单独打包到 socket 模块,可以在web环境中使用。使用场景为服务端生成url下发,web端调用socket模块发起聊天

import { SparkChat } from "spark-nodejs/chat";
const spark = new SparkChat({ url: "xxx" });
spark.chat({ content: "xxx" }); // chat 参数与上文中的chat参数一致

构造参数:

构造参数如下,除了多了 url 与 urlGetter 参数外,其他与上文一致,url与urlGetter填入一个即可

当填入url时,一次请求结束之后需要手动重新获取url设置

当填入urlGetter时,会自动获取url,推荐使用urlGetter

export interface ISparkSocketOptions {
  url?: string; // 指定url
  urlGetter?: () => Promise<string>; // 自动获取url的函数,一般封装获取url的请求
  appid?: string;
  uid?: string;
  temperature?: number; // 取值为[0,1],默认为0.5	核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高
  maxTokens?: number; // 取值为[1,4096],默认为2048	模型回答的tokens的最大长度
  topK?: number; // 取值为[1,6],默认为4	从k个候选中随机选择⼀个(⾮等概率)
  chatId?: string; // 需要保障用户下的唯一性	用于关联用户会话
  useHistory?: boolean; // default: false
}

cdn使用

<script src="https://cdn.jsdelivr.net/npm/spark-nodejs"></script>
<script>
  const spark = new SparkChat({ url: "xxx" });
</script>

关于 useHistory

开启了 useHistory 之后,Spark-Node 内部会缓存之前的对话上下文,然后在 chat 时一起发送给服务端,所以token消耗会很快,请按需要决定是否开启。

histroyToken的最大限制是8192,超过这个限制时Spark-Node会自动清除最早的记录,知道tokens小于8192。