@xsai/stream-text 0.0.9 → 0.0.11
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 +36 -3
- package/dist/index.js +21 -32
- package/package.json +4 -5
package/LICENSE.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
|
|
2
2
|
|
|
3
|
+
interface StreamTextOptions extends ChatCompletionOptions {
|
|
4
|
+
streamOptions?: {
|
|
5
|
+
/**
|
|
6
|
+
* Return usage.
|
|
7
|
+
* @default `undefined`
|
|
8
|
+
* @remarks Ollama doesn't support this, see {@link https://github.com/ollama/ollama/issues/5200}
|
|
9
|
+
*/
|
|
10
|
+
usage?: boolean;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
interface StreamTextResponseUsage {
|
|
14
|
+
completion_tokens: number;
|
|
15
|
+
prompt_tokens: number;
|
|
16
|
+
total_tokens: number;
|
|
17
|
+
}
|
|
3
18
|
interface StreamTextResult {
|
|
19
|
+
finishReason?: FinishReason;
|
|
4
20
|
textStream: ReadableStream<string>;
|
|
21
|
+
usage?: StreamTextResponseUsage;
|
|
22
|
+
}
|
|
23
|
+
interface StreamTextResponse {
|
|
24
|
+
choices: {
|
|
25
|
+
delta: {
|
|
26
|
+
content: string;
|
|
27
|
+
role: 'assistant';
|
|
28
|
+
};
|
|
29
|
+
finish_reason?: FinishReason;
|
|
30
|
+
index: number;
|
|
31
|
+
}[];
|
|
32
|
+
created: number;
|
|
33
|
+
id: string;
|
|
34
|
+
model: string;
|
|
35
|
+
object: 'chat.completion.chunk';
|
|
36
|
+
system_fingerprint: string;
|
|
37
|
+
usage?: StreamTextResponseUsage;
|
|
5
38
|
}
|
|
6
39
|
/**
|
|
7
40
|
* @experimental
|
|
8
41
|
* WIP, currently only returns `textStream`, does not support function calling (tools).
|
|
9
42
|
*/
|
|
10
|
-
declare const streamText: (options:
|
|
43
|
+
declare const streamText: (options: StreamTextOptions) => Promise<StreamTextResult>;
|
|
11
44
|
|
|
12
|
-
export { type StreamTextResult, streamText as default, streamText };
|
|
45
|
+
export { type StreamTextOptions, type StreamTextResponse, type StreamTextResponseUsage, type StreamTextResult, streamText as default, streamText };
|
package/dist/index.js
CHANGED
|
@@ -1,44 +1,33 @@
|
|
|
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
|
+
}
|
|
23
10
|
const decoder = new TextDecoder();
|
|
24
|
-
|
|
11
|
+
let finishReason;
|
|
12
|
+
let usage;
|
|
13
|
+
const textStream = res.body.pipeThrough(new TransformStream({
|
|
25
14
|
transform: (chunk, controller) => {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
if (line.includes("[DONE]")) {
|
|
29
|
-
controller.terminate();
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
15
|
+
for (const line of decoder.decode(chunk).split("\n").filter((line2) => line2)) {
|
|
16
|
+
if (line !== "data: [DONE]") {
|
|
33
17
|
const data = JSON.parse(line.slice(6));
|
|
34
18
|
controller.enqueue(data.choices[0].delta.content);
|
|
35
|
-
|
|
19
|
+
if (data.choices[0].finish_reason) {
|
|
20
|
+
finishReason = data.choices[0].finish_reason;
|
|
21
|
+
}
|
|
22
|
+
if (data.usage)
|
|
23
|
+
usage = data.usage;
|
|
24
|
+
} else {
|
|
25
|
+
controller.terminate();
|
|
36
26
|
}
|
|
37
27
|
}
|
|
38
28
|
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
return { textStream };
|
|
29
|
+
}));
|
|
30
|
+
return { finishReason, textStream, usage };
|
|
42
31
|
});
|
|
43
32
|
|
|
44
33
|
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.11",
|
|
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,7 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@xsai/
|
|
27
|
-
"@xsai/shared": "^0.0.9"
|
|
26
|
+
"@xsai/shared-chat-completion": ""
|
|
28
27
|
},
|
|
29
28
|
"scripts": {
|
|
30
29
|
"build": "pkgroll",
|