modelfusion 0.68.1 → 0.69.0
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/core/schema/JSONParseError.cjs +1 -1
- package/core/schema/JSONParseError.d.ts +1 -1
- package/core/schema/JSONParseError.js +1 -1
- package/model-function/ModelCallEvent.d.ts +3 -2
- package/model-function/generate-structure/StructureValidationError.cjs +11 -0
- package/model-function/generate-structure/StructureValidationError.d.ts +9 -0
- package/model-function/generate-structure/StructureValidationError.js +11 -0
- package/model-function/generate-tool-call/ToolCallDefinition.cjs +2 -0
- package/model-function/generate-tool-call/ToolCallDefinition.d.ts +7 -0
- package/model-function/generate-tool-call/ToolCallDefinition.js +1 -0
- package/model-function/generate-tool-call/ToolCallGenerationError.cjs +35 -0
- package/model-function/generate-tool-call/ToolCallGenerationError.d.ts +15 -0
- package/model-function/generate-tool-call/ToolCallGenerationError.js +31 -0
- package/model-function/generate-tool-call/ToolCallGenerationEvent.cjs +2 -0
- package/model-function/generate-tool-call/ToolCallGenerationEvent.d.ts +23 -0
- package/model-function/generate-tool-call/ToolCallGenerationEvent.js +1 -0
- package/model-function/generate-tool-call/ToolCallGenerationModel.cjs +2 -0
- package/model-function/generate-tool-call/ToolCallGenerationModel.d.ts +19 -0
- package/model-function/generate-tool-call/ToolCallGenerationModel.js +1 -0
- package/model-function/generate-tool-call/ToolCallParametersValidationError.cjs +44 -0
- package/model-function/generate-tool-call/ToolCallParametersValidationError.d.ts +18 -0
- package/model-function/generate-tool-call/ToolCallParametersValidationError.js +40 -0
- package/model-function/generate-tool-call/generateToolCall.cjs +58 -0
- package/model-function/generate-tool-call/generateToolCall.d.ts +20 -0
- package/model-function/generate-tool-call/generateToolCall.js +54 -0
- package/model-function/generate-tool-call/index.cjs +21 -0
- package/model-function/generate-tool-call/index.d.ts +5 -0
- package/model-function/generate-tool-call/index.js +5 -0
- package/model-function/index.cjs +1 -0
- package/model-function/index.d.ts +1 -0
- package/model-function/index.js +1 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +48 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +181 -9
- package/model-provider/openai/chat/OpenAIChatModel.js +48 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +10 -0
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +10 -0
- package/package.json +3 -3
@@ -6,6 +6,7 @@ import { SpeechGenerationFinishedEvent, SpeechGenerationStartedEvent, SpeechStre
|
|
6
6
|
import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
|
7
7
|
import { StructureStreamingFinishedEvent, StructureStreamingStartedEvent } from "./generate-structure/StructureStreamingEvent.js";
|
8
8
|
import { TextGenerationFinishedEvent, TextGenerationStartedEvent, TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextGenerationEvent.js";
|
9
|
+
import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "./generate-tool-call/ToolCallGenerationEvent.js";
|
9
10
|
import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./generate-transcription/TranscriptionEvent.js";
|
10
11
|
export interface BaseModelCallStartedEvent extends BaseFunctionStartedEvent {
|
11
12
|
model: ModelInformation;
|
@@ -47,5 +48,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
|
|
47
48
|
*/
|
48
49
|
result: BaseModelCallFinishedEventResult;
|
49
50
|
}
|
50
|
-
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
|
51
|
-
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
|
51
|
+
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | TranscriptionStartedEvent;
|
52
|
+
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | TranscriptionFinishedEvent;
|
@@ -37,5 +37,16 @@ class StructureValidationError extends Error {
|
|
37
37
|
this.value = value;
|
38
38
|
this.valueText = valueText;
|
39
39
|
}
|
40
|
+
toJSON() {
|
41
|
+
return {
|
42
|
+
name: this.name,
|
43
|
+
message: this.message,
|
44
|
+
cause: this.cause,
|
45
|
+
stack: this.stack,
|
46
|
+
structureName: this.structureName,
|
47
|
+
value: this.value,
|
48
|
+
valueText: this.valueText,
|
49
|
+
};
|
50
|
+
}
|
40
51
|
}
|
41
52
|
exports.StructureValidationError = StructureValidationError;
|
@@ -9,4 +9,13 @@ export declare class StructureValidationError extends Error {
|
|
9
9
|
valueText: string;
|
10
10
|
cause: unknown;
|
11
11
|
});
|
12
|
+
toJSON(): {
|
13
|
+
name: string;
|
14
|
+
message: string;
|
15
|
+
cause: unknown;
|
16
|
+
stack: string | undefined;
|
17
|
+
structureName: string;
|
18
|
+
value: unknown;
|
19
|
+
valueText: string;
|
20
|
+
};
|
12
21
|
}
|
@@ -34,4 +34,15 @@ export class StructureValidationError extends Error {
|
|
34
34
|
this.value = value;
|
35
35
|
this.valueText = valueText;
|
36
36
|
}
|
37
|
+
toJSON() {
|
38
|
+
return {
|
39
|
+
name: this.name,
|
40
|
+
message: this.message,
|
41
|
+
cause: this.cause,
|
42
|
+
stack: this.stack,
|
43
|
+
structureName: this.structureName,
|
44
|
+
value: this.value,
|
45
|
+
valueText: this.valueText,
|
46
|
+
};
|
47
|
+
}
|
37
48
|
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
|
+
import { Schema } from "../../core/schema/Schema.js";
|
3
|
+
export interface ToolCallDefinition<NAME extends string, PARAMETERS> {
|
4
|
+
name: NAME;
|
5
|
+
description?: string;
|
6
|
+
parameters: Schema<PARAMETERS> & JsonSchemaProducer;
|
7
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ToolCallsGenerationError = void 0;
|
4
|
+
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
|
+
class ToolCallsGenerationError extends Error {
|
6
|
+
constructor({ toolName, cause }) {
|
7
|
+
super(`Tool call generation failed for tool '${toolName}'. ` +
|
8
|
+
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
9
|
+
Object.defineProperty(this, "toolName", {
|
10
|
+
enumerable: true,
|
11
|
+
configurable: true,
|
12
|
+
writable: true,
|
13
|
+
value: void 0
|
14
|
+
});
|
15
|
+
Object.defineProperty(this, "cause", {
|
16
|
+
enumerable: true,
|
17
|
+
configurable: true,
|
18
|
+
writable: true,
|
19
|
+
value: void 0
|
20
|
+
});
|
21
|
+
this.name = "ToolCallsGenerationError";
|
22
|
+
this.toolName = toolName;
|
23
|
+
this.cause = cause;
|
24
|
+
}
|
25
|
+
toJSON() {
|
26
|
+
return {
|
27
|
+
name: this.name,
|
28
|
+
message: this.message,
|
29
|
+
cause: this.cause,
|
30
|
+
stack: this.stack,
|
31
|
+
toolName: this.toolName,
|
32
|
+
};
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.ToolCallsGenerationError = ToolCallsGenerationError;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare class ToolCallsGenerationError extends Error {
|
2
|
+
readonly toolName: string;
|
3
|
+
readonly cause: unknown;
|
4
|
+
constructor({ toolName, cause }: {
|
5
|
+
toolName: string;
|
6
|
+
cause: unknown;
|
7
|
+
});
|
8
|
+
toJSON(): {
|
9
|
+
name: string;
|
10
|
+
message: string;
|
11
|
+
cause: unknown;
|
12
|
+
stack: string | undefined;
|
13
|
+
toolName: string;
|
14
|
+
};
|
15
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
|
+
export class ToolCallsGenerationError extends Error {
|
3
|
+
constructor({ toolName, cause }) {
|
4
|
+
super(`Tool call generation failed for tool '${toolName}'. ` +
|
5
|
+
`Error message: ${getErrorMessage(cause)}`);
|
6
|
+
Object.defineProperty(this, "toolName", {
|
7
|
+
enumerable: true,
|
8
|
+
configurable: true,
|
9
|
+
writable: true,
|
10
|
+
value: void 0
|
11
|
+
});
|
12
|
+
Object.defineProperty(this, "cause", {
|
13
|
+
enumerable: true,
|
14
|
+
configurable: true,
|
15
|
+
writable: true,
|
16
|
+
value: void 0
|
17
|
+
});
|
18
|
+
this.name = "ToolCallsGenerationError";
|
19
|
+
this.toolName = toolName;
|
20
|
+
this.cause = cause;
|
21
|
+
}
|
22
|
+
toJSON() {
|
23
|
+
return {
|
24
|
+
name: this.name,
|
25
|
+
message: this.message,
|
26
|
+
cause: this.cause,
|
27
|
+
stack: this.stack,
|
28
|
+
toolName: this.toolName,
|
29
|
+
};
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
+
export interface ToolCallGenerationStartedEvent extends BaseModelCallStartedEvent {
|
3
|
+
functionType: "generate-tool-call";
|
4
|
+
}
|
5
|
+
export type ToolCallGenerationFinishedEventResult = {
|
6
|
+
status: "success";
|
7
|
+
response: unknown;
|
8
|
+
value: unknown;
|
9
|
+
usage?: {
|
10
|
+
promptTokens: number;
|
11
|
+
completionTokens: number;
|
12
|
+
totalTokens: number;
|
13
|
+
};
|
14
|
+
} | {
|
15
|
+
status: "error";
|
16
|
+
error: unknown;
|
17
|
+
} | {
|
18
|
+
status: "abort";
|
19
|
+
};
|
20
|
+
export type ToolCallGenerationFinishedEvent = BaseModelCallFinishedEvent & {
|
21
|
+
functionType: "generate-tool-call";
|
22
|
+
result: ToolCallGenerationFinishedEventResult;
|
23
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { Model, ModelSettings } from "../Model.js";
|
3
|
+
import { ToolCallDefinition } from "./ToolCallDefinition.js";
|
4
|
+
export interface ToolCallGenerationModelSettings extends ModelSettings {
|
5
|
+
}
|
6
|
+
export interface ToolCallGenerationModel<PROMPT, SETTINGS extends ToolCallGenerationModelSettings = ToolCallGenerationModelSettings> extends Model<SETTINGS> {
|
7
|
+
doGenerateToolCall(tool: ToolCallDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
8
|
+
response: unknown;
|
9
|
+
value: {
|
10
|
+
id: string;
|
11
|
+
parameters: unknown;
|
12
|
+
} | null;
|
13
|
+
usage?: {
|
14
|
+
promptTokens: number;
|
15
|
+
completionTokens: number;
|
16
|
+
totalTokens: number;
|
17
|
+
};
|
18
|
+
}>;
|
19
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ToolCallParametersValidationError = void 0;
|
4
|
+
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
|
+
class ToolCallParametersValidationError extends Error {
|
6
|
+
constructor({ toolName, parameters, cause, }) {
|
7
|
+
super(`Parameter validation failed for tool '${toolName}'. ` +
|
8
|
+
`Value: ${JSON.stringify(parameters)}.\n` +
|
9
|
+
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
10
|
+
Object.defineProperty(this, "toolName", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "cause", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: void 0
|
21
|
+
});
|
22
|
+
Object.defineProperty(this, "parameters", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: void 0
|
27
|
+
});
|
28
|
+
this.name = "ToolCallParametersValidationError";
|
29
|
+
this.toolName = toolName;
|
30
|
+
this.cause = cause;
|
31
|
+
this.parameters = parameters;
|
32
|
+
}
|
33
|
+
toJSON() {
|
34
|
+
return {
|
35
|
+
name: this.name,
|
36
|
+
message: this.message,
|
37
|
+
cause: this.cause,
|
38
|
+
stack: this.stack,
|
39
|
+
toolName: this.toolName,
|
40
|
+
parameter: this.parameters,
|
41
|
+
};
|
42
|
+
}
|
43
|
+
}
|
44
|
+
exports.ToolCallParametersValidationError = ToolCallParametersValidationError;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
export declare class ToolCallParametersValidationError extends Error {
|
2
|
+
readonly toolName: string;
|
3
|
+
readonly cause: unknown;
|
4
|
+
readonly parameters: unknown;
|
5
|
+
constructor({ toolName, parameters, cause, }: {
|
6
|
+
toolName: string;
|
7
|
+
parameters: unknown;
|
8
|
+
cause: unknown;
|
9
|
+
});
|
10
|
+
toJSON(): {
|
11
|
+
name: string;
|
12
|
+
message: string;
|
13
|
+
cause: unknown;
|
14
|
+
stack: string | undefined;
|
15
|
+
toolName: string;
|
16
|
+
parameter: unknown;
|
17
|
+
};
|
18
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
|
+
export class ToolCallParametersValidationError extends Error {
|
3
|
+
constructor({ toolName, parameters, cause, }) {
|
4
|
+
super(`Parameter validation failed for tool '${toolName}'. ` +
|
5
|
+
`Value: ${JSON.stringify(parameters)}.\n` +
|
6
|
+
`Error message: ${getErrorMessage(cause)}`);
|
7
|
+
Object.defineProperty(this, "toolName", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "cause", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "parameters", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
this.name = "ToolCallParametersValidationError";
|
26
|
+
this.toolName = toolName;
|
27
|
+
this.cause = cause;
|
28
|
+
this.parameters = parameters;
|
29
|
+
}
|
30
|
+
toJSON() {
|
31
|
+
return {
|
32
|
+
name: this.name,
|
33
|
+
message: this.message,
|
34
|
+
cause: this.cause,
|
35
|
+
stack: this.stack,
|
36
|
+
toolName: this.toolName,
|
37
|
+
parameter: this.parameters,
|
38
|
+
};
|
39
|
+
}
|
40
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.generateToolCall = void 0;
|
4
|
+
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
+
const ToolCallParametersValidationError_js_1 = require("./ToolCallParametersValidationError.cjs");
|
6
|
+
const ToolCallGenerationError_js_1 = require("./ToolCallGenerationError.cjs");
|
7
|
+
async function generateToolCall(model, tool, prompt, options) {
|
8
|
+
// Note: PROMPT must not be a function.
|
9
|
+
const expandedPrompt = typeof prompt === "function"
|
10
|
+
? prompt(tool)
|
11
|
+
: prompt;
|
12
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
13
|
+
functionType: "generate-tool-call",
|
14
|
+
input: expandedPrompt,
|
15
|
+
model,
|
16
|
+
options,
|
17
|
+
generateResponse: async (options) => {
|
18
|
+
try {
|
19
|
+
const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
|
20
|
+
const toolCall = result.value;
|
21
|
+
if (toolCall === null) {
|
22
|
+
throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
|
23
|
+
toolName: tool.name,
|
24
|
+
cause: "No tool call generated.",
|
25
|
+
});
|
26
|
+
}
|
27
|
+
const parseResult = tool.parameters.validate(toolCall.parameters);
|
28
|
+
if (!parseResult.success) {
|
29
|
+
throw new ToolCallParametersValidationError_js_1.ToolCallParametersValidationError({
|
30
|
+
toolName: tool.name,
|
31
|
+
parameters: toolCall.parameters,
|
32
|
+
cause: parseResult.error,
|
33
|
+
});
|
34
|
+
}
|
35
|
+
return {
|
36
|
+
response: result.response,
|
37
|
+
extractedValue: {
|
38
|
+
id: toolCall.id,
|
39
|
+
parameters: parseResult.data,
|
40
|
+
},
|
41
|
+
usage: result.usage,
|
42
|
+
};
|
43
|
+
}
|
44
|
+
catch (error) {
|
45
|
+
if (error instanceof ToolCallParametersValidationError_js_1.ToolCallParametersValidationError ||
|
46
|
+
error instanceof ToolCallGenerationError_js_1.ToolCallsGenerationError) {
|
47
|
+
throw error;
|
48
|
+
}
|
49
|
+
throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
|
50
|
+
toolName: tool.name,
|
51
|
+
cause: error,
|
52
|
+
});
|
53
|
+
}
|
54
|
+
},
|
55
|
+
});
|
56
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
57
|
+
}
|
58
|
+
exports.generateToolCall = generateToolCall;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
3
|
+
import { ToolCallDefinition } from "./ToolCallDefinition.js";
|
4
|
+
import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "./ToolCallGenerationModel.js";
|
5
|
+
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(model: ToolCallGenerationModel<PROMPT, SETTINGS>, tool: ToolCallDefinition<NAME, PARAMETERS>, prompt: PROMPT | ((tool: ToolCallDefinition<NAME, PARAMETERS>) => PROMPT), options?: FunctionOptions & {
|
6
|
+
returnType?: "structure";
|
7
|
+
}): Promise<{
|
8
|
+
id: string;
|
9
|
+
parameters: PARAMETERS;
|
10
|
+
}>;
|
11
|
+
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(model: ToolCallGenerationModel<PROMPT, SETTINGS>, tool: ToolCallDefinition<NAME, PARAMETERS>, prompt: PROMPT | ((tool: ToolCallDefinition<NAME, PARAMETERS>) => PROMPT), options: FunctionOptions & {
|
12
|
+
returnType: "full";
|
13
|
+
}): Promise<{
|
14
|
+
value: {
|
15
|
+
id: string;
|
16
|
+
parameters: PARAMETERS;
|
17
|
+
};
|
18
|
+
response: unknown;
|
19
|
+
metadata: ModelCallMetadata;
|
20
|
+
}>;
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
+
import { ToolCallParametersValidationError } from "./ToolCallParametersValidationError.js";
|
3
|
+
import { ToolCallsGenerationError } from "./ToolCallGenerationError.js";
|
4
|
+
export async function generateToolCall(model, tool, prompt, options) {
|
5
|
+
// Note: PROMPT must not be a function.
|
6
|
+
const expandedPrompt = typeof prompt === "function"
|
7
|
+
? prompt(tool)
|
8
|
+
: prompt;
|
9
|
+
const fullResponse = await executeStandardCall({
|
10
|
+
functionType: "generate-tool-call",
|
11
|
+
input: expandedPrompt,
|
12
|
+
model,
|
13
|
+
options,
|
14
|
+
generateResponse: async (options) => {
|
15
|
+
try {
|
16
|
+
const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
|
17
|
+
const toolCall = result.value;
|
18
|
+
if (toolCall === null) {
|
19
|
+
throw new ToolCallsGenerationError({
|
20
|
+
toolName: tool.name,
|
21
|
+
cause: "No tool call generated.",
|
22
|
+
});
|
23
|
+
}
|
24
|
+
const parseResult = tool.parameters.validate(toolCall.parameters);
|
25
|
+
if (!parseResult.success) {
|
26
|
+
throw new ToolCallParametersValidationError({
|
27
|
+
toolName: tool.name,
|
28
|
+
parameters: toolCall.parameters,
|
29
|
+
cause: parseResult.error,
|
30
|
+
});
|
31
|
+
}
|
32
|
+
return {
|
33
|
+
response: result.response,
|
34
|
+
extractedValue: {
|
35
|
+
id: toolCall.id,
|
36
|
+
parameters: parseResult.data,
|
37
|
+
},
|
38
|
+
usage: result.usage,
|
39
|
+
};
|
40
|
+
}
|
41
|
+
catch (error) {
|
42
|
+
if (error instanceof ToolCallParametersValidationError ||
|
43
|
+
error instanceof ToolCallsGenerationError) {
|
44
|
+
throw error;
|
45
|
+
}
|
46
|
+
throw new ToolCallsGenerationError({
|
47
|
+
toolName: tool.name,
|
48
|
+
cause: error,
|
49
|
+
});
|
50
|
+
}
|
51
|
+
},
|
52
|
+
});
|
53
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
54
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./ToolCallDefinition.cjs"), exports);
|
18
|
+
__exportStar(require("./ToolCallParametersValidationError.cjs"), exports);
|
19
|
+
__exportStar(require("./ToolCallGenerationEvent.cjs"), exports);
|
20
|
+
__exportStar(require("./ToolCallGenerationModel.cjs"), exports);
|
21
|
+
__exportStar(require("./generateToolCall.cjs"), exports);
|
package/model-function/index.cjs
CHANGED
@@ -31,6 +31,7 @@ __exportStar(require("./generate-image/generateImage.cjs"), exports);
|
|
31
31
|
__exportStar(require("./generate-speech/index.cjs"), exports);
|
32
32
|
__exportStar(require("./generate-structure/index.cjs"), exports);
|
33
33
|
__exportStar(require("./generate-text/index.cjs"), exports);
|
34
|
+
__exportStar(require("./generate-tool-call/index.cjs"), exports);
|
34
35
|
__exportStar(require("./generate-transcription/TranscriptionEvent.cjs"), exports);
|
35
36
|
__exportStar(require("./generate-transcription/TranscriptionModel.cjs"), exports);
|
36
37
|
__exportStar(require("./generate-transcription/generateTranscription.cjs"), exports);
|
@@ -15,6 +15,7 @@ export * from "./generate-image/generateImage.js";
|
|
15
15
|
export * from "./generate-speech/index.js";
|
16
16
|
export * from "./generate-structure/index.js";
|
17
17
|
export * from "./generate-text/index.js";
|
18
|
+
export * from "./generate-tool-call/index.js";
|
18
19
|
export * from "./generate-transcription/TranscriptionEvent.js";
|
19
20
|
export * from "./generate-transcription/TranscriptionModel.js";
|
20
21
|
export * from "./generate-transcription/generateTranscription.js";
|
package/model-function/index.js
CHANGED
@@ -15,6 +15,7 @@ export * from "./generate-image/generateImage.js";
|
|
15
15
|
export * from "./generate-speech/index.js";
|
16
16
|
export * from "./generate-structure/index.js";
|
17
17
|
export * from "./generate-text/index.js";
|
18
|
+
export * from "./generate-tool-call/index.js";
|
18
19
|
export * from "./generate-transcription/TranscriptionEvent.js";
|
19
20
|
export * from "./generate-transcription/TranscriptionModel.js";
|
20
21
|
export * from "./generate-transcription/generateTranscription.js";
|
@@ -19,8 +19,8 @@ export declare class Automatic1111ImageGenerationModel extends AbstractModel<Aut
|
|
19
19
|
get settingsForEvent(): Partial<Automatic1111ImageGenerationSettings>;
|
20
20
|
doGenerateImage(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
|
21
21
|
response: {
|
22
|
-
images: string[];
|
23
22
|
parameters: {};
|
23
|
+
images: string[];
|
24
24
|
info: string;
|
25
25
|
};
|
26
26
|
base64Image: string;
|
@@ -42,12 +42,12 @@ declare const Automatic1111ImageGenerationResponseSchema: z.ZodObject<{
|
|
42
42
|
parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
|
43
43
|
info: z.ZodString;
|
44
44
|
}, "strip", z.ZodTypeAny, {
|
45
|
-
images: string[];
|
46
45
|
parameters: {};
|
46
|
+
images: string[];
|
47
47
|
info: string;
|
48
48
|
}, {
|
49
|
-
images: string[];
|
50
49
|
parameters: {};
|
50
|
+
images: string[];
|
51
51
|
info: string;
|
52
52
|
}>;
|
53
53
|
export type Automatic1111ImageGenerationResponse = z.infer<typeof Automatic1111ImageGenerationResponseSchema>;
|
@@ -8,6 +8,7 @@ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
|
8
8
|
const zod_1 = require("zod");
|
9
9
|
const callWithRetryAndThrottle_js_1 = require("../../../core/api/callWithRetryAndThrottle.cjs");
|
10
10
|
const postToApi_js_1 = require("../../../core/api/postToApi.cjs");
|
11
|
+
const parseJSON_js_1 = require("../../../core/schema/parseJSON.cjs");
|
11
12
|
const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
|
12
13
|
const StructureParseError_js_1 = require("../../../model-function/generate-structure/StructureParseError.cjs");
|
13
14
|
const parsePartialJson_js_1 = require("../../../model-function/generate-structure/parsePartialJson.cjs");
|
@@ -210,9 +211,11 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
210
211
|
throttle: this.settings.api?.throttle,
|
211
212
|
call: async () => callOpenAIChatCompletionAPI({
|
212
213
|
...this.settings,
|
213
|
-
// function calling:
|
214
|
+
// function & tool calling:
|
214
215
|
functions: options.functions ?? this.settings.functions,
|
215
216
|
functionCall: options.functionCall ?? this.settings.functionCall,
|
217
|
+
tools: options.tools ?? this.settings.tools,
|
218
|
+
toolChoice: options.toolChoice ?? this.settings.toolChoice,
|
216
219
|
// map to OpenAI API names:
|
217
220
|
stop: this.settings.stopSequences,
|
218
221
|
maxTokens: this.settings.maxCompletionTokens,
|
@@ -358,6 +361,37 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
358
361
|
});
|
359
362
|
}
|
360
363
|
}
|
364
|
+
async doGenerateToolCall(tool, prompt, options) {
|
365
|
+
const response = await this.callAPI(prompt, {
|
366
|
+
...options,
|
367
|
+
responseFormat: exports.OpenAIChatResponseFormat.json,
|
368
|
+
toolChoice: {
|
369
|
+
type: "function",
|
370
|
+
function: { name: tool.name },
|
371
|
+
},
|
372
|
+
tools: [
|
373
|
+
{
|
374
|
+
type: "function",
|
375
|
+
function: {
|
376
|
+
name: tool.name,
|
377
|
+
description: tool.description,
|
378
|
+
parameters: tool.parameters.getJsonSchema(),
|
379
|
+
},
|
380
|
+
},
|
381
|
+
],
|
382
|
+
});
|
383
|
+
const toolCalls = response.choices[0]?.message.tool_calls;
|
384
|
+
return {
|
385
|
+
response,
|
386
|
+
value: toolCalls == null || toolCalls.length === 0
|
387
|
+
? null
|
388
|
+
: {
|
389
|
+
id: toolCalls[0].id,
|
390
|
+
parameters: (0, parseJSON_js_1.parseJSON)({ text: toolCalls[0].function.arguments }),
|
391
|
+
},
|
392
|
+
usage: this.extractUsage(response),
|
393
|
+
};
|
394
|
+
}
|
361
395
|
extractUsage(response) {
|
362
396
|
return {
|
363
397
|
promptTokens: response.usage.prompt_tokens,
|
@@ -405,6 +439,16 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
405
439
|
arguments: zod_1.z.string(),
|
406
440
|
})
|
407
441
|
.optional(),
|
442
|
+
tool_calls: zod_1.z
|
443
|
+
.array(zod_1.z.object({
|
444
|
+
id: zod_1.z.string(),
|
445
|
+
type: zod_1.z.literal("function"),
|
446
|
+
function: zod_1.z.object({
|
447
|
+
name: zod_1.z.string(),
|
448
|
+
arguments: zod_1.z.string(),
|
449
|
+
}),
|
450
|
+
}))
|
451
|
+
.optional(),
|
408
452
|
}),
|
409
453
|
index: zod_1.z.number(),
|
410
454
|
logprobs: zod_1.z.nullable(zod_1.z.any()),
|
@@ -429,7 +473,7 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
429
473
|
total_tokens: zod_1.z.number(),
|
430
474
|
}),
|
431
475
|
});
|
432
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
476
|
+
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, tools, toolChoice, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
433
477
|
// empty arrays are not allowed for stop:
|
434
478
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
435
479
|
stop = undefined;
|
@@ -443,6 +487,8 @@ async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js
|
|
443
487
|
messages,
|
444
488
|
functions,
|
445
489
|
function_call: functionCall,
|
490
|
+
tools,
|
491
|
+
tool_choice: toolChoice,
|
446
492
|
temperature,
|
447
493
|
top_p: topP,
|
448
494
|
n,
|
@@ -10,6 +10,8 @@ import { StructureOrTextGenerationModel } from "../../../model-function/generate
|
|
10
10
|
import { PromptFormatTextStreamingModel } from "../../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
11
11
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../../model-function/generate-text/TextGenerationModel.js";
|
12
12
|
import { TextGenerationPromptFormat } from "../../../model-function/generate-text/TextGenerationPromptFormat.js";
|
13
|
+
import { ToolCallDefinition } from "../../../model-function/generate-tool-call/ToolCallDefinition.js";
|
14
|
+
import { ToolCallGenerationModel } from "../../../model-function/generate-tool-call/ToolCallGenerationModel.js";
|
13
15
|
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
14
16
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
15
17
|
export declare const OPENAI_CHAT_MODELS: {
|
@@ -117,6 +119,20 @@ export interface OpenAIChatCallSettings {
|
|
117
119
|
functionCall?: "none" | "auto" | {
|
118
120
|
name: string;
|
119
121
|
};
|
122
|
+
tools?: Array<{
|
123
|
+
type: "function";
|
124
|
+
function: {
|
125
|
+
name: string;
|
126
|
+
description?: string;
|
127
|
+
parameters: unknown;
|
128
|
+
};
|
129
|
+
}>;
|
130
|
+
toolChoice?: "none" | "auto" | {
|
131
|
+
type: "function";
|
132
|
+
function: {
|
133
|
+
name: string;
|
134
|
+
};
|
135
|
+
};
|
120
136
|
stop?: string | string[];
|
121
137
|
maxTokens?: number;
|
122
138
|
temperature?: number;
|
@@ -152,7 +168,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
152
168
|
* ),
|
153
169
|
* ]);
|
154
170
|
*/
|
155
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatSettings> {
|
171
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, ToolCallGenerationModel<OpenAIChatMessage[], OpenAIChatSettings> {
|
156
172
|
constructor(settings: OpenAIChatSettings);
|
157
173
|
readonly provider: "openai";
|
158
174
|
get modelName(): OpenAIChatModelType;
|
@@ -166,14 +182,10 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
166
182
|
callAPI<RESULT>(messages: Array<OpenAIChatMessage>, options: {
|
167
183
|
responseFormat: OpenAIChatResponseFormatType<RESULT>;
|
168
184
|
} & FunctionOptions & {
|
169
|
-
functions?:
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
}>;
|
174
|
-
functionCall?: "none" | "auto" | {
|
175
|
-
name: string;
|
176
|
-
};
|
185
|
+
functions?: OpenAIChatCallSettings["functions"];
|
186
|
+
functionCall?: OpenAIChatCallSettings["functionCall"];
|
187
|
+
tools?: OpenAIChatCallSettings["tools"];
|
188
|
+
toolChoice?: OpenAIChatCallSettings["toolChoice"];
|
177
189
|
}): Promise<RESULT>;
|
178
190
|
get settingsForEvent(): Partial<OpenAIChatSettings>;
|
179
191
|
doGenerateText(prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
@@ -194,6 +206,14 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
194
206
|
name: string;
|
195
207
|
arguments: string;
|
196
208
|
} | undefined;
|
209
|
+
tool_calls?: {
|
210
|
+
function: {
|
211
|
+
name: string;
|
212
|
+
arguments: string;
|
213
|
+
};
|
214
|
+
type: "function";
|
215
|
+
id: string;
|
216
|
+
}[] | undefined;
|
197
217
|
};
|
198
218
|
index: number;
|
199
219
|
logprobs?: any;
|
@@ -235,6 +255,14 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
235
255
|
name: string;
|
236
256
|
arguments: string;
|
237
257
|
} | undefined;
|
258
|
+
tool_calls?: {
|
259
|
+
function: {
|
260
|
+
name: string;
|
261
|
+
arguments: string;
|
262
|
+
};
|
263
|
+
type: "function";
|
264
|
+
id: string;
|
265
|
+
}[] | undefined;
|
238
266
|
};
|
239
267
|
index: number;
|
240
268
|
logprobs?: any;
|
@@ -270,6 +298,14 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
270
298
|
name: string;
|
271
299
|
arguments: string;
|
272
300
|
} | undefined;
|
301
|
+
tool_calls?: {
|
302
|
+
function: {
|
303
|
+
name: string;
|
304
|
+
arguments: string;
|
305
|
+
};
|
306
|
+
type: "function";
|
307
|
+
id: string;
|
308
|
+
}[] | undefined;
|
273
309
|
};
|
274
310
|
index: number;
|
275
311
|
logprobs?: any;
|
@@ -307,6 +343,14 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
307
343
|
name: string;
|
308
344
|
arguments: string;
|
309
345
|
} | undefined;
|
346
|
+
tool_calls?: {
|
347
|
+
function: {
|
348
|
+
name: string;
|
349
|
+
arguments: string;
|
350
|
+
};
|
351
|
+
type: "function";
|
352
|
+
id: string;
|
353
|
+
}[] | undefined;
|
310
354
|
};
|
311
355
|
index: number;
|
312
356
|
logprobs?: any;
|
@@ -327,6 +371,50 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
327
371
|
totalTokens: number;
|
328
372
|
};
|
329
373
|
}>;
|
374
|
+
doGenerateToolCall(tool: ToolCallDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
375
|
+
response: {
|
376
|
+
object: "chat.completion";
|
377
|
+
usage: {
|
378
|
+
prompt_tokens: number;
|
379
|
+
total_tokens: number;
|
380
|
+
completion_tokens: number;
|
381
|
+
};
|
382
|
+
model: string;
|
383
|
+
id: string;
|
384
|
+
choices: {
|
385
|
+
message: {
|
386
|
+
content: string | null;
|
387
|
+
role: "assistant";
|
388
|
+
function_call?: {
|
389
|
+
name: string;
|
390
|
+
arguments: string;
|
391
|
+
} | undefined;
|
392
|
+
tool_calls?: {
|
393
|
+
function: {
|
394
|
+
name: string;
|
395
|
+
arguments: string;
|
396
|
+
};
|
397
|
+
type: "function";
|
398
|
+
id: string;
|
399
|
+
}[] | undefined;
|
400
|
+
};
|
401
|
+
index: number;
|
402
|
+
logprobs?: any;
|
403
|
+
finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
|
404
|
+
}[];
|
405
|
+
created: number;
|
406
|
+
system_fingerprint?: string | undefined;
|
407
|
+
};
|
408
|
+
value: {
|
409
|
+
id: string;
|
410
|
+
parameters: unknown;
|
411
|
+
} | null;
|
412
|
+
usage: {
|
413
|
+
promptTokens: number;
|
414
|
+
completionTokens: number;
|
415
|
+
totalTokens: number;
|
416
|
+
};
|
417
|
+
}>;
|
330
418
|
extractUsage(response: OpenAIChatResponse): {
|
331
419
|
promptTokens: number;
|
332
420
|
completionTokens: number;
|
@@ -359,6 +447,34 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
359
447
|
name: string;
|
360
448
|
arguments: string;
|
361
449
|
}>>;
|
450
|
+
tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
451
|
+
id: z.ZodString;
|
452
|
+
type: z.ZodLiteral<"function">;
|
453
|
+
function: z.ZodObject<{
|
454
|
+
name: z.ZodString;
|
455
|
+
arguments: z.ZodString;
|
456
|
+
}, "strip", z.ZodTypeAny, {
|
457
|
+
name: string;
|
458
|
+
arguments: string;
|
459
|
+
}, {
|
460
|
+
name: string;
|
461
|
+
arguments: string;
|
462
|
+
}>;
|
463
|
+
}, "strip", z.ZodTypeAny, {
|
464
|
+
function: {
|
465
|
+
name: string;
|
466
|
+
arguments: string;
|
467
|
+
};
|
468
|
+
type: "function";
|
469
|
+
id: string;
|
470
|
+
}, {
|
471
|
+
function: {
|
472
|
+
name: string;
|
473
|
+
arguments: string;
|
474
|
+
};
|
475
|
+
type: "function";
|
476
|
+
id: string;
|
477
|
+
}>, "many">>;
|
362
478
|
}, "strip", z.ZodTypeAny, {
|
363
479
|
content: string | null;
|
364
480
|
role: "assistant";
|
@@ -366,6 +482,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
366
482
|
name: string;
|
367
483
|
arguments: string;
|
368
484
|
} | undefined;
|
485
|
+
tool_calls?: {
|
486
|
+
function: {
|
487
|
+
name: string;
|
488
|
+
arguments: string;
|
489
|
+
};
|
490
|
+
type: "function";
|
491
|
+
id: string;
|
492
|
+
}[] | undefined;
|
369
493
|
}, {
|
370
494
|
content: string | null;
|
371
495
|
role: "assistant";
|
@@ -373,6 +497,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
373
497
|
name: string;
|
374
498
|
arguments: string;
|
375
499
|
} | undefined;
|
500
|
+
tool_calls?: {
|
501
|
+
function: {
|
502
|
+
name: string;
|
503
|
+
arguments: string;
|
504
|
+
};
|
505
|
+
type: "function";
|
506
|
+
id: string;
|
507
|
+
}[] | undefined;
|
376
508
|
}>;
|
377
509
|
index: z.ZodNumber;
|
378
510
|
logprobs: z.ZodNullable<z.ZodAny>;
|
@@ -385,6 +517,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
385
517
|
name: string;
|
386
518
|
arguments: string;
|
387
519
|
} | undefined;
|
520
|
+
tool_calls?: {
|
521
|
+
function: {
|
522
|
+
name: string;
|
523
|
+
arguments: string;
|
524
|
+
};
|
525
|
+
type: "function";
|
526
|
+
id: string;
|
527
|
+
}[] | undefined;
|
388
528
|
};
|
389
529
|
index: number;
|
390
530
|
logprobs?: any;
|
@@ -397,6 +537,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
397
537
|
name: string;
|
398
538
|
arguments: string;
|
399
539
|
} | undefined;
|
540
|
+
tool_calls?: {
|
541
|
+
function: {
|
542
|
+
name: string;
|
543
|
+
arguments: string;
|
544
|
+
};
|
545
|
+
type: "function";
|
546
|
+
id: string;
|
547
|
+
}[] | undefined;
|
400
548
|
};
|
401
549
|
index: number;
|
402
550
|
logprobs?: any;
|
@@ -436,6 +584,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
436
584
|
name: string;
|
437
585
|
arguments: string;
|
438
586
|
} | undefined;
|
587
|
+
tool_calls?: {
|
588
|
+
function: {
|
589
|
+
name: string;
|
590
|
+
arguments: string;
|
591
|
+
};
|
592
|
+
type: "function";
|
593
|
+
id: string;
|
594
|
+
}[] | undefined;
|
439
595
|
};
|
440
596
|
index: number;
|
441
597
|
logprobs?: any;
|
@@ -460,6 +616,14 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
460
616
|
name: string;
|
461
617
|
arguments: string;
|
462
618
|
} | undefined;
|
619
|
+
tool_calls?: {
|
620
|
+
function: {
|
621
|
+
name: string;
|
622
|
+
arguments: string;
|
623
|
+
};
|
624
|
+
type: "function";
|
625
|
+
id: string;
|
626
|
+
}[] | undefined;
|
463
627
|
};
|
464
628
|
index: number;
|
465
629
|
logprobs?: any;
|
@@ -496,6 +660,14 @@ export declare const OpenAIChatResponseFormat: {
|
|
496
660
|
name: string;
|
497
661
|
arguments: string;
|
498
662
|
} | undefined;
|
663
|
+
tool_calls?: {
|
664
|
+
function: {
|
665
|
+
name: string;
|
666
|
+
arguments: string;
|
667
|
+
};
|
668
|
+
type: "function";
|
669
|
+
id: string;
|
670
|
+
}[] | undefined;
|
499
671
|
};
|
500
672
|
index: number;
|
501
673
|
logprobs?: any;
|
@@ -2,6 +2,7 @@ import SecureJSON from "secure-json-parse";
|
|
2
2
|
import { z } from "zod";
|
3
3
|
import { callWithRetryAndThrottle } from "../../../core/api/callWithRetryAndThrottle.js";
|
4
4
|
import { createJsonResponseHandler, postJsonToApi, } from "../../../core/api/postToApi.js";
|
5
|
+
import { parseJSON } from "../../../core/schema/parseJSON.js";
|
5
6
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
6
7
|
import { StructureParseError } from "../../../model-function/generate-structure/StructureParseError.js";
|
7
8
|
import { parsePartialJson } from "../../../model-function/generate-structure/parsePartialJson.js";
|
@@ -201,9 +202,11 @@ export class OpenAIChatModel extends AbstractModel {
|
|
201
202
|
throttle: this.settings.api?.throttle,
|
202
203
|
call: async () => callOpenAIChatCompletionAPI({
|
203
204
|
...this.settings,
|
204
|
-
// function calling:
|
205
|
+
// function & tool calling:
|
205
206
|
functions: options.functions ?? this.settings.functions,
|
206
207
|
functionCall: options.functionCall ?? this.settings.functionCall,
|
208
|
+
tools: options.tools ?? this.settings.tools,
|
209
|
+
toolChoice: options.toolChoice ?? this.settings.toolChoice,
|
207
210
|
// map to OpenAI API names:
|
208
211
|
stop: this.settings.stopSequences,
|
209
212
|
maxTokens: this.settings.maxCompletionTokens,
|
@@ -349,6 +352,37 @@ export class OpenAIChatModel extends AbstractModel {
|
|
349
352
|
});
|
350
353
|
}
|
351
354
|
}
|
355
|
+
async doGenerateToolCall(tool, prompt, options) {
|
356
|
+
const response = await this.callAPI(prompt, {
|
357
|
+
...options,
|
358
|
+
responseFormat: OpenAIChatResponseFormat.json,
|
359
|
+
toolChoice: {
|
360
|
+
type: "function",
|
361
|
+
function: { name: tool.name },
|
362
|
+
},
|
363
|
+
tools: [
|
364
|
+
{
|
365
|
+
type: "function",
|
366
|
+
function: {
|
367
|
+
name: tool.name,
|
368
|
+
description: tool.description,
|
369
|
+
parameters: tool.parameters.getJsonSchema(),
|
370
|
+
},
|
371
|
+
},
|
372
|
+
],
|
373
|
+
});
|
374
|
+
const toolCalls = response.choices[0]?.message.tool_calls;
|
375
|
+
return {
|
376
|
+
response,
|
377
|
+
value: toolCalls == null || toolCalls.length === 0
|
378
|
+
? null
|
379
|
+
: {
|
380
|
+
id: toolCalls[0].id,
|
381
|
+
parameters: parseJSON({ text: toolCalls[0].function.arguments }),
|
382
|
+
},
|
383
|
+
usage: this.extractUsage(response),
|
384
|
+
};
|
385
|
+
}
|
352
386
|
extractUsage(response) {
|
353
387
|
return {
|
354
388
|
promptTokens: response.usage.prompt_tokens,
|
@@ -395,6 +429,16 @@ const openAIChatResponseSchema = z.object({
|
|
395
429
|
arguments: z.string(),
|
396
430
|
})
|
397
431
|
.optional(),
|
432
|
+
tool_calls: z
|
433
|
+
.array(z.object({
|
434
|
+
id: z.string(),
|
435
|
+
type: z.literal("function"),
|
436
|
+
function: z.object({
|
437
|
+
name: z.string(),
|
438
|
+
arguments: z.string(),
|
439
|
+
}),
|
440
|
+
}))
|
441
|
+
.optional(),
|
398
442
|
}),
|
399
443
|
index: z.number(),
|
400
444
|
logprobs: z.nullable(z.any()),
|
@@ -419,7 +463,7 @@ const openAIChatResponseSchema = z.object({
|
|
419
463
|
total_tokens: z.number(),
|
420
464
|
}),
|
421
465
|
});
|
422
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
466
|
+
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, tools, toolChoice, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
423
467
|
// empty arrays are not allowed for stop:
|
424
468
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
425
469
|
stop = undefined;
|
@@ -433,6 +477,8 @@ async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(),
|
|
433
477
|
messages,
|
434
478
|
functions,
|
435
479
|
function_call: functionCall,
|
480
|
+
tools,
|
481
|
+
tool_choice: toolChoice,
|
436
482
|
temperature,
|
437
483
|
top_p: topP,
|
438
484
|
n,
|
@@ -18,6 +18,16 @@ const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.objec
|
|
18
18
|
arguments: zod_1.z.string().optional(),
|
19
19
|
})
|
20
20
|
.optional(),
|
21
|
+
tool_calls: zod_1.z
|
22
|
+
.array(zod_1.z.object({
|
23
|
+
id: zod_1.z.string(),
|
24
|
+
type: zod_1.z.literal("function"),
|
25
|
+
function: zod_1.z.object({
|
26
|
+
name: zod_1.z.string(),
|
27
|
+
arguments: zod_1.z.string(),
|
28
|
+
}),
|
29
|
+
}))
|
30
|
+
.optional(),
|
21
31
|
}),
|
22
32
|
finish_reason: zod_1.z
|
23
33
|
.enum([
|
@@ -15,6 +15,16 @@ const chatResponseStreamEventSchema = new ZodSchema(z.object({
|
|
15
15
|
arguments: z.string().optional(),
|
16
16
|
})
|
17
17
|
.optional(),
|
18
|
+
tool_calls: z
|
19
|
+
.array(z.object({
|
20
|
+
id: z.string(),
|
21
|
+
type: z.literal("function"),
|
22
|
+
function: z.object({
|
23
|
+
name: z.string(),
|
24
|
+
arguments: z.string(),
|
25
|
+
}),
|
26
|
+
}))
|
27
|
+
.optional(),
|
18
28
|
}),
|
19
29
|
finish_reason: z
|
20
30
|
.enum([
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.69.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -81,8 +81,8 @@
|
|
81
81
|
"eslint-config-prettier": "9.0.0",
|
82
82
|
"fastify": "^4.0.0",
|
83
83
|
"husky": "^8.0.3",
|
84
|
-
"lint-staged": "15.0
|
85
|
-
"prettier": "3.0
|
84
|
+
"lint-staged": "15.1.0",
|
85
|
+
"prettier": "3.1.0",
|
86
86
|
"rimraf": "5.0.5",
|
87
87
|
"typescript": "5.2.2",
|
88
88
|
"vitest": "^0.34.5"
|