@xsai/generate-text 0.0.6 → 0.0.8
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 +31 -3
- package/dist/index.js +34 -6
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -2,23 +2,51 @@ import { CommonRequestOptions } from '@xsai/shared';
|
|
|
2
2
|
|
|
3
3
|
type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | ({} & string);
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type Message = AssistantMessage | SystemMessage | ToolMessage | UserMessage;
|
|
6
|
+
interface CommonMessage<T extends string> {
|
|
6
7
|
content: string;
|
|
7
|
-
|
|
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;
|
|
8
28
|
}
|
|
9
29
|
|
|
10
30
|
type TextGenerationModel = 'gemma2' | 'llama3.1' | 'llama3.2' | 'llama3.2-vision' | 'mistral-nemo' | 'mistral-small' | 'nemotron' | 'qwen2.5' | 'qwen2.5-coder' | ({} & string);
|
|
11
31
|
|
|
32
|
+
type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
33
|
+
function: {
|
|
34
|
+
name: string;
|
|
35
|
+
};
|
|
36
|
+
type: 'function';
|
|
37
|
+
};
|
|
38
|
+
|
|
12
39
|
interface GenerateTextOptions extends CommonRequestOptions<'chat/completions'> {
|
|
13
40
|
[key: string]: unknown;
|
|
14
41
|
messages: Message[];
|
|
15
42
|
model: TextGenerationModel;
|
|
43
|
+
toolChoice?: ToolChoice;
|
|
16
44
|
}
|
|
17
45
|
interface GenerateTextResponse {
|
|
18
46
|
choices: {
|
|
19
47
|
finish_reason: FinishReason;
|
|
20
48
|
index: number;
|
|
21
|
-
message:
|
|
49
|
+
message: AssistantMessage;
|
|
22
50
|
}[];
|
|
23
51
|
created: number;
|
|
24
52
|
id: string;
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,55 @@
|
|
|
1
|
-
import { requestUrl, clean } from '@xsai/shared';
|
|
1
|
+
import { requestUrl, clean, objCamelToSnake } from '@xsai/shared';
|
|
2
2
|
|
|
3
3
|
const generateText = async (options) => {
|
|
4
4
|
const request = new Request(requestUrl(options.path ?? "chat/completions", options.base), {
|
|
5
5
|
body: JSON.stringify(clean({
|
|
6
|
-
...options,
|
|
6
|
+
...objCamelToSnake(options),
|
|
7
|
+
abortSignal: void 0,
|
|
7
8
|
base: void 0,
|
|
8
9
|
headers: void 0,
|
|
9
10
|
path: void 0,
|
|
10
|
-
stream: false
|
|
11
|
+
stream: false,
|
|
12
|
+
tools: options.tools?.map((tool) => ({
|
|
13
|
+
function: tool.function,
|
|
14
|
+
type: "function"
|
|
15
|
+
}))
|
|
11
16
|
})),
|
|
12
17
|
headers: {
|
|
13
18
|
"Content-Type": "application/json",
|
|
14
19
|
...options.headers
|
|
15
20
|
},
|
|
16
|
-
method: "POST"
|
|
21
|
+
method: "POST",
|
|
22
|
+
signal: options.abortSingal
|
|
17
23
|
});
|
|
18
24
|
const response = await fetch(request);
|
|
19
25
|
const json = await response.json();
|
|
26
|
+
const { finish_reason, message } = json.choices[0];
|
|
27
|
+
if (message.tool_calls) {
|
|
28
|
+
const toolMessages = [];
|
|
29
|
+
for (const toolCall of message.tool_calls) {
|
|
30
|
+
const tool = options.tools.find((tool2) => tool2.function.name === toolCall.function.name);
|
|
31
|
+
const toolResult = await tool.execute(JSON.parse(toolCall.function.arguments));
|
|
32
|
+
const toolMessage = {
|
|
33
|
+
content: toolResult,
|
|
34
|
+
role: "tool",
|
|
35
|
+
tool_call_id: toolCall.id
|
|
36
|
+
};
|
|
37
|
+
toolMessages.push(toolMessage);
|
|
38
|
+
}
|
|
39
|
+
return await generateText({
|
|
40
|
+
...options,
|
|
41
|
+
messages: [
|
|
42
|
+
...options.messages,
|
|
43
|
+
message,
|
|
44
|
+
...toolMessages
|
|
45
|
+
]
|
|
46
|
+
});
|
|
47
|
+
}
|
|
20
48
|
return {
|
|
21
|
-
finishReason:
|
|
49
|
+
finishReason: finish_reason,
|
|
22
50
|
request,
|
|
23
51
|
response,
|
|
24
|
-
text:
|
|
52
|
+
text: message.content,
|
|
25
53
|
usage: json.usage
|
|
26
54
|
};
|
|
27
55
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/generate-text",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "藍+85CD",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@xsai/shared": "^0.0.
|
|
26
|
+
"@xsai/shared": "^0.0.8"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "pkgroll",
|