我已经看到使用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
的有效类型。
1条答案
按热度按时间4xy9mtcn1#
你应该试试
transformstream.readable
(docs)。