在NextJS中修改OpenAI流

omqzjyyz  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(69)

我已经看到使用Vercel ai库快速创建可流式处理的OpenAI响应类型(例如https://stackoverflow.com/a/76634615/5348578)的示例,但是,我想在发送响应之前进行数据修改。
使用图书馆是否可行?如果不可行,正确的方法是什么?
目前,我正在尝试在不使用上述库的情况下执行以下操作:

const completionMessage = await openai.createChatCompletion({
        model: 'gpt-4',
        stream: true,
        messages: conversation,
    });

const transformStream = new Transform({
    transform(chunk, encoding, callback) {
        const stringData = chunk.toString();

        // Parse the Server-Sent Event
        const eventData = stringData.replace('data: ', '');
        /*
            If `stream` is set, partial message deltas will be sent. Tokens will be sent as data- 
            only server-sent events as they become available, with the stream terminated by a data: 
            [DONE] message.
        */
        if (eventData === '[DONE]') {
            return callback();
        }

        // Parse the data
        const data = JSON.parse(eventData);
        const message = data.choices[0].message?.content;

        const newData = {
            type: 'chat',
            data: {
                role: 'assistant',
                content: message,
            },
            myProperty: false,
        };

        this.push(JSON.stringify(newData));
        callback();
    },
});

const responseStream = completionMessage.body;

if (responseStream === null) {
    throw new Error('Response stream is null');
}

const writableStream = new WritableStream({
    write(chunk) {
        transformStream.write(chunk);
    },
    close() {
        transformStream.end();
    },
    abort(err) {
        transformStream.destroy(err);
    },
});

responseStream.pipeTo(writableStream);

return new NextResponse(transformStream);

字符串
但是,这种方法似乎不起作用,因为transformStream不是分配给NextResponse的有效类型。

4xy9mtcn

4xy9mtcn1#

你应该试试transformstream.readabledocs)。

相关问题