@xsai/generate-text 0.0.27 → 0.0.29
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/dist/index.d.ts +17 -21
- package/dist/index.js +12 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ChatOptions, FinishReason, AssistantMessageResponse } from '@xsai/shared-chat';
|
|
1
|
+
import { ChatOptions, FinishReason, AssistantMessageResponse, Usage } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
3
|
interface GenerateTextOptions extends ChatOptions {
|
|
4
4
|
/** @default 1 */
|
|
5
5
|
maxSteps?: number;
|
|
6
|
+
onStepFinish?: (step: StepResult) => Promise<void> | void;
|
|
6
7
|
/** @internal */
|
|
7
8
|
steps?: StepResult[];
|
|
8
9
|
/** if you want to enable stream, use `@xsai/stream-{text,object}` */
|
|
@@ -19,12 +20,21 @@ interface GenerateTextResponse {
|
|
|
19
20
|
model: string;
|
|
20
21
|
object: 'chat.completion';
|
|
21
22
|
system_fingerprint: string;
|
|
22
|
-
usage:
|
|
23
|
+
usage: Usage;
|
|
23
24
|
}
|
|
24
|
-
interface
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
interface GenerateTextResult {
|
|
26
|
+
finishReason: FinishReason;
|
|
27
|
+
steps: StepResult[];
|
|
28
|
+
text?: string;
|
|
29
|
+
toolCalls: ToolCall[];
|
|
30
|
+
toolResults: ToolResult[];
|
|
31
|
+
usage: Usage;
|
|
32
|
+
}
|
|
33
|
+
interface StepResult {
|
|
34
|
+
text?: string;
|
|
35
|
+
toolCalls: ToolCall[];
|
|
36
|
+
toolResults: ToolResult[];
|
|
37
|
+
usage: Usage;
|
|
28
38
|
}
|
|
29
39
|
interface ToolCall {
|
|
30
40
|
args: string;
|
|
@@ -38,20 +48,6 @@ interface ToolResult {
|
|
|
38
48
|
toolCallId: string;
|
|
39
49
|
toolName: string;
|
|
40
50
|
}
|
|
41
|
-
interface GenerateTextResult {
|
|
42
|
-
finishReason: FinishReason;
|
|
43
|
-
steps: StepResult[];
|
|
44
|
-
text?: string;
|
|
45
|
-
toolCalls: ToolCall[];
|
|
46
|
-
toolResults: ToolResult[];
|
|
47
|
-
usage: GenerateTextResponseUsage;
|
|
48
|
-
}
|
|
49
|
-
interface StepResult {
|
|
50
|
-
text?: string;
|
|
51
|
-
toolCalls: ToolCall[];
|
|
52
|
-
toolResults: ToolResult[];
|
|
53
|
-
usage: GenerateTextResponseUsage;
|
|
54
|
-
}
|
|
55
51
|
declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
|
|
56
52
|
|
|
57
|
-
export { type GenerateTextOptions, type GenerateTextResponse, type
|
|
53
|
+
export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type StepResult, type ToolCall, type ToolResult, generateText as default, generateText };
|
package/dist/index.js
CHANGED
|
@@ -2,9 +2,9 @@ import { chat } from '@xsai/shared-chat';
|
|
|
2
2
|
|
|
3
3
|
const rawGenerateText = async (options) => await chat({
|
|
4
4
|
...options,
|
|
5
|
-
maxSteps:
|
|
5
|
+
maxSteps: undefined,
|
|
6
6
|
messages: options.messages,
|
|
7
|
-
steps:
|
|
7
|
+
steps: undefined,
|
|
8
8
|
stream: false
|
|
9
9
|
}).then((res) => res.json()).then(async ({ choices, usage }) => {
|
|
10
10
|
const messages = options.messages;
|
|
@@ -13,17 +13,19 @@ const rawGenerateText = async (options) => await chat({
|
|
|
13
13
|
const toolResults = [];
|
|
14
14
|
const { finish_reason: finishReason, message } = choices[0];
|
|
15
15
|
if (message.content || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
|
|
16
|
-
const
|
|
16
|
+
const step2 = {
|
|
17
17
|
text: message.content,
|
|
18
18
|
toolCalls,
|
|
19
19
|
toolResults,
|
|
20
20
|
usage
|
|
21
21
|
};
|
|
22
|
-
steps.push(
|
|
22
|
+
steps.push(step2);
|
|
23
|
+
if (options.onStepFinish)
|
|
24
|
+
await options.onStepFinish(step2);
|
|
23
25
|
return {
|
|
24
26
|
finishReason,
|
|
25
27
|
steps,
|
|
26
|
-
...
|
|
28
|
+
...step2
|
|
27
29
|
};
|
|
28
30
|
}
|
|
29
31
|
messages.push({ ...message, content: message.content });
|
|
@@ -53,12 +55,15 @@ const rawGenerateText = async (options) => await chat({
|
|
|
53
55
|
tool_call_id: toolCallId
|
|
54
56
|
});
|
|
55
57
|
}
|
|
56
|
-
|
|
58
|
+
const step = {
|
|
57
59
|
text: message.content,
|
|
58
60
|
toolCalls,
|
|
59
61
|
toolResults,
|
|
60
62
|
usage
|
|
61
|
-
}
|
|
63
|
+
};
|
|
64
|
+
steps.push(step);
|
|
65
|
+
if (options.onStepFinish)
|
|
66
|
+
await options.onStepFinish(step);
|
|
62
67
|
return async () => await rawGenerateText({
|
|
63
68
|
...options,
|
|
64
69
|
messages,
|