Skip to content

Commit

Permalink
♻️ refactor: change empty content stream behavior (#3883)
Browse files Browse the repository at this point in the history
* ♻️ refactor: change empty content stream behavior

* 🐛 fix: fix one-api

---------

Co-authored-by: arvinxx <[email protected]>
  • Loading branch information
hezhijie0327 and arvinxx authored Sep 10, 2024
1 parent 8a1b3ec commit e910f68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/libs/agent-runtime/utils/streams/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,40 @@ describe('OpenAIStream', () => {
expect(chunks).toEqual(['id: 3\n', 'event: data\n', `data: {"content":null}\n\n`]);
});

it('should handle content with finish_reason', async () => {
const mockOpenAIStream = new ReadableStream({
start(controller) {
controller.enqueue({
id: '123',
model: 'deepl',
choices: [
{
index: 0,
delta: { role: 'assistant', content: 'Introduce yourself.' },
finish_reason: 'stop',
},
],
});

controller.close();
},
});

const protocolStream = OpenAIStream(mockOpenAIStream);

const decoder = new TextDecoder();
const chunks = [];

// @ts-ignore
for await (const chunk of protocolStream) {
chunks.push(decoder.decode(chunk, { stream: true }));
}

expect(chunks).toEqual(
['id: 123', 'event: text', `data: "Introduce yourself."\n`].map((i) => `${i}\n`),
);
});

it('should handle other delta data', async () => {
const mockOpenAIStream = new ReadableStream({
start(controller) {
Expand Down
15 changes: 11 additions & 4 deletions src/libs/agent-runtime/utils/streams/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export const transformOpenAIStream = (
return { data: chunk, id: chunk.id, type: 'data' };
}

if (typeof item.delta?.content === 'string' && !item.finish_reason && !item.delta?.tool_calls) {
return { data: item.delta.content, id: chunk.id, type: 'text' };
}

if (item.delta?.tool_calls) {
return {
data: item.delta.tool_calls.map((value, index): StreamToolCallChunkData => {
Expand Down Expand Up @@ -60,9 +56,20 @@ export const transformOpenAIStream = (

// 给定结束原因
if (item.finish_reason) {
// one-api 的流式接口,会出现既有 finish_reason ,也有 content 的情况
// {"id":"demo","model":"deepl-en","choices":[{"index":0,"delta":{"role":"assistant","content":"Introduce yourself."},"finish_reason":"stop"}]}

if (typeof item.delta?.content === 'string' && !!item.delta.content) {
return { data: item.delta.content, id: chunk.id, type: 'text' };
}

return { data: item.finish_reason, id: chunk.id, type: 'stop' };
}

if (typeof item.delta?.content === 'string') {
return { data: item.delta.content, id: chunk.id, type: 'text' };
}

if (item.delta?.content === null) {
return { data: item.delta, id: chunk.id, type: 'data' };
}
Expand Down

0 comments on commit e910f68

Please sign in to comment.