@xsai/generate-text 0.0.26 → 0.0.28

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 CHANGED
@@ -1,8 +1,11 @@
1
- import { ChatOptions, FinishReason, AssistantMessageResponse } from '@xsai/shared-chat';
1
+ import { ChatOptions, FinishReason, AssistantMessageResponse, Usage } from '@xsai/shared-chat';
2
2
 
3
3
  interface GenerateTextOptions extends ChatOptions {
4
4
  /** @default 1 */
5
5
  maxSteps?: number;
6
+ onStepFinish?: (step: StepResult) => Promise<void> | void;
7
+ /** @internal */
8
+ steps?: StepResult[];
6
9
  /** if you want to enable stream, use `@xsai/stream-{text,object}` */
7
10
  stream?: never;
8
11
  }
@@ -17,23 +20,34 @@ interface GenerateTextResponse {
17
20
  model: string;
18
21
  object: 'chat.completion';
19
22
  system_fingerprint: string;
20
- usage: GenerateTextResponseUsage;
23
+ usage: Usage;
21
24
  }
22
- interface GenerateTextResponseUsage {
23
- completion_tokens: number;
24
- prompt_tokens: number;
25
- total_tokens: number;
25
+ interface ToolCall {
26
+ args: string;
27
+ toolCallId: string;
28
+ toolCallType: 'function';
29
+ toolName: string;
30
+ }
31
+ interface ToolResult {
32
+ args: Record<string, unknown>;
33
+ result: string;
34
+ toolCallId: string;
35
+ toolName: string;
26
36
  }
27
37
  interface GenerateTextResult {
28
38
  finishReason: FinishReason;
29
39
  steps: StepResult[];
30
40
  text?: string;
31
- usage: GenerateTextResponseUsage;
41
+ toolCalls: ToolCall[];
42
+ toolResults: ToolResult[];
43
+ usage: Usage;
32
44
  }
33
45
  interface StepResult {
34
46
  text?: string;
35
- usage: GenerateTextResponseUsage;
47
+ toolCalls: ToolCall[];
48
+ toolResults: ToolResult[];
49
+ usage: Usage;
36
50
  }
37
51
  declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
38
52
 
39
- export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResponseUsage, type GenerateTextResult, type StepResult, generateText as default, generateText };
53
+ export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type StepResult, type ToolCall, type ToolResult, generateText as default, generateText };
package/dist/index.js CHANGED
@@ -1,60 +1,80 @@
1
1
  import { chat } from '@xsai/shared-chat';
2
2
 
3
- const generateText = async (options) => {
4
- let currentStep = 0;
5
- let finishReason = "error";
6
- let text;
7
- let usage = {
8
- completion_tokens: 0,
9
- prompt_tokens: 0,
10
- total_tokens: 0
11
- };
12
- const steps = [];
3
+ const rawGenerateText = async (options) => await chat({
4
+ ...options,
5
+ maxSteps: void 0,
6
+ messages: options.messages,
7
+ steps: void 0,
8
+ stream: false
9
+ }).then((res) => res.json()).then(async ({ choices, usage }) => {
13
10
  const messages = options.messages;
14
- while (currentStep < (options.maxSteps ?? 1)) {
15
- currentStep += 1;
16
- const data = await chat({
17
- ...options,
18
- maxSteps: void 0,
19
- messages,
20
- stream: false
21
- }).then((res) => res.json());
22
- const { finish_reason, message } = data.choices[0];
23
- finishReason = finish_reason;
24
- text = message.content;
25
- usage = data.usage;
26
- steps.push({
11
+ const steps = options.steps ?? [];
12
+ const toolCalls = [];
13
+ const toolResults = [];
14
+ const { finish_reason: finishReason, message } = choices[0];
15
+ if (message.content || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
16
+ const step2 = {
27
17
  text: message.content,
28
- // type: 'initial',
18
+ toolCalls,
19
+ toolResults,
29
20
  usage
21
+ };
22
+ steps.push(step2);
23
+ if (options.onStepFinish)
24
+ await options.onStepFinish(step2);
25
+ return {
26
+ finishReason,
27
+ steps,
28
+ ...step2
29
+ };
30
+ }
31
+ messages.push({ ...message, content: message.content });
32
+ for (const {
33
+ function: { arguments: toolArgs, name: toolName },
34
+ id: toolCallId,
35
+ type: toolCallType
36
+ } of message.tool_calls) {
37
+ const tool = options.tools.find((tool2) => tool2.function.name === toolName);
38
+ const parsedArgs = JSON.parse(toolArgs);
39
+ const result = await tool.execute(parsedArgs);
40
+ toolCalls.push({
41
+ args: toolArgs,
42
+ toolCallId,
43
+ toolCallType,
44
+ toolName
45
+ });
46
+ toolResults.push({
47
+ args: parsedArgs,
48
+ result,
49
+ toolCallId,
50
+ toolName
51
+ });
52
+ messages.push({
53
+ content: result,
54
+ role: "tool",
55
+ tool_call_id: toolCallId
30
56
  });
31
- messages.push({ ...message, content: message.content });
32
- if (message.tool_calls) {
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
- messages.push(toolMessage);
42
- }
43
- } else {
44
- return {
45
- finishReason: finish_reason,
46
- steps,
47
- text: message.content,
48
- usage
49
- };
50
- }
51
57
  }
52
- return {
53
- finishReason,
54
- steps,
55
- text,
58
+ const step = {
59
+ text: message.content,
60
+ toolCalls,
61
+ toolResults,
56
62
  usage
57
63
  };
64
+ steps.push(step);
65
+ if (options.onStepFinish)
66
+ await options.onStepFinish(step);
67
+ return async () => await rawGenerateText({
68
+ ...options,
69
+ messages,
70
+ steps
71
+ });
72
+ });
73
+ const generateText = async (options) => {
74
+ let result = await rawGenerateText(options);
75
+ while (result instanceof Function)
76
+ result = await result();
77
+ return result;
58
78
  };
59
79
 
60
80
  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.26",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",