@xsai/generate-text 0.0.6 → 0.0.7
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 +18 -1
- package/dist/index.js +34 -6
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -4,15 +4,32 @@ type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | '
|
|
|
4
4
|
|
|
5
5
|
interface Message {
|
|
6
6
|
content: string;
|
|
7
|
-
role: 'assistant' | 'system' | 'user' | ({} & string);
|
|
7
|
+
role: 'assistant' | 'system' | 'tool' | 'user' | ({} & string);
|
|
8
|
+
tool_call_id?: string;
|
|
9
|
+
tool_calls?: {
|
|
10
|
+
function: {
|
|
11
|
+
arguments: string;
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
14
|
+
id: string;
|
|
15
|
+
type: 'function';
|
|
16
|
+
}[];
|
|
8
17
|
}
|
|
9
18
|
|
|
10
19
|
type TextGenerationModel = 'gemma2' | 'llama3.1' | 'llama3.2' | 'llama3.2-vision' | 'mistral-nemo' | 'mistral-small' | 'nemotron' | 'qwen2.5' | 'qwen2.5-coder' | ({} & string);
|
|
11
20
|
|
|
21
|
+
type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
22
|
+
function: {
|
|
23
|
+
name: string;
|
|
24
|
+
};
|
|
25
|
+
type: 'function';
|
|
26
|
+
};
|
|
27
|
+
|
|
12
28
|
interface GenerateTextOptions extends CommonRequestOptions<'chat/completions'> {
|
|
13
29
|
[key: string]: unknown;
|
|
14
30
|
messages: Message[];
|
|
15
31
|
model: TextGenerationModel;
|
|
32
|
+
toolChoice?: ToolChoice;
|
|
16
33
|
}
|
|
17
34
|
interface GenerateTextResponse {
|
|
18
35
|
choices: {
|
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.7",
|
|
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.7"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "pkgroll",
|