UNPKG

zhipu-sdk-js

Version:

[官方接口文档](https://www.bigmodel.cn/dev/api/normal-model/glm-4)

2 lines (1 loc) 1.03 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("axios")):"function"==typeof define&&define.amd?define(["axios"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).ZhipuSDK=t(e.axios)}(this,(function(e){"use strict";return class{axios;constructor(t){this.axios=e.create({headers:{Authorization:t.apiKey},baseURL:"https://open.bigmodel.cn/api/paas/v4",withCredentials:!0})}async createCompletions(e,t){const s=await this.axios({method:"POST",url:"/chat/completions",responseType:e.stream?"stream":"json",data:{...e,stream:!!e.stream}});if(e.stream){let a,o="",i="";for await(const n of s.data){const s=(o+n.toString()).split("\n");o="",s.forEach((s=>{if(s.startsWith("data:"))if("data: [DONE]"===s)t?.onProgressEnd?.(a);else try{if(s.endsWith("}")){const o=JSON.parse(s.replace("data: ",""));"append"===e.stream&&(i+=o.choices[0].delta.content,o.choices[0].message=i),a=o,t?.onProgress?.(o)}else o+=s}catch(e){o+=s}else o+=s}))}return a}return t?.onProgress?.(s.data),s.data}}}));