Giter Club home page Giter Club logo

Comments (4)

suwubee avatar suwubee commented on July 18, 2024

ai写了个

 const readChunk = async (totalChunks = 0) => { // 将 totalChunks 作为参数
                return reader.read().then(async ({value, done}) => {
                        if (!done) {
                            value = decoder.decode(value);
                            let chunks = value.split(/\n{2}/g);
                            chunks = chunks.filter(item => {
                                return item.trim();
                            });
                            for (let i = 0; i < chunks.length; i++) {
                                let chunk = chunks[i];
                                if (chunk) {
                                    totalChunks++; // 每次处理一个 chunk 时递增
                                    let payload;
                                    try {
                                        payload = JSON.parse(chunk.slice(6));
                                    } catch (e) {
                                        break;
                                    }
                                    if (payload.choices[0].finish_reason) {
                                        let lenStop = payload.choices[0].finish_reason === "length";
                                        let longReplyFlag = enableLongReply && lenStop;
                                        if (!enableLongReply && lenStop) {currentResEle.children[1].children[0].className = "halfRefReq"}
                                        else {currentResEle.children[1].children[0].className = "refreshReq"};
                                        if (existVoice && enableAutoVoice && currentVoiceIdx === autoVoiceDataIdx) {
                                            let voiceText = longReplyFlag ? "" : progressData.slice(autoVoiceIdx), stop = !longReplyFlag;
                                            autoSpeechEvent(voiceText, currentResEle, false, stop);
                                        }
                                        break;
                                    } else {
                                        let content = payload.choices[0].delta.content;
                                        if (content) {
                                            if (!progressData && !content.trim()) continue;
                                            if (existVoice && enableAutoVoice && currentVoiceIdx === autoVoiceDataIdx) {
                                                let spliter = content.match(/\.|\?|!|。|?|!|\n/);
                                                if (spliter) {
                                                    let voiceText = progressData.slice(autoVoiceIdx) + content.slice(0, spliter.index + 1);
                                                    autoVoiceIdx += voiceText.length;
                                                    autoSpeechEvent(voiceText, currentResEle);
                                                }
                                            }
                                            if (progressData) await delay();
                                            progressData += content;
                                            currentResEle.children[0].innerHTML = md.render(progressData);
                                            if (!isRefresh) {
                                                scrollToBottom();
                                            }
                                        }
                                    }
                                }
                            }
                            return readChunk(totalChunks);
                        } else {
                            console.log('Total chunks processed:', totalChunks); // 输出总的 chunks 数量
                            if (isRefresh) {
                                data[refreshIdx].content = progressData;
                                if (longReplyFlag) return streamGen(true);
                            } else {
                                if (long) {data[data.length - 1].content = progressData}
                                else {data.push({role: "assistant", content: progressData})}
                                if (longReplyFlag) return streamGen(true);
                            }
                            stopLoading(false);
                        }
                    });
                };

from chatgpt-web.

suwubee avatar suwubee commented on July 18, 2024

然后再写一个tiktoken的服务,就可以统计输入的tokens了

from chatgpt-web.

xqdoo00o avatar xqdoo00o commented on July 18, 2024

是这样啊,我还在研究怎么前端实现tiktoken,,,不过输入确实要tiktoken才能算

from chatgpt-web.

suwubee avatar suwubee commented on July 18, 2024

是这样啊,我还在研究怎么前端实现tiktoken,,,不过输入确实要tiktoken才能算

好像只能自己封装api了。

from chatgpt-web.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.