@xsai/generate-text 0.1.0-beta.5 → 0.1.0-beta.6
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 +6 -18
- package/dist/index.js +16 -10
- package/package.json +9 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message } from '@xsai/shared-chat';
|
|
1
|
+
import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
3
|
interface GenerateTextOptions extends ChatOptions {
|
|
4
4
|
/** @default 1 */
|
|
@@ -28,30 +28,18 @@ interface GenerateTextResult {
|
|
|
28
28
|
messages: Message[];
|
|
29
29
|
steps: GenerateTextStepResult[];
|
|
30
30
|
text?: string;
|
|
31
|
-
toolCalls:
|
|
32
|
-
toolResults:
|
|
31
|
+
toolCalls: CompletionToolCall[];
|
|
32
|
+
toolResults: CompletionToolResult[];
|
|
33
33
|
usage: Usage;
|
|
34
34
|
}
|
|
35
35
|
interface GenerateTextStepResult {
|
|
36
36
|
finishReason: FinishReason;
|
|
37
37
|
stepType: 'continue' | 'initial' | 'tool-result';
|
|
38
38
|
text?: string;
|
|
39
|
-
toolCalls:
|
|
40
|
-
toolResults:
|
|
39
|
+
toolCalls: CompletionToolCall[];
|
|
40
|
+
toolResults: CompletionToolResult[];
|
|
41
41
|
usage: Usage;
|
|
42
42
|
}
|
|
43
|
-
interface GenerateTextToolCall {
|
|
44
|
-
args: string;
|
|
45
|
-
toolCallId: string;
|
|
46
|
-
toolCallType: 'function';
|
|
47
|
-
toolName: string;
|
|
48
|
-
}
|
|
49
|
-
interface GenerateTextToolResult {
|
|
50
|
-
args: Record<string, unknown>;
|
|
51
|
-
result: string;
|
|
52
|
-
toolCallId: string;
|
|
53
|
-
toolName: string;
|
|
54
|
-
}
|
|
55
43
|
declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
|
|
56
44
|
|
|
57
|
-
export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type GenerateTextStepResult,
|
|
45
|
+
export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type GenerateTextStepResult, generateText };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { chat } from "@xsai/shared-chat";
|
|
3
|
+
var rawGenerateText = async (options) => chat({
|
|
4
4
|
...options,
|
|
5
|
-
maxSteps:
|
|
5
|
+
maxSteps: void 0,
|
|
6
6
|
messages: options.messages,
|
|
7
|
-
steps:
|
|
7
|
+
steps: void 0,
|
|
8
8
|
stream: false
|
|
9
9
|
}).then(async (res) => res.json()).then(async ({ choices, usage }) => {
|
|
10
10
|
const messages = structuredClone(options.messages);
|
|
@@ -14,7 +14,7 @@ const rawGenerateText = async (options) => chat({
|
|
|
14
14
|
const { finish_reason: finishReason, message } = choices[0];
|
|
15
15
|
messages.push({ ...message, content: message.content });
|
|
16
16
|
const stepType = steps.length === 0 ? "initial" : steps.at(-1)?.finishReason === "tool-calls" ? "tool-result" : "continue";
|
|
17
|
-
if (message.content !==
|
|
17
|
+
if (message.content !== void 0 && message.content.length > 0 || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
|
|
18
18
|
const step2 = {
|
|
19
19
|
finishReason,
|
|
20
20
|
stepType,
|
|
@@ -41,7 +41,12 @@ const rawGenerateText = async (options) => chat({
|
|
|
41
41
|
id: toolCallId,
|
|
42
42
|
type: toolCallType
|
|
43
43
|
} of message.tool_calls) {
|
|
44
|
-
const tool = options.tools
|
|
44
|
+
const tool = options.tools?.find((tool2) => tool2.function.name === toolName);
|
|
45
|
+
if (!tool) {
|
|
46
|
+
const availableTools = options.tools?.map((tool2) => tool2.function.name);
|
|
47
|
+
const availableToolsErrorMsg = availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`;
|
|
48
|
+
throw new Error(`Model tried to call unavailable tool '${toolName}. ${availableToolsErrorMsg}.`);
|
|
49
|
+
}
|
|
45
50
|
const parsedArgs = JSON.parse(toolArgs);
|
|
46
51
|
const result = await tool.execute(parsedArgs, { abortSignal: options.abortSignal, messages, toolCallId });
|
|
47
52
|
toolCalls.push({
|
|
@@ -79,11 +84,12 @@ const rawGenerateText = async (options) => chat({
|
|
|
79
84
|
steps
|
|
80
85
|
});
|
|
81
86
|
});
|
|
82
|
-
|
|
87
|
+
var generateText = async (options) => {
|
|
83
88
|
let result = await rawGenerateText(options);
|
|
84
89
|
while (typeof result === "function")
|
|
85
90
|
result = await result();
|
|
86
91
|
return result;
|
|
87
92
|
};
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
export {
|
|
94
|
+
generateText
|
|
95
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/generate-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.0-beta.
|
|
4
|
+
"version": "0.1.0-beta.6",
|
|
5
5
|
"description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,10 +22,9 @@
|
|
|
22
22
|
".": {
|
|
23
23
|
"types": "./dist/index.d.ts",
|
|
24
24
|
"default": "./dist/index.js"
|
|
25
|
-
}
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
26
27
|
},
|
|
27
|
-
"main": "./dist/index.js",
|
|
28
|
-
"types": "./dist/index.d.ts",
|
|
29
28
|
"files": [
|
|
30
29
|
"dist"
|
|
31
30
|
],
|
|
@@ -33,12 +32,14 @@
|
|
|
33
32
|
"@xsai/shared-chat": ""
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
|
-
"@xsai/
|
|
35
|
+
"@xsai/tool": "",
|
|
36
|
+
"valibot": "^1.0.0-rc.1"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
|
-
"build": "
|
|
40
|
-
"build:watch": "pkgroll --watch",
|
|
39
|
+
"build": "tsup",
|
|
41
40
|
"test": "vitest run",
|
|
42
41
|
"test:watch": "vitest"
|
|
43
|
-
}
|
|
42
|
+
},
|
|
43
|
+
"main": "./dist/index.js",
|
|
44
|
+
"types": "./dist/index.d.ts"
|
|
44
45
|
}
|