@xsai/generate-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 +4 -44
- package/dist/index.js +24 -41
- package/package.json +4 -4
package/LICENSE.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
|
|
6
|
-
interface CommonMessage<T extends string> {
|
|
7
|
-
content: string;
|
|
8
|
-
name?: string;
|
|
9
|
-
role: T;
|
|
10
|
-
}
|
|
11
|
-
interface SystemMessage extends CommonMessage<'system'> {
|
|
12
|
-
}
|
|
13
|
-
interface UserMessage extends CommonMessage<'user'> {
|
|
14
|
-
}
|
|
15
|
-
interface AssistantMessage extends CommonMessage<'assistant'> {
|
|
16
|
-
refusal?: string;
|
|
17
|
-
tool_calls?: {
|
|
18
|
-
function: {
|
|
19
|
-
arguments: string;
|
|
20
|
-
name: string;
|
|
21
|
-
};
|
|
22
|
-
id: string;
|
|
23
|
-
type: 'function';
|
|
24
|
-
}[];
|
|
25
|
-
}
|
|
26
|
-
interface ToolMessage extends Omit<CommonMessage<'tool'>, 'name'> {
|
|
27
|
-
tool_call_id: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type TextGenerationModel = 'gemma2' | 'llama3.1' | 'llama3.2' | 'llama3.2-vision' | 'mistral-nemo' | 'mistral-small' | 'nemotron' | 'qwen2.5' | 'qwen2.5-coder' | ({} & string);
|
|
31
|
-
|
|
32
|
-
type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
33
|
-
function: {
|
|
34
|
-
name: string;
|
|
35
|
-
};
|
|
36
|
-
type: 'function';
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
interface GenerateTextOptions extends CommonRequestOptions<'chat/completions'> {
|
|
40
|
-
[key: string]: unknown;
|
|
41
|
-
messages: Message[];
|
|
42
|
-
model: TextGenerationModel;
|
|
43
|
-
toolChoice?: ToolChoice;
|
|
3
|
+
interface GenerateTextOptions extends ChatCompletionOptions {
|
|
44
4
|
}
|
|
45
5
|
interface GenerateTextResponseUsage {
|
|
46
6
|
completion_tokens: number;
|
|
@@ -49,9 +9,9 @@ interface GenerateTextResponseUsage {
|
|
|
49
9
|
}
|
|
50
10
|
interface GenerateTextResult {
|
|
51
11
|
finishReason: FinishReason;
|
|
52
|
-
text
|
|
12
|
+
text?: string;
|
|
53
13
|
usage: GenerateTextResponseUsage;
|
|
54
14
|
}
|
|
55
15
|
declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
|
|
56
16
|
|
|
57
|
-
export { type
|
|
17
|
+
export { type GenerateTextOptions, type GenerateTextResponseUsage, type GenerateTextResult, generateText as default, generateText };
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { chatCompletion } from '@xsai/shared-chat-completion';
|
|
2
2
|
|
|
3
|
-
const generateText = async (options) => await
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
abortSignal: void 0,
|
|
7
|
-
base: void 0,
|
|
8
|
-
headers: void 0,
|
|
9
|
-
path: void 0,
|
|
10
|
-
stream: false,
|
|
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 generateText = async (options) => await chatCompletion({
|
|
4
|
+
...options,
|
|
5
|
+
stream: false
|
|
22
6
|
}).then(async (res) => {
|
|
23
7
|
if (!res.ok) {
|
|
24
8
|
const error = await res.text();
|
|
@@ -28,33 +12,32 @@ const generateText = async (options) => await fetch(requestUrl(options.path ?? "
|
|
|
28
12
|
}
|
|
29
13
|
}).then(async ({ choices, usage }) => {
|
|
30
14
|
const { finish_reason, message } = choices[0];
|
|
31
|
-
if (message.tool_calls) {
|
|
32
|
-
const toolMessages = [];
|
|
33
|
-
for (const toolCall of message.tool_calls) {
|
|
34
|
-
const tool = options.tools.find((tool2) => tool2.function.name === toolCall.function.name);
|
|
35
|
-
const toolResult = await tool.execute(JSON.parse(toolCall.function.arguments));
|
|
36
|
-
const toolMessage = {
|
|
37
|
-
content: toolResult,
|
|
38
|
-
role: "tool",
|
|
39
|
-
tool_call_id: toolCall.id
|
|
40
|
-
};
|
|
41
|
-
toolMessages.push(toolMessage);
|
|
42
|
-
}
|
|
43
|
-
return await generateText({
|
|
44
|
-
...options,
|
|
45
|
-
messages: [
|
|
46
|
-
...options.messages,
|
|
47
|
-
message,
|
|
48
|
-
...toolMessages
|
|
49
|
-
]
|
|
50
|
-
});
|
|
51
|
-
} else {
|
|
15
|
+
if (!!message.content || !message.tool_calls) {
|
|
52
16
|
return {
|
|
53
17
|
finishReason: finish_reason,
|
|
54
18
|
text: message.content,
|
|
55
19
|
usage
|
|
56
20
|
};
|
|
57
21
|
}
|
|
22
|
+
const toolMessages = [];
|
|
23
|
+
for (const toolCall of message.tool_calls) {
|
|
24
|
+
const tool = options.tools.find((tool2) => tool2.function.name === toolCall.function.name);
|
|
25
|
+
const toolResult = await tool.execute(JSON.parse(toolCall.function.arguments));
|
|
26
|
+
const toolMessage = {
|
|
27
|
+
content: toolResult,
|
|
28
|
+
role: "tool",
|
|
29
|
+
tool_call_id: toolCall.id
|
|
30
|
+
};
|
|
31
|
+
toolMessages.push(toolMessage);
|
|
32
|
+
}
|
|
33
|
+
return await generateText({
|
|
34
|
+
...options,
|
|
35
|
+
messages: [
|
|
36
|
+
...options.messages,
|
|
37
|
+
{ ...message, content: message.content },
|
|
38
|
+
...toolMessages
|
|
39
|
+
]
|
|
40
|
+
});
|
|
58
41
|
});
|
|
59
42
|
|
|
60
43
|
export { generateText as default, generateText };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/generate-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,7 +23,7 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@xsai/shared": "
|
|
26
|
+
"@xsai/shared-chat-completion": ""
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "pkgroll",
|