@xsai/generate-text 0.0.8 → 0.0.9
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 +1 -16
- package/dist/index.js +35 -32
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -42,19 +42,6 @@ interface GenerateTextOptions extends CommonRequestOptions<'chat/completions'> {
|
|
|
42
42
|
model: TextGenerationModel;
|
|
43
43
|
toolChoice?: ToolChoice;
|
|
44
44
|
}
|
|
45
|
-
interface GenerateTextResponse {
|
|
46
|
-
choices: {
|
|
47
|
-
finish_reason: FinishReason;
|
|
48
|
-
index: number;
|
|
49
|
-
message: AssistantMessage;
|
|
50
|
-
}[];
|
|
51
|
-
created: number;
|
|
52
|
-
id: string;
|
|
53
|
-
model: string;
|
|
54
|
-
object: 'chat.completion';
|
|
55
|
-
system_fingerprint: string;
|
|
56
|
-
usage: GenerateTextResponseUsage;
|
|
57
|
-
}
|
|
58
45
|
interface GenerateTextResponseUsage {
|
|
59
46
|
completion_tokens: number;
|
|
60
47
|
prompt_tokens: number;
|
|
@@ -62,11 +49,9 @@ interface GenerateTextResponseUsage {
|
|
|
62
49
|
}
|
|
63
50
|
interface GenerateTextResult {
|
|
64
51
|
finishReason: FinishReason;
|
|
65
|
-
request: Request;
|
|
66
|
-
response: Response;
|
|
67
52
|
text: string;
|
|
68
53
|
usage: GenerateTextResponseUsage;
|
|
69
54
|
}
|
|
70
55
|
declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
|
|
71
56
|
|
|
72
|
-
export { type
|
|
57
|
+
export { type AssistantMessage, type CommonMessage, type FinishReason, type GenerateTextOptions, type GenerateTextResponseUsage, type GenerateTextResult, type Message, type SystemMessage, type TextGenerationModel, type ToolChoice, type ToolMessage, type UserMessage, generateText as default, generateText };
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
1
|
import { requestUrl, clean, objCamelToSnake } from '@xsai/shared';
|
|
2
2
|
|
|
3
|
-
const generateText = async (options) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
const generateText = async (options) => await fetch(requestUrl(options.path ?? "chat/completions", options.base), {
|
|
4
|
+
body: JSON.stringify(clean({
|
|
5
|
+
...objCamelToSnake(options),
|
|
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
|
|
22
|
+
}).then(async (res) => {
|
|
23
|
+
if (!res.ok) {
|
|
24
|
+
const error = await res.text();
|
|
25
|
+
throw new Error(`Error(${res.status}): ${error}`);
|
|
26
|
+
} else {
|
|
27
|
+
return res.json();
|
|
28
|
+
}
|
|
29
|
+
}).then(async ({ choices, usage }) => {
|
|
30
|
+
const { finish_reason, message } = choices[0];
|
|
27
31
|
if (message.tool_calls) {
|
|
28
32
|
const toolMessages = [];
|
|
29
33
|
for (const toolCall of message.tool_calls) {
|
|
@@ -44,14 +48,13 @@ const generateText = async (options) => {
|
|
|
44
48
|
...toolMessages
|
|
45
49
|
]
|
|
46
50
|
});
|
|
51
|
+
} else {
|
|
52
|
+
return {
|
|
53
|
+
finishReason: finish_reason,
|
|
54
|
+
text: message.content,
|
|
55
|
+
usage
|
|
56
|
+
};
|
|
47
57
|
}
|
|
48
|
-
|
|
49
|
-
finishReason: finish_reason,
|
|
50
|
-
request,
|
|
51
|
-
response,
|
|
52
|
-
text: message.content,
|
|
53
|
-
usage: json.usage
|
|
54
|
-
};
|
|
55
|
-
};
|
|
58
|
+
});
|
|
56
59
|
|
|
57
60
|
export { generateText as default, generateText };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/generate-text",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
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.9"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "pkgroll",
|