@xsai/generate-text 0.5.0-beta.1 → 0.5.0-beta.3
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 +3 -3
- package/dist/index.js +16 -20
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WithUnknown } from '@xsai/shared';
|
|
2
|
-
import { ChatOptions, CompletionStep, PrepareStep, StopCondition, FinishReason, AssistantMessage,
|
|
2
|
+
import { ChatOptions, CompletionStep, PrepareStep, StopCondition, Message, FinishReason, AssistantMessage, ChatCompletionUsage, CompletionToolCall, CompletionToolResult, Usage } from '@xsai/shared-chat';
|
|
3
3
|
|
|
4
4
|
interface GenerateTextOptions extends ChatOptions {
|
|
5
5
|
onStepFinish?: (step: CompletionStep<true>) => Promise<unknown> | unknown;
|
|
@@ -7,7 +7,7 @@ interface GenerateTextOptions extends ChatOptions {
|
|
|
7
7
|
/** @internal */
|
|
8
8
|
steps?: CompletionStep<true>[];
|
|
9
9
|
/** @default `stepCountAtLeast(1)` */
|
|
10
|
-
stopWhen?: StopCondition
|
|
10
|
+
stopWhen?: StopCondition<Message>;
|
|
11
11
|
/** if you want to enable stream, use `@xsai/stream-{text,object}` */
|
|
12
12
|
stream?: never;
|
|
13
13
|
}
|
|
@@ -23,7 +23,7 @@ interface GenerateTextResponse {
|
|
|
23
23
|
model: string;
|
|
24
24
|
object: 'chat.completion';
|
|
25
25
|
system_fingerprint: string;
|
|
26
|
-
usage:
|
|
26
|
+
usage: ChatCompletionUsage;
|
|
27
27
|
}
|
|
28
28
|
interface GenerateTextResult {
|
|
29
29
|
finishReason: FinishReason;
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { trampoline, responseJSON, InvalidResponseError } from '@xsai/shared';
|
|
2
|
-
import {
|
|
2
|
+
import { resolvePrepareStep, chat, normalizeChatCompletionUsage, stepCountAtLeast, executeTool, shouldStop } from '@xsai/shared-chat';
|
|
3
3
|
|
|
4
4
|
const rawGenerateText = async (options) => {
|
|
5
5
|
const messages = options.steps == null ? structuredClone(options.messages) : options.messages;
|
|
6
6
|
const steps = options.steps ?? [];
|
|
7
|
-
const stepOptions = await
|
|
8
|
-
messages,
|
|
7
|
+
const stepOptions = await resolvePrepareStep({
|
|
8
|
+
input: messages,
|
|
9
9
|
model: options.model,
|
|
10
10
|
prepareStep: options.prepareStep,
|
|
11
11
|
stepNumber: steps.length,
|
|
@@ -15,14 +15,15 @@ const rawGenerateText = async (options) => {
|
|
|
15
15
|
return chat({
|
|
16
16
|
...options,
|
|
17
17
|
maxSteps: void 0,
|
|
18
|
-
messages: stepOptions.
|
|
18
|
+
messages: stepOptions.input,
|
|
19
19
|
model: stepOptions.model,
|
|
20
20
|
steps: void 0,
|
|
21
21
|
stopWhen: void 0,
|
|
22
22
|
stream: false,
|
|
23
23
|
toolChoice: stepOptions.toolChoice
|
|
24
24
|
}).then(responseJSON).then(async (res) => {
|
|
25
|
-
const { choices
|
|
25
|
+
const { choices } = res;
|
|
26
|
+
const usage = normalizeChatCompletionUsage(res.usage);
|
|
26
27
|
if (!choices?.length) {
|
|
27
28
|
const responseBody = JSON.stringify(res);
|
|
28
29
|
throw new InvalidResponseError(`No choices returned, response body: ${responseBody}`, {
|
|
@@ -45,13 +46,17 @@ const rawGenerateText = async (options) => {
|
|
|
45
46
|
tools: options.tools
|
|
46
47
|
}))
|
|
47
48
|
);
|
|
48
|
-
for (const { completionToolCall, completionToolResult,
|
|
49
|
+
for (const { completionToolCall, completionToolResult, result } of results) {
|
|
49
50
|
toolCalls.push(completionToolCall);
|
|
50
51
|
toolResults.push(completionToolResult);
|
|
51
|
-
messages.push(
|
|
52
|
+
messages.push({
|
|
53
|
+
content: result,
|
|
54
|
+
role: "tool",
|
|
55
|
+
tool_call_id: completionToolCall.toolCallId
|
|
56
|
+
});
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
|
-
const
|
|
59
|
+
const step = {
|
|
55
60
|
finishReason,
|
|
56
61
|
text: Array.isArray(message.content) ? message.content.filter((m) => m.type === "text").map((m) => m.text).join("\n") : message.content,
|
|
57
62
|
toolCalls,
|
|
@@ -59,20 +64,11 @@ const rawGenerateText = async (options) => {
|
|
|
59
64
|
usage
|
|
60
65
|
};
|
|
61
66
|
const stop = shouldStop(stopWhen, {
|
|
62
|
-
messages,
|
|
63
|
-
step
|
|
64
|
-
steps: [...steps,
|
|
67
|
+
input: messages,
|
|
68
|
+
step,
|
|
69
|
+
steps: [...steps, step]
|
|
65
70
|
});
|
|
66
71
|
const willContinue = toolCalls.length > 0 && !stop;
|
|
67
|
-
const step = {
|
|
68
|
-
...stopStep,
|
|
69
|
-
stepType: determineStepType({
|
|
70
|
-
finishReason,
|
|
71
|
-
stepsLength: steps.length,
|
|
72
|
-
toolCallsLength: toolCalls.length,
|
|
73
|
-
willContinue
|
|
74
|
-
})
|
|
75
|
-
};
|
|
76
72
|
steps.push(step);
|
|
77
73
|
if (options.onStepFinish)
|
|
78
74
|
await options.onStepFinish(step);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/generate-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-beta.
|
|
4
|
+
"version": "0.5.0-beta.3",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@xsai/shared": "
|
|
37
|
-
"@xsai/shared-chat": "
|
|
36
|
+
"@xsai/shared": "0.5.0-beta.3",
|
|
37
|
+
"@xsai/shared-chat": "0.5.0-beta.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"valibot": "^1.0.0",
|
|
41
|
-
"@xsai/tool": "
|
|
41
|
+
"@xsai/tool": "0.5.0-beta.3"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "pkgroll",
|