modelfusion 0.79.0 → 0.81.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/README.md +58 -46
- package/core/schema/UncheckedSchema.d.ts +2 -1
- package/core/schema/ZodSchema.d.ts +2 -1
- package/core/schema/index.cjs +0 -3
- package/core/schema/index.d.ts +0 -3
- package/core/schema/index.js +0 -3
- package/guard/fixStructure.cjs +14 -8
- package/guard/fixStructure.d.ts +14 -8
- package/guard/fixStructure.js +14 -8
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +2 -3
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +8 -10
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +2 -3
- package/model-function/generate-structure/StructureFromTextPromptFormat.d.ts +6 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +69 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +22 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.js +65 -0
- package/model-function/generate-structure/StructureGenerationModel.d.ts +4 -3
- package/model-function/generate-structure/StructureParseError.cjs +2 -10
- package/model-function/generate-structure/StructureParseError.d.ts +1 -4
- package/model-function/generate-structure/StructureParseError.js +2 -10
- package/model-function/generate-structure/StructureValidationError.cjs +2 -10
- package/model-function/generate-structure/StructureValidationError.d.ts +1 -4
- package/model-function/generate-structure/StructureValidationError.js +2 -10
- package/model-function/generate-structure/generateStructure.cjs +4 -5
- package/model-function/generate-structure/generateStructure.d.ts +14 -20
- package/model-function/generate-structure/generateStructure.js +4 -5
- package/model-function/generate-structure/index.cjs +3 -0
- package/model-function/generate-structure/index.d.ts +3 -0
- package/model-function/generate-structure/index.js +3 -0
- package/model-function/generate-structure/jsonStructurePrompt.cjs +11 -0
- package/model-function/generate-structure/jsonStructurePrompt.d.ts +4 -0
- package/model-function/generate-structure/jsonStructurePrompt.js +7 -0
- package/model-function/generate-structure/streamStructure.cjs +4 -4
- package/model-function/generate-structure/streamStructure.d.ts +18 -26
- package/model-function/generate-structure/streamStructure.js +4 -4
- package/model-function/generate-text/PromptFormatTextGenerationModel.cjs +7 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.d.ts +3 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.js +7 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.cjs +7 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.d.ts +3 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.js +7 -0
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.cjs +10 -10
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.d.ts +2 -2
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.js +10 -10
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.cjs +15 -13
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.js +15 -13
- package/model-function/generate-text/prompt-format/ChatPrompt.cjs +24 -0
- package/model-function/generate-text/prompt-format/ChatPrompt.d.ts +10 -4
- package/model-function/generate-text/prompt-format/ChatPrompt.js +22 -1
- package/model-function/generate-text/prompt-format/Content.cjs +2 -0
- package/model-function/generate-text/prompt-format/Content.d.ts +20 -0
- package/model-function/generate-text/prompt-format/Content.js +1 -0
- package/model-function/generate-text/prompt-format/InstructionPrompt.d.ts +22 -17
- package/model-function/generate-text/prompt-format/InvalidPromptError.cjs +28 -0
- package/model-function/generate-text/prompt-format/InvalidPromptError.d.ts +13 -0
- package/model-function/generate-text/prompt-format/InvalidPromptError.js +24 -0
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.cjs +14 -10
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.js +14 -10
- package/model-function/generate-text/prompt-format/TextPromptFormat.cjs +11 -11
- package/model-function/generate-text/prompt-format/TextPromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/TextPromptFormat.js +11 -11
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.cjs +6 -6
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.d.ts +1 -1
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.js +6 -6
- package/model-function/generate-text/prompt-format/index.cjs +2 -2
- package/model-function/generate-text/prompt-format/index.d.ts +2 -2
- package/model-function/generate-text/prompt-format/index.js +2 -2
- package/model-function/generate-text/prompt-format/trimChatPrompt.cjs +2 -2
- package/model-function/generate-text/prompt-format/trimChatPrompt.d.ts +1 -1
- package/model-function/generate-text/prompt-format/trimChatPrompt.js +1 -1
- package/model-provider/anthropic/AnthropicPromptFormat.cjs +10 -10
- package/model-provider/anthropic/AnthropicPromptFormat.d.ts +3 -3
- package/model-provider/anthropic/AnthropicPromptFormat.js +10 -10
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppBakLLaVA1Format.cjs +20 -11
- package/model-provider/llamacpp/LlamaCppBakLLaVA1Format.js +20 -11
- package/model-provider/openai/OpenAICompletionModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.cjs +147 -0
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -0
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.js +140 -0
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +19 -14
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +2 -5
- package/model-provider/openai/chat/OpenAIChatMessage.js +19 -14
- package/model-provider/openai/chat/OpenAIChatModel.cjs +16 -56
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +10 -54
- package/model-provider/openai/chat/OpenAIChatModel.js +17 -54
- package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +19 -14
- package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +6 -2
- package/model-provider/openai/chat/OpenAIChatPromptFormat.js +17 -13
- package/package.json +1 -1
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +2 -2
- package/core/schema/StructureDefinition.d.ts +0 -7
- package/core/schema/UncheckedStructureDefinition.cjs +0 -30
- package/core/schema/UncheckedStructureDefinition.d.ts +0 -12
- package/core/schema/UncheckedStructureDefinition.js +0 -26
- package/core/schema/ZodStructureDefinition.cjs +0 -30
- package/core/schema/ZodStructureDefinition.d.ts +0 -13
- package/core/schema/ZodStructureDefinition.js +0 -26
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.cjs +0 -17
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.d.ts +0 -8
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.js +0 -13
- package/model-function/generate-text/prompt-format/validateChatPrompt.cjs +0 -24
- package/model-function/generate-text/prompt-format/validateChatPrompt.d.ts +0 -5
- package/model-function/generate-text/prompt-format/validateChatPrompt.js +0 -20
- /package/{core/schema/StructureDefinition.cjs → model-function/generate-structure/StructureFromTextPromptFormat.cjs} +0 -0
- /package/{core/schema/StructureDefinition.js → model-function/generate-structure/StructureFromTextPromptFormat.js} +0 -0
@@ -0,0 +1,140 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
2
|
+
import { StructureParseError } from "../../../model-function/generate-structure/StructureParseError.js";
|
3
|
+
import { OpenAIChatResponseFormat, } from "./OpenAIChatModel";
|
4
|
+
import { chat, instruction, text } from "./OpenAIChatPromptFormat.js";
|
5
|
+
export class OpenAIChatFunctionCallStructureGenerationModel {
|
6
|
+
constructor({ model, fnName, fnDescription, promptFormat, }) {
|
7
|
+
Object.defineProperty(this, "model", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "fnName", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "fnDescription", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
Object.defineProperty(this, "promptFormat", {
|
26
|
+
enumerable: true,
|
27
|
+
configurable: true,
|
28
|
+
writable: true,
|
29
|
+
value: void 0
|
30
|
+
});
|
31
|
+
this.model = model;
|
32
|
+
this.fnName = fnName;
|
33
|
+
this.fnDescription = fnDescription;
|
34
|
+
this.promptFormat = promptFormat;
|
35
|
+
}
|
36
|
+
get modelInformation() {
|
37
|
+
return this.model.modelInformation;
|
38
|
+
}
|
39
|
+
get settings() {
|
40
|
+
return this.model.settings;
|
41
|
+
}
|
42
|
+
get settingsForEvent() {
|
43
|
+
return this.model.settingsForEvent;
|
44
|
+
}
|
45
|
+
/**
|
46
|
+
* Returns this model with a text prompt format.
|
47
|
+
*/
|
48
|
+
withTextPrompt() {
|
49
|
+
return this.withPromptFormat(text());
|
50
|
+
}
|
51
|
+
/**
|
52
|
+
* Returns this model with an instruction prompt format.
|
53
|
+
*/
|
54
|
+
withInstructionPrompt() {
|
55
|
+
return this.withPromptFormat(instruction());
|
56
|
+
}
|
57
|
+
/**
|
58
|
+
* Returns this model with a chat prompt format.
|
59
|
+
*/
|
60
|
+
withChatPrompt() {
|
61
|
+
return this.withPromptFormat(chat());
|
62
|
+
}
|
63
|
+
withPromptFormat(promptFormat) {
|
64
|
+
return new OpenAIChatFunctionCallStructureGenerationModel({
|
65
|
+
model: this.model,
|
66
|
+
fnName: this.fnName,
|
67
|
+
fnDescription: this.fnDescription,
|
68
|
+
promptFormat,
|
69
|
+
});
|
70
|
+
}
|
71
|
+
withSettings(additionalSettings) {
|
72
|
+
return new OpenAIChatFunctionCallStructureGenerationModel({
|
73
|
+
model: this.model.withSettings(additionalSettings),
|
74
|
+
fnName: this.fnName,
|
75
|
+
fnDescription: this.fnDescription,
|
76
|
+
promptFormat: this.promptFormat,
|
77
|
+
});
|
78
|
+
}
|
79
|
+
/**
|
80
|
+
* JSON generation uses the OpenAI GPT function calling API.
|
81
|
+
* It provides a single function specification and instructs the model to provide parameters for calling the function.
|
82
|
+
* The result is returned as parsed JSON.
|
83
|
+
*
|
84
|
+
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
85
|
+
*/
|
86
|
+
async doGenerateStructure(schema, prompt, // first argument of the function
|
87
|
+
options) {
|
88
|
+
const expandedPrompt = this.promptFormat.format(prompt);
|
89
|
+
const response = await this.model
|
90
|
+
.withSettings({
|
91
|
+
stopSequences: [
|
92
|
+
...(this.settings.stopSequences ?? []),
|
93
|
+
...this.promptFormat.stopSequences,
|
94
|
+
],
|
95
|
+
})
|
96
|
+
.callAPI(expandedPrompt, {
|
97
|
+
...options,
|
98
|
+
responseFormat: OpenAIChatResponseFormat.json,
|
99
|
+
functionCall: { name: this.fnName },
|
100
|
+
functions: [
|
101
|
+
{
|
102
|
+
name: this.fnName,
|
103
|
+
description: this.fnDescription,
|
104
|
+
parameters: schema.getJsonSchema(),
|
105
|
+
},
|
106
|
+
],
|
107
|
+
});
|
108
|
+
const valueText = response.choices[0].message.function_call.arguments;
|
109
|
+
try {
|
110
|
+
return {
|
111
|
+
response,
|
112
|
+
valueText,
|
113
|
+
value: SecureJSON.parse(valueText),
|
114
|
+
usage: this.model.extractUsage(response),
|
115
|
+
};
|
116
|
+
}
|
117
|
+
catch (error) {
|
118
|
+
throw new StructureParseError({
|
119
|
+
valueText,
|
120
|
+
cause: error,
|
121
|
+
});
|
122
|
+
}
|
123
|
+
}
|
124
|
+
async doStreamStructure(schema, prompt, // first argument of the function
|
125
|
+
options) {
|
126
|
+
const expandedPrompt = this.promptFormat.format(prompt);
|
127
|
+
return this.model.callAPI(expandedPrompt, {
|
128
|
+
...options,
|
129
|
+
responseFormat: OpenAIChatResponseFormat.structureDeltaIterable,
|
130
|
+
functionCall: { name: this.fnName },
|
131
|
+
functions: [
|
132
|
+
{
|
133
|
+
name: this.fnName,
|
134
|
+
description: this.fnDescription,
|
135
|
+
parameters: schema.getJsonSchema(),
|
136
|
+
},
|
137
|
+
],
|
138
|
+
});
|
139
|
+
}
|
140
|
+
}
|
@@ -6,20 +6,25 @@ exports.OpenAIChatMessage = {
|
|
6
6
|
return { role: "system", content };
|
7
7
|
},
|
8
8
|
user(content, options) {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
content
|
13
|
-
|
14
|
-
{
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
9
|
+
return {
|
10
|
+
role: "user",
|
11
|
+
content: typeof content === "string"
|
12
|
+
? content
|
13
|
+
: content.map((element) => {
|
14
|
+
switch (element.type) {
|
15
|
+
case "text": {
|
16
|
+
return { type: "text", text: element.text };
|
17
|
+
}
|
18
|
+
case "image": {
|
19
|
+
return {
|
20
|
+
type: "image_url",
|
21
|
+
image_url: `data:${element.mimeType ?? "image/jpeg"};base64,${element.base64Image}`,
|
22
|
+
};
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}),
|
26
|
+
name: options?.name,
|
27
|
+
};
|
23
28
|
},
|
24
29
|
/**
|
25
30
|
* Creates an assistant chat message. The assistant message can optionally contain tool calls.
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { MultiModalInput } from "../../../model-function/generate-text/prompt-format/Content.js";
|
1
2
|
import { ToolCall } from "../../../tool/ToolCall.js";
|
2
3
|
export type OpenAIChatMessage = {
|
3
4
|
role: "system";
|
@@ -43,12 +44,8 @@ export type OpenAIChatMessage = {
|
|
43
44
|
};
|
44
45
|
export declare const OpenAIChatMessage: {
|
45
46
|
system(content: string): OpenAIChatMessage;
|
46
|
-
user(content: string, options?: {
|
47
|
+
user(content: string | MultiModalInput, options?: {
|
47
48
|
name?: string;
|
48
|
-
image?: {
|
49
|
-
base64Content: string;
|
50
|
-
mimeType?: string;
|
51
|
-
};
|
52
49
|
}): OpenAIChatMessage;
|
53
50
|
/**
|
54
51
|
* Creates an assistant chat message. The assistant message can optionally contain tool calls.
|
@@ -3,20 +3,25 @@ export const OpenAIChatMessage = {
|
|
3
3
|
return { role: "system", content };
|
4
4
|
},
|
5
5
|
user(content, options) {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
content
|
10
|
-
|
11
|
-
{
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
6
|
+
return {
|
7
|
+
role: "user",
|
8
|
+
content: typeof content === "string"
|
9
|
+
? content
|
10
|
+
: content.map((element) => {
|
11
|
+
switch (element.type) {
|
12
|
+
case "text": {
|
13
|
+
return { type: "text", text: element.text };
|
14
|
+
}
|
15
|
+
case "image": {
|
16
|
+
return {
|
17
|
+
type: "image_url",
|
18
|
+
image_url: `data:${element.mimeType ?? "image/jpeg"};base64,${element.base64Image}`,
|
19
|
+
};
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}),
|
23
|
+
name: options?.name,
|
24
|
+
};
|
20
25
|
},
|
21
26
|
/**
|
22
27
|
* Creates an assistant chat message. The assistant message can optionally contain tool calls.
|
@@ -1,21 +1,18 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.OpenAIChatResponseFormat = exports.OpenAIChatModel = exports.calculateOpenAIChatCostInMillicents = exports.isOpenAIChatModel = exports.getOpenAIChatModelInformation = exports.OPENAI_CHAT_MODELS = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
4
|
const zod_1 = require("zod");
|
9
5
|
const callWithRetryAndThrottle_js_1 = require("../../../core/api/callWithRetryAndThrottle.cjs");
|
10
6
|
const postToApi_js_1 = require("../../../core/api/postToApi.cjs");
|
11
7
|
const parseJSON_js_1 = require("../../../core/schema/parseJSON.cjs");
|
12
8
|
const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
|
13
|
-
const
|
9
|
+
const StructureFromTextStreamingModel_js_1 = require("../../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
|
14
10
|
const parsePartialJson_js_1 = require("../../../model-function/generate-structure/parsePartialJson.cjs");
|
15
11
|
const PromptFormatTextStreamingModel_js_1 = require("../../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
16
12
|
const OpenAIApiConfiguration_js_1 = require("../OpenAIApiConfiguration.cjs");
|
17
13
|
const OpenAIError_js_1 = require("../OpenAIError.cjs");
|
18
14
|
const TikTokenTokenizer_js_1 = require("../TikTokenTokenizer.cjs");
|
15
|
+
const OpenAIChatFunctionCallStructureGenerationModel_js_1 = require("./OpenAIChatFunctionCallStructureGenerationModel.cjs");
|
19
16
|
const OpenAIChatPromptFormat_js_1 = require("./OpenAIChatPromptFormat.cjs");
|
20
17
|
const OpenAIChatStreamIterable_js_1 = require("./OpenAIChatStreamIterable.cjs");
|
21
18
|
const countOpenAIChatMessageTokens_js_1 = require("./countOpenAIChatMessageTokens.cjs");
|
@@ -264,57 +261,6 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
264
261
|
responseFormat: exports.OpenAIChatResponseFormat.textDeltaIterable,
|
265
262
|
});
|
266
263
|
}
|
267
|
-
/**
|
268
|
-
* JSON generation uses the OpenAI GPT function calling API.
|
269
|
-
* It provides a single function specification and instructs the model to provide parameters for calling the function.
|
270
|
-
* The result is returned as parsed JSON.
|
271
|
-
*
|
272
|
-
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
273
|
-
*/
|
274
|
-
async doGenerateStructure(structureDefinition, prompt, options) {
|
275
|
-
const response = await this.callAPI(prompt, {
|
276
|
-
...options,
|
277
|
-
responseFormat: exports.OpenAIChatResponseFormat.json,
|
278
|
-
functionCall: { name: structureDefinition.name },
|
279
|
-
functions: [
|
280
|
-
{
|
281
|
-
name: structureDefinition.name,
|
282
|
-
description: structureDefinition.description,
|
283
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
284
|
-
},
|
285
|
-
],
|
286
|
-
});
|
287
|
-
const valueText = response.choices[0].message.function_call.arguments;
|
288
|
-
try {
|
289
|
-
return {
|
290
|
-
response,
|
291
|
-
valueText,
|
292
|
-
value: secure_json_parse_1.default.parse(valueText),
|
293
|
-
usage: this.extractUsage(response),
|
294
|
-
};
|
295
|
-
}
|
296
|
-
catch (error) {
|
297
|
-
throw new StructureParseError_js_1.StructureParseError({
|
298
|
-
structureName: structureDefinition.name,
|
299
|
-
valueText,
|
300
|
-
cause: error,
|
301
|
-
});
|
302
|
-
}
|
303
|
-
}
|
304
|
-
async doStreamStructure(structureDefinition, prompt, options) {
|
305
|
-
return this.callAPI(prompt, {
|
306
|
-
...options,
|
307
|
-
responseFormat: exports.OpenAIChatResponseFormat.structureDeltaIterable,
|
308
|
-
functionCall: { name: structureDefinition.name },
|
309
|
-
functions: [
|
310
|
-
{
|
311
|
-
name: structureDefinition.name,
|
312
|
-
description: structureDefinition.description,
|
313
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
314
|
-
},
|
315
|
-
],
|
316
|
-
});
|
317
|
-
}
|
318
264
|
async doGenerateToolCall(tool, prompt, options) {
|
319
265
|
const response = await this.callAPI(prompt, {
|
320
266
|
...options,
|
@@ -379,6 +325,20 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
379
325
|
totalTokens: response.usage.total_tokens,
|
380
326
|
};
|
381
327
|
}
|
328
|
+
asFunctionCallStructureGenerationModel({ fnName, fnDescription, }) {
|
329
|
+
return new OpenAIChatFunctionCallStructureGenerationModel_js_1.OpenAIChatFunctionCallStructureGenerationModel({
|
330
|
+
model: this,
|
331
|
+
fnName,
|
332
|
+
fnDescription,
|
333
|
+
promptFormat: (0, OpenAIChatPromptFormat_js_1.identity)(),
|
334
|
+
});
|
335
|
+
}
|
336
|
+
asStructureGenerationModel(promptFormat) {
|
337
|
+
return new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
|
338
|
+
model: this,
|
339
|
+
format: promptFormat,
|
340
|
+
});
|
341
|
+
}
|
382
342
|
/**
|
383
343
|
* Returns this model with a text prompt format.
|
384
344
|
*/
|
@@ -2,10 +2,10 @@ import { z } from "zod";
|
|
2
2
|
import { FunctionOptions } from "../../../core/FunctionOptions.js";
|
3
3
|
import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
|
4
4
|
import { ResponseHandler } from "../../../core/api/postToApi.js";
|
5
|
-
import { StructureDefinition } from "../../../core/schema/StructureDefinition.js";
|
6
5
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
7
6
|
import { Delta } from "../../../model-function/Delta.js";
|
8
|
-
import {
|
7
|
+
import { StructureFromTextPromptFormat } from "../../../model-function/generate-structure/StructureFromTextPromptFormat.js";
|
8
|
+
import { StructureFromTextStreamingModel } from "../../../model-function/generate-structure/StructureFromTextStreamingModel.js";
|
9
9
|
import { PromptFormatTextStreamingModel } from "../../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
10
10
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../../model-function/generate-text/TextGenerationModel.js";
|
11
11
|
import { TextGenerationPromptFormat } from "../../../model-function/generate-text/TextGenerationPromptFormat.js";
|
@@ -13,6 +13,7 @@ import { ToolDefinition } from "../../../tool/ToolDefinition.js";
|
|
13
13
|
import { ToolCallGenerationModel } from "../../../tool/generate-tool-call/ToolCallGenerationModel.js";
|
14
14
|
import { ToolCallsOrTextGenerationModel } from "../../../tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js";
|
15
15
|
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
16
|
+
import { OpenAIChatFunctionCallStructureGenerationModel } from "./OpenAIChatFunctionCallStructureGenerationModel.js";
|
16
17
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
17
18
|
export declare const OPENAI_CHAT_MODELS: {
|
18
19
|
"gpt-4": {
|
@@ -168,7 +169,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
168
169
|
* ),
|
169
170
|
* ]);
|
170
171
|
*/
|
171
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>,
|
172
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>, ToolCallGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, ToolCallsOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatSettings> {
|
172
173
|
constructor(settings: OpenAIChatSettings);
|
173
174
|
readonly provider: "openai";
|
174
175
|
get modelName(): OpenAIChatModelType;
|
@@ -230,56 +231,6 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
230
231
|
};
|
231
232
|
}>;
|
232
233
|
doStreamText(prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
233
|
-
/**
|
234
|
-
* JSON generation uses the OpenAI GPT function calling API.
|
235
|
-
* It provides a single function specification and instructs the model to provide parameters for calling the function.
|
236
|
-
* The result is returned as parsed JSON.
|
237
|
-
*
|
238
|
-
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
239
|
-
*/
|
240
|
-
doGenerateStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
241
|
-
response: {
|
242
|
-
object: "chat.completion";
|
243
|
-
usage: {
|
244
|
-
prompt_tokens: number;
|
245
|
-
total_tokens: number;
|
246
|
-
completion_tokens: number;
|
247
|
-
};
|
248
|
-
model: string;
|
249
|
-
id: string;
|
250
|
-
choices: {
|
251
|
-
message: {
|
252
|
-
role: "assistant";
|
253
|
-
content: string | null;
|
254
|
-
function_call?: {
|
255
|
-
name: string;
|
256
|
-
arguments: string;
|
257
|
-
} | undefined;
|
258
|
-
tool_calls?: {
|
259
|
-
function: {
|
260
|
-
name: string;
|
261
|
-
arguments: string;
|
262
|
-
};
|
263
|
-
type: "function";
|
264
|
-
id: string;
|
265
|
-
}[] | undefined;
|
266
|
-
};
|
267
|
-
index: number;
|
268
|
-
logprobs?: any;
|
269
|
-
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
270
|
-
}[];
|
271
|
-
created: number;
|
272
|
-
system_fingerprint?: string | undefined;
|
273
|
-
};
|
274
|
-
valueText: string;
|
275
|
-
value: any;
|
276
|
-
usage: {
|
277
|
-
promptTokens: number;
|
278
|
-
completionTokens: number;
|
279
|
-
totalTokens: number;
|
280
|
-
};
|
281
|
-
}>;
|
282
|
-
doStreamStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<AsyncIterable<Delta<unknown>>>;
|
283
234
|
doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
284
235
|
response: {
|
285
236
|
object: "chat.completion";
|
@@ -375,6 +326,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
375
326
|
completionTokens: number;
|
376
327
|
totalTokens: number;
|
377
328
|
};
|
329
|
+
asFunctionCallStructureGenerationModel({ fnName, fnDescription, }: {
|
330
|
+
fnName: string;
|
331
|
+
fnDescription?: string;
|
332
|
+
}): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptFormat<OpenAIChatMessage[], OpenAIChatMessage[]>>;
|
333
|
+
asStructureGenerationModel<INPUT_PROMPT>(promptFormat: StructureFromTextPromptFormat<INPUT_PROMPT, OpenAIChatMessage[]>): StructureFromTextStreamingModel<INPUT_PROMPT, OpenAIChatMessage[], this>;
|
378
334
|
/**
|
379
335
|
* Returns this model with a text prompt format.
|
380
336
|
*/
|
@@ -382,7 +338,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
382
338
|
/**
|
383
339
|
* Returns this model with an instruction prompt format.
|
384
340
|
*/
|
385
|
-
withInstructionPrompt(): PromptFormatTextStreamingModel<import("../../../index.js").InstructionPrompt, OpenAIChatMessage[], OpenAIChatSettings, this>;
|
341
|
+
withInstructionPrompt(): PromptFormatTextStreamingModel<import("../../../index.js").InstructionPrompt | import("../../../index.js").TextInstructionPrompt, OpenAIChatMessage[], OpenAIChatSettings, this>;
|
386
342
|
/**
|
387
343
|
* Returns this model with a chat prompt format.
|
388
344
|
*/
|
@@ -1,16 +1,16 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
1
|
import { z } from "zod";
|
3
2
|
import { callWithRetryAndThrottle } from "../../../core/api/callWithRetryAndThrottle.js";
|
4
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../../core/api/postToApi.js";
|
5
4
|
import { parseJSON } from "../../../core/schema/parseJSON.js";
|
6
5
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
7
|
-
import {
|
6
|
+
import { StructureFromTextStreamingModel } from "../../../model-function/generate-structure/StructureFromTextStreamingModel.js";
|
8
7
|
import { parsePartialJson } from "../../../model-function/generate-structure/parsePartialJson.js";
|
9
8
|
import { PromptFormatTextStreamingModel } from "../../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
10
9
|
import { OpenAIApiConfiguration } from "../OpenAIApiConfiguration.js";
|
11
10
|
import { failedOpenAICallResponseHandler } from "../OpenAIError.js";
|
12
11
|
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
13
|
-
import {
|
12
|
+
import { OpenAIChatFunctionCallStructureGenerationModel } from "./OpenAIChatFunctionCallStructureGenerationModel.js";
|
13
|
+
import { chat, identity, instruction, text } from "./OpenAIChatPromptFormat.js";
|
14
14
|
import { createOpenAIChatDeltaIterableQueue } from "./OpenAIChatStreamIterable.js";
|
15
15
|
import { countOpenAIChatPromptTokens } from "./countOpenAIChatMessageTokens.js";
|
16
16
|
/*
|
@@ -255,57 +255,6 @@ export class OpenAIChatModel extends AbstractModel {
|
|
255
255
|
responseFormat: OpenAIChatResponseFormat.textDeltaIterable,
|
256
256
|
});
|
257
257
|
}
|
258
|
-
/**
|
259
|
-
* JSON generation uses the OpenAI GPT function calling API.
|
260
|
-
* It provides a single function specification and instructs the model to provide parameters for calling the function.
|
261
|
-
* The result is returned as parsed JSON.
|
262
|
-
*
|
263
|
-
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
264
|
-
*/
|
265
|
-
async doGenerateStructure(structureDefinition, prompt, options) {
|
266
|
-
const response = await this.callAPI(prompt, {
|
267
|
-
...options,
|
268
|
-
responseFormat: OpenAIChatResponseFormat.json,
|
269
|
-
functionCall: { name: structureDefinition.name },
|
270
|
-
functions: [
|
271
|
-
{
|
272
|
-
name: structureDefinition.name,
|
273
|
-
description: structureDefinition.description,
|
274
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
275
|
-
},
|
276
|
-
],
|
277
|
-
});
|
278
|
-
const valueText = response.choices[0].message.function_call.arguments;
|
279
|
-
try {
|
280
|
-
return {
|
281
|
-
response,
|
282
|
-
valueText,
|
283
|
-
value: SecureJSON.parse(valueText),
|
284
|
-
usage: this.extractUsage(response),
|
285
|
-
};
|
286
|
-
}
|
287
|
-
catch (error) {
|
288
|
-
throw new StructureParseError({
|
289
|
-
structureName: structureDefinition.name,
|
290
|
-
valueText,
|
291
|
-
cause: error,
|
292
|
-
});
|
293
|
-
}
|
294
|
-
}
|
295
|
-
async doStreamStructure(structureDefinition, prompt, options) {
|
296
|
-
return this.callAPI(prompt, {
|
297
|
-
...options,
|
298
|
-
responseFormat: OpenAIChatResponseFormat.structureDeltaIterable,
|
299
|
-
functionCall: { name: structureDefinition.name },
|
300
|
-
functions: [
|
301
|
-
{
|
302
|
-
name: structureDefinition.name,
|
303
|
-
description: structureDefinition.description,
|
304
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
305
|
-
},
|
306
|
-
],
|
307
|
-
});
|
308
|
-
}
|
309
258
|
async doGenerateToolCall(tool, prompt, options) {
|
310
259
|
const response = await this.callAPI(prompt, {
|
311
260
|
...options,
|
@@ -370,6 +319,20 @@ export class OpenAIChatModel extends AbstractModel {
|
|
370
319
|
totalTokens: response.usage.total_tokens,
|
371
320
|
};
|
372
321
|
}
|
322
|
+
asFunctionCallStructureGenerationModel({ fnName, fnDescription, }) {
|
323
|
+
return new OpenAIChatFunctionCallStructureGenerationModel({
|
324
|
+
model: this,
|
325
|
+
fnName,
|
326
|
+
fnDescription,
|
327
|
+
promptFormat: identity(),
|
328
|
+
});
|
329
|
+
}
|
330
|
+
asStructureGenerationModel(promptFormat) {
|
331
|
+
return new StructureFromTextStreamingModel({
|
332
|
+
model: this,
|
333
|
+
format: promptFormat,
|
334
|
+
});
|
335
|
+
}
|
373
336
|
/**
|
374
337
|
* Returns this model with a text prompt format.
|
375
338
|
*/
|
@@ -1,14 +1,21 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.chat = exports.instruction = exports.text = void 0;
|
4
|
-
const
|
3
|
+
exports.chat = exports.instruction = exports.text = exports.identity = void 0;
|
4
|
+
const ChatPrompt_js_1 = require("../../../model-function/generate-text/prompt-format/ChatPrompt.cjs");
|
5
5
|
const OpenAIChatMessage_js_1 = require("./OpenAIChatMessage.cjs");
|
6
|
+
/**
|
7
|
+
* OpenAIMessage[] identity chat format.
|
8
|
+
*/
|
9
|
+
function identity() {
|
10
|
+
return { format: (prompt) => prompt, stopSequences: [] };
|
11
|
+
}
|
12
|
+
exports.identity = identity;
|
6
13
|
/**
|
7
14
|
* Formats a text prompt as an OpenAI chat prompt.
|
8
15
|
*/
|
9
16
|
function text() {
|
10
17
|
return {
|
11
|
-
format: (
|
18
|
+
format: (prompt) => [OpenAIChatMessage_js_1.OpenAIChatMessage.user(prompt)],
|
12
19
|
stopSequences: [],
|
13
20
|
};
|
14
21
|
}
|
@@ -18,14 +25,12 @@ exports.text = text;
|
|
18
25
|
*/
|
19
26
|
function instruction() {
|
20
27
|
return {
|
21
|
-
format
|
28
|
+
format(prompt) {
|
22
29
|
const messages = [];
|
23
|
-
if (
|
24
|
-
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.system(
|
30
|
+
if (prompt.system != null) {
|
31
|
+
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.system(prompt.system));
|
25
32
|
}
|
26
|
-
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.user(
|
27
|
-
image: instruction.image,
|
28
|
-
}));
|
33
|
+
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.user(prompt.instruction));
|
29
34
|
return messages;
|
30
35
|
},
|
31
36
|
stopSequences: [],
|
@@ -37,13 +42,13 @@ exports.instruction = instruction;
|
|
37
42
|
*/
|
38
43
|
function chat() {
|
39
44
|
return {
|
40
|
-
format
|
41
|
-
(0,
|
45
|
+
format(prompt) {
|
46
|
+
(0, ChatPrompt_js_1.validateChatPrompt)(prompt);
|
42
47
|
const messages = [];
|
43
|
-
if (
|
44
|
-
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.system(
|
48
|
+
if (prompt.system != null) {
|
49
|
+
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.system(prompt.system));
|
45
50
|
}
|
46
|
-
for (const { role, content } of
|
51
|
+
for (const { role, content } of prompt.messages) {
|
47
52
|
switch (role) {
|
48
53
|
case "user": {
|
49
54
|
messages.push(OpenAIChatMessage_js_1.OpenAIChatMessage.user(content));
|
@@ -1,7 +1,11 @@
|
|
1
1
|
import { TextGenerationPromptFormat } from "../../../model-function/generate-text/TextGenerationPromptFormat.js";
|
2
2
|
import { ChatPrompt } from "../../../model-function/generate-text/prompt-format/ChatPrompt.js";
|
3
|
-
import { InstructionPrompt } from "../../../model-function/generate-text/prompt-format/InstructionPrompt.js";
|
3
|
+
import { InstructionPrompt, TextInstructionPrompt } from "../../../model-function/generate-text/prompt-format/InstructionPrompt.js";
|
4
4
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
5
|
+
/**
|
6
|
+
* OpenAIMessage[] identity chat format.
|
7
|
+
*/
|
8
|
+
export declare function identity(): TextGenerationPromptFormat<Array<OpenAIChatMessage>, Array<OpenAIChatMessage>>;
|
5
9
|
/**
|
6
10
|
* Formats a text prompt as an OpenAI chat prompt.
|
7
11
|
*/
|
@@ -9,7 +13,7 @@ export declare function text(): TextGenerationPromptFormat<string, Array<OpenAIC
|
|
9
13
|
/**
|
10
14
|
* Formats an instruction prompt as an OpenAI chat prompt.
|
11
15
|
*/
|
12
|
-
export declare function instruction(): TextGenerationPromptFormat<InstructionPrompt, Array<OpenAIChatMessage>>;
|
16
|
+
export declare function instruction(): TextGenerationPromptFormat<InstructionPrompt | TextInstructionPrompt, Array<OpenAIChatMessage>>;
|
13
17
|
/**
|
14
18
|
* Formats a chat prompt as an OpenAI chat prompt.
|
15
19
|
*/
|