@xsai/stream-text 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/dist/index.d.ts +26 -4
- package/dist/index.js +19 -29
- package/package.json +8 -5
package/LICENSE.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
|
|
2
2
|
|
|
3
|
-
interface StreamTextOptions extends
|
|
3
|
+
interface StreamTextOptions extends ChatCompletionOptions {
|
|
4
4
|
streamOptions?: {
|
|
5
5
|
/**
|
|
6
6
|
* Return usage.
|
|
@@ -10,10 +10,32 @@ interface StreamTextOptions extends GenerateTextOptions {
|
|
|
10
10
|
usage?: boolean;
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
|
+
interface StreamTextResponseUsage {
|
|
14
|
+
completion_tokens: number;
|
|
15
|
+
prompt_tokens: number;
|
|
16
|
+
total_tokens: number;
|
|
17
|
+
}
|
|
13
18
|
interface StreamTextResult {
|
|
19
|
+
chunkStream: ReadableStream<StreamTextResponse>;
|
|
14
20
|
finishReason?: FinishReason;
|
|
15
21
|
textStream: ReadableStream<string>;
|
|
16
|
-
usage?:
|
|
22
|
+
usage?: StreamTextResponseUsage;
|
|
23
|
+
}
|
|
24
|
+
interface StreamTextResponse {
|
|
25
|
+
choices: {
|
|
26
|
+
delta: {
|
|
27
|
+
content: string;
|
|
28
|
+
role: 'assistant';
|
|
29
|
+
};
|
|
30
|
+
finish_reason?: FinishReason;
|
|
31
|
+
index: number;
|
|
32
|
+
}[];
|
|
33
|
+
created: number;
|
|
34
|
+
id: string;
|
|
35
|
+
model: string;
|
|
36
|
+
object: 'chat.completion.chunk';
|
|
37
|
+
system_fingerprint: string;
|
|
38
|
+
usage?: StreamTextResponseUsage;
|
|
17
39
|
}
|
|
18
40
|
/**
|
|
19
41
|
* @experimental
|
|
@@ -21,4 +43,4 @@ interface StreamTextResult {
|
|
|
21
43
|
*/
|
|
22
44
|
declare const streamText: (options: StreamTextOptions) => Promise<StreamTextResult>;
|
|
23
45
|
|
|
24
|
-
export { type StreamTextOptions, type StreamTextResult, streamText as default, streamText };
|
|
46
|
+
export { type StreamTextOptions, type StreamTextResponse, type StreamTextResponseUsage, type StreamTextResult, streamText as default, streamText };
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,37 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { chatCompletion } from '@xsai/shared-chat-completion';
|
|
2
2
|
|
|
3
|
-
const streamText = async (options) => await
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
abortSignal: void 0,
|
|
7
|
-
base: void 0,
|
|
8
|
-
headers: void 0,
|
|
9
|
-
path: void 0,
|
|
10
|
-
stream: true,
|
|
11
|
-
tools: options.tools?.map((tool) => ({
|
|
12
|
-
function: tool.function,
|
|
13
|
-
type: "function"
|
|
14
|
-
}))
|
|
15
|
-
})),
|
|
16
|
-
headers: {
|
|
17
|
-
"Content-Type": "application/json",
|
|
18
|
-
...options.headers
|
|
19
|
-
},
|
|
20
|
-
method: "POST",
|
|
21
|
-
signal: options.abortSignal
|
|
3
|
+
const streamText = async (options) => await chatCompletion({
|
|
4
|
+
...options,
|
|
5
|
+
stream: true
|
|
22
6
|
}).then((res) => {
|
|
7
|
+
if (!res.body) {
|
|
8
|
+
return Promise.reject(res);
|
|
9
|
+
}
|
|
10
|
+
const decoder = new TextDecoder();
|
|
23
11
|
let finishReason;
|
|
24
12
|
let usage;
|
|
25
|
-
const
|
|
26
|
-
const transformStream = new TransformStream({
|
|
13
|
+
const rawChunkStream = res.body.pipeThrough(new TransformStream({
|
|
27
14
|
transform: (chunk, controller) => {
|
|
28
|
-
for (const line of decoder.decode(chunk).split("\n")) {
|
|
29
|
-
if (line
|
|
15
|
+
for (const line of decoder.decode(chunk).split("\n").filter((line2) => line2)) {
|
|
16
|
+
if (line !== "data: [DONE]") {
|
|
30
17
|
const data = JSON.parse(line.slice(6));
|
|
31
|
-
controller.enqueue(data
|
|
18
|
+
controller.enqueue(data);
|
|
32
19
|
if (data.choices[0].finish_reason) {
|
|
33
20
|
finishReason = data.choices[0].finish_reason;
|
|
34
21
|
}
|
|
35
22
|
if (data.usage)
|
|
36
23
|
usage = data.usage;
|
|
37
|
-
} else
|
|
24
|
+
} else {
|
|
38
25
|
controller.terminate();
|
|
39
26
|
}
|
|
40
27
|
}
|
|
41
28
|
}
|
|
42
|
-
});
|
|
43
|
-
const
|
|
44
|
-
|
|
29
|
+
}));
|
|
30
|
+
const [chunkStream, rawTextStream] = rawChunkStream.tee();
|
|
31
|
+
const textStream = rawTextStream.pipeThrough(new TransformStream({
|
|
32
|
+
transform: (chunk, controller) => controller.enqueue(chunk.choices[0].delta.content)
|
|
33
|
+
}));
|
|
34
|
+
return { chunkStream, finishReason, textStream, usage };
|
|
45
35
|
});
|
|
46
36
|
|
|
47
37
|
export { streamText as default, streamText };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-text",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"author": "
|
|
5
|
+
"author": "Moeru AI",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"homepage": "https://
|
|
7
|
+
"homepage": "https://xsai.js.org",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/moeru-ai/xsai.git",
|
|
@@ -23,8 +23,11 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@xsai/
|
|
27
|
-
|
|
26
|
+
"@xsai/shared-chat-completion": ""
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@xsai/providers": "",
|
|
30
|
+
"@xsai/shared": ""
|
|
28
31
|
},
|
|
29
32
|
"scripts": {
|
|
30
33
|
"build": "pkgroll",
|