modelfusion 0.77.0 → 0.79.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/model-function/generate-text/PromptFormatTextGenerationModel.cjs +14 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.d.ts +4 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.js +14 -0
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.cjs +5 -2
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.js +5 -2
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +8 -1
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +2 -0
- package/model-provider/ollama/OllamaTextGenerationModel.js +8 -1
- package/package.json +1 -1
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +1 -1
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +1 -1
- package/tool/{ToolCallParseError.cjs → generate-tool-call/ToolCallParseError.cjs} +1 -1
- package/tool/{ToolCallParseError.js → generate-tool-call/ToolCallParseError.js} +1 -1
- package/tool/generate-tool-call/index.cjs +1 -14
- package/tool/generate-tool-call/index.d.ts +1 -1
- package/tool/generate-tool-call/index.js +1 -1
- package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs +60 -0
- package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.d.ts +41 -0
- package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js +56 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextParseError.cjs +36 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextParseError.d.ts +15 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextParseError.js +32 -0
- package/tool/generate-tool-calls-or-text/index.cjs +2 -0
- package/tool/generate-tool-calls-or-text/index.d.ts +2 -0
- package/tool/generate-tool-calls-or-text/index.js +2 -0
- package/tool/generate-tool-call/FunctionListToolCallPromptFormat.cjs +0 -70
- package/tool/generate-tool-call/FunctionListToolCallPromptFormat.d.ts +0 -14
- package/tool/generate-tool-call/FunctionListToolCallPromptFormat.js +0 -65
- /package/tool/{ToolCallParseError.d.ts → generate-tool-call/ToolCallParseError.d.ts} +0 -0
@@ -1,6 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.PromptFormatTextGenerationModel = void 0;
|
4
|
+
const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
|
5
|
+
const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
|
4
6
|
class PromptFormatTextGenerationModel {
|
5
7
|
constructor({ model, promptFormat, }) {
|
6
8
|
Object.defineProperty(this, "model", {
|
@@ -44,6 +46,18 @@ class PromptFormatTextGenerationModel {
|
|
44
46
|
get settingsForEvent() {
|
45
47
|
return this.model.settingsForEvent;
|
46
48
|
}
|
49
|
+
asToolCallGenerationModel(promptFormat) {
|
50
|
+
return new TextGenerationToolCallModel_js_1.TextGenerationToolCallModel({
|
51
|
+
model: this,
|
52
|
+
format: promptFormat,
|
53
|
+
});
|
54
|
+
}
|
55
|
+
asToolCallsOrTextGenerationModel(promptFormat) {
|
56
|
+
return new TextGenerationToolCallsOrGenerateTextModel_js_1.TextGenerationToolCallsOrGenerateTextModel({
|
57
|
+
model: this,
|
58
|
+
format: promptFormat,
|
59
|
+
});
|
60
|
+
}
|
47
61
|
withPromptFormat(promptFormat) {
|
48
62
|
return new PromptFormatTextGenerationModel({
|
49
63
|
model: this.withSettings({
|
@@ -1,4 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { TextGenerationToolCallModel, ToolCallPromptFormat } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
3
|
+
import { TextGenerationToolCallsOrGenerateTextModel, ToolCallsOrGenerateTextPromptFormat } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
2
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
3
5
|
import { TextGenerationPromptFormat } from "./TextGenerationPromptFormat.js";
|
4
6
|
export declare class PromptFormatTextGenerationModel<PROMPT, MODEL_PROMPT, SETTINGS extends TextGenerationModelSettings, MODEL extends TextGenerationModel<MODEL_PROMPT, SETTINGS>> implements TextGenerationModel<PROMPT, SETTINGS> {
|
@@ -23,6 +25,8 @@ export declare class PromptFormatTextGenerationModel<PROMPT, MODEL_PROMPT, SETTI
|
|
23
25
|
} | undefined;
|
24
26
|
}>;
|
25
27
|
get settingsForEvent(): Partial<SETTINGS>;
|
28
|
+
asToolCallGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallModel<INPUT_PROMPT, PROMPT, this>;
|
29
|
+
asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallsOrGenerateTextPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, PROMPT, this>;
|
26
30
|
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
27
31
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
28
32
|
}
|
@@ -1,3 +1,5 @@
|
|
1
|
+
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
2
|
+
import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
1
3
|
export class PromptFormatTextGenerationModel {
|
2
4
|
constructor({ model, promptFormat, }) {
|
3
5
|
Object.defineProperty(this, "model", {
|
@@ -41,6 +43,18 @@ export class PromptFormatTextGenerationModel {
|
|
41
43
|
get settingsForEvent() {
|
42
44
|
return this.model.settingsForEvent;
|
43
45
|
}
|
46
|
+
asToolCallGenerationModel(promptFormat) {
|
47
|
+
return new TextGenerationToolCallModel({
|
48
|
+
model: this,
|
49
|
+
format: promptFormat,
|
50
|
+
});
|
51
|
+
}
|
52
|
+
asToolCallsOrTextGenerationModel(promptFormat) {
|
53
|
+
return new TextGenerationToolCallsOrGenerateTextModel({
|
54
|
+
model: this,
|
55
|
+
format: promptFormat,
|
56
|
+
});
|
57
|
+
}
|
44
58
|
withPromptFormat(promptFormat) {
|
45
59
|
return new PromptFormatTextGenerationModel({
|
46
60
|
model: this.withSettings({
|
@@ -16,7 +16,9 @@ function chatMLSegment(role, text) {
|
|
16
16
|
function text() {
|
17
17
|
return {
|
18
18
|
stopSequences: [END_SEGMENT],
|
19
|
-
format: (instruction) =>
|
19
|
+
format: (instruction) =>
|
20
|
+
// prompt and then prefix start of assistant response:
|
21
|
+
chatMLSegment("user", instruction) + chatMLStart("assistant"),
|
20
22
|
};
|
21
23
|
}
|
22
24
|
exports.text = text;
|
@@ -37,7 +39,8 @@ function instruction() {
|
|
37
39
|
return {
|
38
40
|
stopSequences: [END_SEGMENT],
|
39
41
|
format: (instruction) => chatMLSegment("system", instruction.system) +
|
40
|
-
chatMLSegment("user", instruction.instruction)
|
42
|
+
chatMLSegment("user", instruction.instruction) +
|
43
|
+
chatMLStart("assistant"), // prefix start of assistant response
|
41
44
|
};
|
42
45
|
}
|
43
46
|
exports.instruction = instruction;
|
@@ -13,7 +13,9 @@ function chatMLSegment(role, text) {
|
|
13
13
|
export function text() {
|
14
14
|
return {
|
15
15
|
stopSequences: [END_SEGMENT],
|
16
|
-
format: (instruction) =>
|
16
|
+
format: (instruction) =>
|
17
|
+
// prompt and then prefix start of assistant response:
|
18
|
+
chatMLSegment("user", instruction) + chatMLStart("assistant"),
|
17
19
|
};
|
18
20
|
}
|
19
21
|
/**
|
@@ -33,7 +35,8 @@ export function instruction() {
|
|
33
35
|
return {
|
34
36
|
stopSequences: [END_SEGMENT],
|
35
37
|
format: (instruction) => chatMLSegment("system", instruction.system) +
|
36
|
-
chatMLSegment("user", instruction.instruction)
|
38
|
+
chatMLSegment("user", instruction.instruction) +
|
39
|
+
chatMLStart("assistant"), // prefix start of assistant response
|
37
40
|
};
|
38
41
|
}
|
39
42
|
/**
|
@@ -8,6 +8,7 @@ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
|
8
8
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
9
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
10
10
|
const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
|
11
|
+
const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
|
11
12
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
12
13
|
const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs");
|
13
14
|
const OllamaApiConfiguration_js_1 = require("./OllamaApiConfiguration.cjs");
|
@@ -97,7 +98,13 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
97
98
|
}
|
98
99
|
asToolCallGenerationModel(promptFormat) {
|
99
100
|
return new TextGenerationToolCallModel_js_1.TextGenerationToolCallModel({
|
100
|
-
model: this
|
101
|
+
model: this,
|
102
|
+
format: promptFormat,
|
103
|
+
});
|
104
|
+
}
|
105
|
+
asToolCallsOrTextGenerationModel(promptFormat) {
|
106
|
+
return new TextGenerationToolCallsOrGenerateTextModel_js_1.TextGenerationToolCallsOrGenerateTextModel({
|
107
|
+
model: this,
|
101
108
|
format: promptFormat,
|
102
109
|
});
|
103
110
|
}
|
@@ -8,6 +8,7 @@ import { PromptFormatTextStreamingModel } from "../../model-function/generate-te
|
|
8
8
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
9
9
|
import { TextGenerationPromptFormat } from "../../model-function/generate-text/TextGenerationPromptFormat.js";
|
10
10
|
import { TextGenerationToolCallModel, ToolCallPromptFormat } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
11
|
+
import { TextGenerationToolCallsOrGenerateTextModel, ToolCallsOrGenerateTextPromptFormat } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
11
12
|
/**
|
12
13
|
* @see https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion
|
13
14
|
*/
|
@@ -137,6 +138,7 @@ export declare class OllamaTextGenerationModel<CONTEXT_WINDOW_SIZE extends numbe
|
|
137
138
|
}>;
|
138
139
|
doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
139
140
|
asToolCallGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallPromptFormat<INPUT_PROMPT, string>): TextGenerationToolCallModel<INPUT_PROMPT, string, this>;
|
141
|
+
asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallsOrGenerateTextPromptFormat<INPUT_PROMPT, string>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, string, this>;
|
140
142
|
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, string>): PromptFormatTextStreamingModel<INPUT_PROMPT, string, OllamaTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>, this>;
|
141
143
|
withSettings(additionalSettings: Partial<OllamaTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>>): this;
|
142
144
|
}
|
@@ -5,6 +5,7 @@ import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
|
5
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
6
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
7
7
|
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
8
|
+
import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
8
9
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
9
10
|
import { parseJsonStream } from "../../util/streaming/parseJsonStream.js";
|
10
11
|
import { OllamaApiConfiguration } from "./OllamaApiConfiguration.js";
|
@@ -94,7 +95,13 @@ export class OllamaTextGenerationModel extends AbstractModel {
|
|
94
95
|
}
|
95
96
|
asToolCallGenerationModel(promptFormat) {
|
96
97
|
return new TextGenerationToolCallModel({
|
97
|
-
model: this
|
98
|
+
model: this,
|
99
|
+
format: promptFormat,
|
100
|
+
});
|
101
|
+
}
|
102
|
+
asToolCallsOrTextGenerationModel(promptFormat) {
|
103
|
+
return new TextGenerationToolCallsOrGenerateTextModel({
|
104
|
+
model: this,
|
98
105
|
format: promptFormat,
|
99
106
|
});
|
100
107
|
}
|
package/package.json
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.TextGenerationToolCallModel = void 0;
|
4
4
|
const generateText_js_1 = require("../../model-function/generate-text/generateText.cjs");
|
5
|
-
const ToolCallParseError_js_1 = require("
|
5
|
+
const ToolCallParseError_js_1 = require("./ToolCallParseError.cjs");
|
6
6
|
class TextGenerationToolCallModel {
|
7
7
|
constructor({ model, format, }) {
|
8
8
|
Object.defineProperty(this, "model", {
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { generateText } from "../../model-function/generate-text/generateText.js";
|
2
|
-
import { ToolCallParseError } from "
|
2
|
+
import { ToolCallParseError } from "./ToolCallParseError.js";
|
3
3
|
export class TextGenerationToolCallModel {
|
4
4
|
constructor({ model, format, }) {
|
5
5
|
Object.defineProperty(this, "model", {
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.ToolCallParseError = void 0;
|
4
|
-
const getErrorMessage_js_1 = require("
|
4
|
+
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
5
|
class ToolCallParseError extends Error {
|
6
6
|
constructor({ toolName, valueText, cause, }) {
|
7
7
|
super(`Tool call parsing failed for '${toolName}'. ` +
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { getErrorMessage } from "
|
1
|
+
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
2
|
export class ToolCallParseError extends Error {
|
3
3
|
constructor({ toolName, valueText, cause, }) {
|
4
4
|
super(`Tool call parsing failed for '${toolName}'. ` +
|
@@ -10,25 +10,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
11
11
|
o[k2] = m[k];
|
12
12
|
}));
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
-
}) : function(o, v) {
|
16
|
-
o["default"] = v;
|
17
|
-
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
-
if (mod && mod.__esModule) return mod;
|
20
|
-
var result = {};
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
-
__setModuleDefault(result, mod);
|
23
|
-
return result;
|
24
|
-
};
|
25
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
26
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
27
15
|
};
|
28
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.FunctionListToolCallPromptFormat = void 0;
|
30
|
-
exports.FunctionListToolCallPromptFormat = __importStar(require("./FunctionListToolCallPromptFormat.cjs"));
|
31
17
|
__exportStar(require("./TextGenerationToolCallModel.cjs"), exports);
|
32
18
|
__exportStar(require("./ToolCallGenerationEvent.cjs"), exports);
|
33
19
|
__exportStar(require("./ToolCallGenerationModel.cjs"), exports);
|
20
|
+
__exportStar(require("./ToolCallParseError.cjs"), exports);
|
34
21
|
__exportStar(require("./generateToolCall.cjs"), exports);
|
@@ -1,5 +1,5 @@
|
|
1
|
-
export * as FunctionListToolCallPromptFormat from "./FunctionListToolCallPromptFormat.js";
|
2
1
|
export * from "./TextGenerationToolCallModel.js";
|
3
2
|
export * from "./ToolCallGenerationEvent.js";
|
4
3
|
export * from "./ToolCallGenerationModel.js";
|
4
|
+
export * from "./ToolCallParseError.js";
|
5
5
|
export * from "./generateToolCall.js";
|
@@ -1,5 +1,5 @@
|
|
1
|
-
export * as FunctionListToolCallPromptFormat from "./FunctionListToolCallPromptFormat.js";
|
2
1
|
export * from "./TextGenerationToolCallModel.js";
|
3
2
|
export * from "./ToolCallGenerationEvent.js";
|
4
3
|
export * from "./ToolCallGenerationModel.js";
|
4
|
+
export * from "./ToolCallParseError.js";
|
5
5
|
export * from "./generateToolCall.js";
|
@@ -0,0 +1,60 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TextGenerationToolCallsOrGenerateTextModel = void 0;
|
4
|
+
const generateText_js_1 = require("../../model-function/generate-text/generateText.cjs");
|
5
|
+
const ToolCallsOrTextParseError_js_1 = require("./ToolCallsOrTextParseError.cjs");
|
6
|
+
class TextGenerationToolCallsOrGenerateTextModel {
|
7
|
+
constructor({ model, format, }) {
|
8
|
+
Object.defineProperty(this, "model", {
|
9
|
+
enumerable: true,
|
10
|
+
configurable: true,
|
11
|
+
writable: true,
|
12
|
+
value: void 0
|
13
|
+
});
|
14
|
+
Object.defineProperty(this, "format", {
|
15
|
+
enumerable: true,
|
16
|
+
configurable: true,
|
17
|
+
writable: true,
|
18
|
+
value: void 0
|
19
|
+
});
|
20
|
+
this.model = model;
|
21
|
+
this.format = format;
|
22
|
+
}
|
23
|
+
get modelInformation() {
|
24
|
+
return this.model.modelInformation;
|
25
|
+
}
|
26
|
+
get settings() {
|
27
|
+
return this.model.settings;
|
28
|
+
}
|
29
|
+
get settingsForEvent() {
|
30
|
+
return this.model.settingsForEvent;
|
31
|
+
}
|
32
|
+
async doGenerateToolCallsOrText(tools, prompt, options) {
|
33
|
+
const { response, value, metadata } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, tools), {
|
34
|
+
...options,
|
35
|
+
returnType: "full",
|
36
|
+
});
|
37
|
+
try {
|
38
|
+
const { text, toolCalls } = this.format.extractToolCallsAndText(value);
|
39
|
+
return {
|
40
|
+
response,
|
41
|
+
text,
|
42
|
+
toolCalls,
|
43
|
+
usage: metadata?.usage,
|
44
|
+
};
|
45
|
+
}
|
46
|
+
catch (error) {
|
47
|
+
throw new ToolCallsOrTextParseError_js_1.ToolCallsOrTextParseError({
|
48
|
+
valueText: value,
|
49
|
+
cause: error,
|
50
|
+
});
|
51
|
+
}
|
52
|
+
}
|
53
|
+
withSettings(additionalSettings) {
|
54
|
+
return new TextGenerationToolCallsOrGenerateTextModel({
|
55
|
+
model: this.model.withSettings(additionalSettings),
|
56
|
+
format: this.format,
|
57
|
+
});
|
58
|
+
}
|
59
|
+
}
|
60
|
+
exports.TextGenerationToolCallsOrGenerateTextModel = TextGenerationToolCallsOrGenerateTextModel;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { TextGenerationModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
3
|
+
import { ToolDefinition } from "../ToolDefinition.js";
|
4
|
+
import { ToolCallsOrTextGenerationModel, ToolCallsOrTextGenerationModelSettings } from "./ToolCallsOrTextGenerationModel.js";
|
5
|
+
export interface ToolCallsOrGenerateTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT> {
|
6
|
+
createPrompt: (prompt: SOURCE_PROMPT, tools: Array<ToolDefinition<string, unknown>>) => TARGET_PROMPT;
|
7
|
+
extractToolCallsAndText: (response: string) => {
|
8
|
+
text: string | null;
|
9
|
+
toolCalls: Array<{
|
10
|
+
id: string;
|
11
|
+
name: string;
|
12
|
+
args: unknown;
|
13
|
+
}> | null;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
export declare class TextGenerationToolCallsOrGenerateTextModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL extends TextGenerationModel<TARGET_PROMPT, ToolCallsOrTextGenerationModelSettings>> implements ToolCallsOrTextGenerationModel<SOURCE_PROMPT, MODEL["settings"]> {
|
17
|
+
private readonly model;
|
18
|
+
private readonly format;
|
19
|
+
constructor({ model, format, }: {
|
20
|
+
model: MODEL;
|
21
|
+
format: ToolCallsOrGenerateTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
|
22
|
+
});
|
23
|
+
get modelInformation(): import("../../index.js").ModelInformation;
|
24
|
+
get settings(): ToolCallsOrTextGenerationModelSettings;
|
25
|
+
get settingsForEvent(): Partial<MODEL["settings"]>;
|
26
|
+
doGenerateToolCallsOrText(tools: Array<ToolDefinition<string, unknown>>, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
|
27
|
+
response: unknown;
|
28
|
+
text: string | null;
|
29
|
+
toolCalls: {
|
30
|
+
id: string;
|
31
|
+
name: string;
|
32
|
+
args: unknown;
|
33
|
+
}[] | null;
|
34
|
+
usage: {
|
35
|
+
promptTokens: number;
|
36
|
+
completionTokens: number;
|
37
|
+
totalTokens: number;
|
38
|
+
} | undefined;
|
39
|
+
}>;
|
40
|
+
withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
|
41
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import { generateText } from "../../model-function/generate-text/generateText.js";
|
2
|
+
import { ToolCallsOrTextParseError } from "./ToolCallsOrTextParseError.js";
|
3
|
+
export class TextGenerationToolCallsOrGenerateTextModel {
|
4
|
+
constructor({ model, format, }) {
|
5
|
+
Object.defineProperty(this, "model", {
|
6
|
+
enumerable: true,
|
7
|
+
configurable: true,
|
8
|
+
writable: true,
|
9
|
+
value: void 0
|
10
|
+
});
|
11
|
+
Object.defineProperty(this, "format", {
|
12
|
+
enumerable: true,
|
13
|
+
configurable: true,
|
14
|
+
writable: true,
|
15
|
+
value: void 0
|
16
|
+
});
|
17
|
+
this.model = model;
|
18
|
+
this.format = format;
|
19
|
+
}
|
20
|
+
get modelInformation() {
|
21
|
+
return this.model.modelInformation;
|
22
|
+
}
|
23
|
+
get settings() {
|
24
|
+
return this.model.settings;
|
25
|
+
}
|
26
|
+
get settingsForEvent() {
|
27
|
+
return this.model.settingsForEvent;
|
28
|
+
}
|
29
|
+
async doGenerateToolCallsOrText(tools, prompt, options) {
|
30
|
+
const { response, value, metadata } = await generateText(this.model, this.format.createPrompt(prompt, tools), {
|
31
|
+
...options,
|
32
|
+
returnType: "full",
|
33
|
+
});
|
34
|
+
try {
|
35
|
+
const { text, toolCalls } = this.format.extractToolCallsAndText(value);
|
36
|
+
return {
|
37
|
+
response,
|
38
|
+
text,
|
39
|
+
toolCalls,
|
40
|
+
usage: metadata?.usage,
|
41
|
+
};
|
42
|
+
}
|
43
|
+
catch (error) {
|
44
|
+
throw new ToolCallsOrTextParseError({
|
45
|
+
valueText: value,
|
46
|
+
cause: error,
|
47
|
+
});
|
48
|
+
}
|
49
|
+
}
|
50
|
+
withSettings(additionalSettings) {
|
51
|
+
return new TextGenerationToolCallsOrGenerateTextModel({
|
52
|
+
model: this.model.withSettings(additionalSettings),
|
53
|
+
format: this.format,
|
54
|
+
});
|
55
|
+
}
|
56
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ToolCallsOrTextParseError = void 0;
|
4
|
+
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
|
+
class ToolCallsOrTextParseError extends Error {
|
6
|
+
constructor({ valueText, cause }) {
|
7
|
+
super(`Tool calls or text parsing failed. ` +
|
8
|
+
`Value: ${valueText}.\n` +
|
9
|
+
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
10
|
+
Object.defineProperty(this, "valueText", {
|
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
|
+
this.name = "ToolCallsOrTextParseError";
|
23
|
+
this.cause = cause;
|
24
|
+
this.valueText = valueText;
|
25
|
+
}
|
26
|
+
toJSON() {
|
27
|
+
return {
|
28
|
+
name: this.name,
|
29
|
+
cause: this.cause,
|
30
|
+
message: this.message,
|
31
|
+
stack: this.stack,
|
32
|
+
valueText: this.valueText,
|
33
|
+
};
|
34
|
+
}
|
35
|
+
}
|
36
|
+
exports.ToolCallsOrTextParseError = ToolCallsOrTextParseError;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare class ToolCallsOrTextParseError extends Error {
|
2
|
+
readonly valueText: string;
|
3
|
+
readonly cause: unknown;
|
4
|
+
constructor({ valueText, cause }: {
|
5
|
+
valueText: string;
|
6
|
+
cause: unknown;
|
7
|
+
});
|
8
|
+
toJSON(): {
|
9
|
+
name: string;
|
10
|
+
cause: unknown;
|
11
|
+
message: string;
|
12
|
+
stack: string | undefined;
|
13
|
+
valueText: string;
|
14
|
+
};
|
15
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
|
+
export class ToolCallsOrTextParseError extends Error {
|
3
|
+
constructor({ valueText, cause }) {
|
4
|
+
super(`Tool calls or text parsing failed. ` +
|
5
|
+
`Value: ${valueText}.\n` +
|
6
|
+
`Error message: ${getErrorMessage(cause)}`);
|
7
|
+
Object.defineProperty(this, "valueText", {
|
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
|
+
this.name = "ToolCallsOrTextParseError";
|
20
|
+
this.cause = cause;
|
21
|
+
this.valueText = valueText;
|
22
|
+
}
|
23
|
+
toJSON() {
|
24
|
+
return {
|
25
|
+
name: this.name,
|
26
|
+
cause: this.cause,
|
27
|
+
message: this.message,
|
28
|
+
stack: this.stack,
|
29
|
+
valueText: this.valueText,
|
30
|
+
};
|
31
|
+
}
|
32
|
+
}
|
@@ -14,6 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./TextGenerationToolCallsOrGenerateTextModel.cjs"), exports);
|
17
18
|
__exportStar(require("./ToolCallsOrTextGenerationEvent.cjs"), exports);
|
18
19
|
__exportStar(require("./ToolCallsOrTextGenerationModel.cjs"), exports);
|
20
|
+
__exportStar(require("./ToolCallsOrTextParseError.cjs"), exports);
|
19
21
|
__exportStar(require("./generateToolCallsOrText.cjs"), exports);
|
@@ -1,3 +1,5 @@
|
|
1
|
+
export * from "./TextGenerationToolCallsOrGenerateTextModel.js";
|
1
2
|
export * from "./ToolCallsOrTextGenerationEvent.js";
|
2
3
|
export * from "./ToolCallsOrTextGenerationModel.js";
|
4
|
+
export * from "./ToolCallsOrTextParseError.js";
|
3
5
|
export * from "./generateToolCallsOrText.js";
|
@@ -1,3 +1,5 @@
|
|
1
|
+
export * from "./TextGenerationToolCallsOrGenerateTextModel.js";
|
1
2
|
export * from "./ToolCallsOrTextGenerationEvent.js";
|
2
3
|
export * from "./ToolCallsOrTextGenerationModel.js";
|
4
|
+
export * from "./ToolCallsOrTextParseError.js";
|
3
5
|
export * from "./generateToolCallsOrText.js";
|
@@ -1,70 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.instruction = exports.text = void 0;
|
4
|
-
const nanoid_1 = require("nanoid");
|
5
|
-
const zod_1 = require("zod");
|
6
|
-
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
|
-
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
|
-
const functionSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
9
|
-
function: zod_1.z.string(),
|
10
|
-
parameters: zod_1.z.any(),
|
11
|
-
}));
|
12
|
-
const DEFAULT_FUNCTION_CALL_PROMPT = [
|
13
|
-
``,
|
14
|
-
`Select the most suitable function and parameters ` +
|
15
|
-
`from the list of available functions below, based on the user's input. ` +
|
16
|
-
`Provide your response in JSON format.`,
|
17
|
-
``,
|
18
|
-
`Available functions:`,
|
19
|
-
].join("\n");
|
20
|
-
function text({ functionCallPrompt = DEFAULT_FUNCTION_CALL_PROMPT, baseFormat, } = {}) {
|
21
|
-
return {
|
22
|
-
createPrompt(instruction, tool) {
|
23
|
-
const instructionWithFunctionCall = [
|
24
|
-
instruction,
|
25
|
-
functionCallPrompt,
|
26
|
-
`${tool.name}:`,
|
27
|
-
` description: ${tool.description ?? ""}`,
|
28
|
-
` parameters: ${JSON.stringify(tool.parameters.getJsonSchema())}`,
|
29
|
-
``,
|
30
|
-
].join("\n");
|
31
|
-
return (baseFormat?.format(instructionWithFunctionCall) ??
|
32
|
-
// handled by signature overloading:
|
33
|
-
instructionWithFunctionCall); // eslint-disable-line @typescript-eslint/no-explicit-any
|
34
|
-
},
|
35
|
-
extractToolCall(response) {
|
36
|
-
const json = (0, parseJSON_js_1.parseJSON)({ text: response, schema: functionSchema });
|
37
|
-
return {
|
38
|
-
id: (0, nanoid_1.nanoid)(),
|
39
|
-
args: json.parameters,
|
40
|
-
};
|
41
|
-
},
|
42
|
-
};
|
43
|
-
}
|
44
|
-
exports.text = text;
|
45
|
-
function instruction({ functionCallPrompt = DEFAULT_FUNCTION_CALL_PROMPT, baseFormat, }) {
|
46
|
-
return {
|
47
|
-
createPrompt(instruction, tool) {
|
48
|
-
const instructionWithFunctionCall = [
|
49
|
-
instruction.instruction,
|
50
|
-
functionCallPrompt,
|
51
|
-
`${tool.name}:`,
|
52
|
-
` description: ${tool.description ?? ""}`,
|
53
|
-
` parameters: ${JSON.stringify(tool.parameters.getJsonSchema())}`,
|
54
|
-
``,
|
55
|
-
].join("\n");
|
56
|
-
return baseFormat.format({
|
57
|
-
...instruction,
|
58
|
-
instruction: instructionWithFunctionCall,
|
59
|
-
});
|
60
|
-
},
|
61
|
-
extractToolCall(response) {
|
62
|
-
const json = (0, parseJSON_js_1.parseJSON)({ text: response, schema: functionSchema });
|
63
|
-
return {
|
64
|
-
id: (0, nanoid_1.nanoid)(),
|
65
|
-
args: json.parameters,
|
66
|
-
};
|
67
|
-
},
|
68
|
-
};
|
69
|
-
}
|
70
|
-
exports.instruction = instruction;
|
@@ -1,14 +0,0 @@
|
|
1
|
-
import { PromptFormat } from "../../model-function/PromptFormat.js";
|
2
|
-
import { InstructionPrompt } from "../../model-function/generate-text/prompt-format/InstructionPrompt.js";
|
3
|
-
import { ToolCallPromptFormat } from "./TextGenerationToolCallModel.js";
|
4
|
-
export declare function text(options?: {
|
5
|
-
functionCallPrompt?: string;
|
6
|
-
}): ToolCallPromptFormat<string, string>;
|
7
|
-
export declare function text<TARGET_PROMPT>({ functionCallPrompt, baseFormat, }: {
|
8
|
-
functionCallPrompt?: string;
|
9
|
-
baseFormat: PromptFormat<string, TARGET_PROMPT>;
|
10
|
-
}): ToolCallPromptFormat<string, TARGET_PROMPT>;
|
11
|
-
export declare function instruction<TARGET_PROMPT>({ functionCallPrompt, baseFormat, }: {
|
12
|
-
functionCallPrompt?: string;
|
13
|
-
baseFormat: PromptFormat<InstructionPrompt, TARGET_PROMPT>;
|
14
|
-
}): ToolCallPromptFormat<InstructionPrompt, TARGET_PROMPT>;
|
@@ -1,65 +0,0 @@
|
|
1
|
-
import { nanoid } from "nanoid";
|
2
|
-
import { z } from "zod";
|
3
|
-
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
-
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
|
-
const functionSchema = new ZodSchema(z.object({
|
6
|
-
function: z.string(),
|
7
|
-
parameters: z.any(),
|
8
|
-
}));
|
9
|
-
const DEFAULT_FUNCTION_CALL_PROMPT = [
|
10
|
-
``,
|
11
|
-
`Select the most suitable function and parameters ` +
|
12
|
-
`from the list of available functions below, based on the user's input. ` +
|
13
|
-
`Provide your response in JSON format.`,
|
14
|
-
``,
|
15
|
-
`Available functions:`,
|
16
|
-
].join("\n");
|
17
|
-
export function text({ functionCallPrompt = DEFAULT_FUNCTION_CALL_PROMPT, baseFormat, } = {}) {
|
18
|
-
return {
|
19
|
-
createPrompt(instruction, tool) {
|
20
|
-
const instructionWithFunctionCall = [
|
21
|
-
instruction,
|
22
|
-
functionCallPrompt,
|
23
|
-
`${tool.name}:`,
|
24
|
-
` description: ${tool.description ?? ""}`,
|
25
|
-
` parameters: ${JSON.stringify(tool.parameters.getJsonSchema())}`,
|
26
|
-
``,
|
27
|
-
].join("\n");
|
28
|
-
return (baseFormat?.format(instructionWithFunctionCall) ??
|
29
|
-
// handled by signature overloading:
|
30
|
-
instructionWithFunctionCall); // eslint-disable-line @typescript-eslint/no-explicit-any
|
31
|
-
},
|
32
|
-
extractToolCall(response) {
|
33
|
-
const json = parseJSON({ text: response, schema: functionSchema });
|
34
|
-
return {
|
35
|
-
id: nanoid(),
|
36
|
-
args: json.parameters,
|
37
|
-
};
|
38
|
-
},
|
39
|
-
};
|
40
|
-
}
|
41
|
-
export function instruction({ functionCallPrompt = DEFAULT_FUNCTION_CALL_PROMPT, baseFormat, }) {
|
42
|
-
return {
|
43
|
-
createPrompt(instruction, tool) {
|
44
|
-
const instructionWithFunctionCall = [
|
45
|
-
instruction.instruction,
|
46
|
-
functionCallPrompt,
|
47
|
-
`${tool.name}:`,
|
48
|
-
` description: ${tool.description ?? ""}`,
|
49
|
-
` parameters: ${JSON.stringify(tool.parameters.getJsonSchema())}`,
|
50
|
-
``,
|
51
|
-
].join("\n");
|
52
|
-
return baseFormat.format({
|
53
|
-
...instruction,
|
54
|
-
instruction: instructionWithFunctionCall,
|
55
|
-
});
|
56
|
-
},
|
57
|
-
extractToolCall(response) {
|
58
|
-
const json = parseJSON({ text: response, schema: functionSchema });
|
59
|
-
return {
|
60
|
-
id: nanoid(),
|
61
|
-
args: json.parameters,
|
62
|
-
};
|
63
|
-
},
|
64
|
-
};
|
65
|
-
}
|
File without changes
|