modelfusion 0.11.0 → 0.12.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 +7 -1
- package/model-function/generate-json/GenerateJsonModel.d.ts +2 -4
- package/model-function/generate-json/InstructionWithSchemaPrompt.cjs +17 -0
- package/model-function/generate-json/InstructionWithSchemaPrompt.d.ts +26 -0
- package/model-function/generate-json/InstructionWithSchemaPrompt.js +14 -0
- package/model-function/generate-json/JsonTextGenerationModel.cjs +41 -0
- package/model-function/generate-json/JsonTextGenerationModel.d.ts +25 -0
- package/model-function/generate-json/JsonTextGenerationModel.js +37 -0
- package/model-function/generate-json/generateJson.cjs +1 -1
- package/model-function/generate-json/generateJson.d.ts +3 -3
- package/model-function/generate-json/generateJson.js +1 -1
- package/model-function/index.cjs +4 -3
- package/model-function/index.d.ts +4 -3
- package/model-function/index.js +4 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +5 -0
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +3 -1
- package/model-provider/openai/chat/OpenAIChatModel.js +5 -0
- package/model-provider/openai/chat/OpenAIChatPrompt.cjs +0 -4
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +3 -5
- package/model-provider/openai/chat/OpenAIChatPrompt.js +0 -4
- package/package.json +2 -2
- package/tool/useTool.d.ts +2 -2
package/README.md
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
[](https://discord.gg/GqCwYZATem)
|
9
9
|
[](https://twitter.com/lgrammel)
|
10
10
|
|
11
|
-
[Introduction](#introduction) | [Quick Install](#quick-install) | [Usage](#usage-examples) | [Features](#features) | [Integrations](#integrations) | [Documentation](#documentation) | [Examples](#more-examples) | [modelfusion.dev](https://modelfusion.dev)
|
11
|
+
[Introduction](#introduction) | [Quick Install](#quick-install) | [Usage](#usage-examples) | [Features](#features) | [Integrations](#integrations) | [Documentation](#documentation) | [Examples](#more-examples) | [Contributing](#contributing) | [modelfusion.dev](https://modelfusion.dev)
|
12
12
|
|
13
13
|
> [!NOTE]
|
14
14
|
> ModelFusion is in its initial development phase. Until version 1.0 there may be breaking changes, because I am still exploring the API design. Feedback and suggestions are welcome.
|
@@ -468,3 +468,9 @@ Small agent that solves middle school math problems. It uses a calculator tool t
|
|
468
468
|
> _terminal app_, _PDF parsing_, _recursive information extraction_, _in memory vector index, \_style example retrieval_, _OpenAI GPT-4_, _cost calculation_
|
469
469
|
|
470
470
|
Extracts information about a topic from a PDF and writes a tweet in your own style about it.
|
471
|
+
|
472
|
+
## Contributing
|
473
|
+
|
474
|
+
### [Contributing Guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md)
|
475
|
+
|
476
|
+
Read the [ModelFusion contributing guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md) to learn about the development process, how to propose bugfixes and improvements, and how to build and test your changes.
|
@@ -2,9 +2,7 @@ import { FunctionOptions } from "../FunctionOptions.js";
|
|
2
2
|
import { Model, ModelSettings } from "../Model.js";
|
3
3
|
export interface GenerateJsonModelSettings extends ModelSettings {
|
4
4
|
}
|
5
|
-
export interface GenerateJsonPrompt<RESPONSE> {
|
6
|
-
extractJson(response: RESPONSE): unknown;
|
7
|
-
}
|
8
5
|
export interface GenerateJsonModel<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings> extends Model<SETTINGS> {
|
9
|
-
generateJsonResponse(prompt: PROMPT
|
6
|
+
generateJsonResponse(prompt: PROMPT, options?: FunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
7
|
+
extractJson(response: RESPONSE): unknown;
|
10
8
|
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.InstructionWithSchemaPrompt = void 0;
|
4
|
+
exports.InstructionWithSchemaPrompt = {
|
5
|
+
forSchema({ instruction, schemaDefinition, }) {
|
6
|
+
return { schemaDefinition, instruction };
|
7
|
+
},
|
8
|
+
forTool({ instruction, tool, }) {
|
9
|
+
return exports.InstructionWithSchemaPrompt.forSchema({
|
10
|
+
instruction,
|
11
|
+
schemaDefinition: tool.inputSchemaDefinition,
|
12
|
+
});
|
13
|
+
},
|
14
|
+
forToolCurried(instruction) {
|
15
|
+
return (tool) => this.forTool({ instruction, tool });
|
16
|
+
},
|
17
|
+
};
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { Tool } from "../../tool/Tool.js";
|
2
|
+
import { SchemaDefinition } from "./SchemaDefinition.js";
|
3
|
+
export type InstructionWithSchema<NAME extends string, STRUCTURE> = {
|
4
|
+
instruction: string;
|
5
|
+
schemaDefinition: SchemaDefinition<NAME, STRUCTURE>;
|
6
|
+
};
|
7
|
+
export declare const InstructionWithSchemaPrompt: {
|
8
|
+
forSchema<STRUCTURE>({ instruction, schemaDefinition, }: {
|
9
|
+
instruction: string;
|
10
|
+
schemaDefinition: SchemaDefinition<string, STRUCTURE>;
|
11
|
+
}): {
|
12
|
+
schemaDefinition: SchemaDefinition<string, STRUCTURE>;
|
13
|
+
instruction: string;
|
14
|
+
};
|
15
|
+
forTool<INPUT, OUTPUT>({ instruction, tool, }: {
|
16
|
+
instruction: string;
|
17
|
+
tool: Tool<string, INPUT, OUTPUT>;
|
18
|
+
}): {
|
19
|
+
schemaDefinition: SchemaDefinition<string, INPUT>;
|
20
|
+
instruction: string;
|
21
|
+
};
|
22
|
+
forToolCurried<INPUT_1, OUTPUT_1>(instruction: string): (tool: Tool<string, INPUT_1, OUTPUT_1>) => {
|
23
|
+
schemaDefinition: SchemaDefinition<string, INPUT_1>;
|
24
|
+
instruction: string;
|
25
|
+
};
|
26
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export const InstructionWithSchemaPrompt = {
|
2
|
+
forSchema({ instruction, schemaDefinition, }) {
|
3
|
+
return { schemaDefinition, instruction };
|
4
|
+
},
|
5
|
+
forTool({ instruction, tool, }) {
|
6
|
+
return InstructionWithSchemaPrompt.forSchema({
|
7
|
+
instruction,
|
8
|
+
schemaDefinition: tool.inputSchemaDefinition,
|
9
|
+
});
|
10
|
+
},
|
11
|
+
forToolCurried(instruction) {
|
12
|
+
return (tool) => this.forTool({ instruction, tool });
|
13
|
+
},
|
14
|
+
};
|
@@ -0,0 +1,41 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.JsonTextGenerationModel = void 0;
|
4
|
+
const generateText_js_1 = require("../generate-text/generateText.cjs");
|
5
|
+
class JsonTextGenerationModel {
|
6
|
+
constructor({ model, format, }) {
|
7
|
+
Object.defineProperty(this, "model", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "format", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
this.model = model;
|
20
|
+
this.format = format;
|
21
|
+
}
|
22
|
+
get modelInformation() {
|
23
|
+
return this.model.modelInformation;
|
24
|
+
}
|
25
|
+
get settings() {
|
26
|
+
return this.model.settings;
|
27
|
+
}
|
28
|
+
async generateJsonResponse(prompt, options) {
|
29
|
+
return await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt), options);
|
30
|
+
}
|
31
|
+
extractJson(response) {
|
32
|
+
return this.format.extractJson(response);
|
33
|
+
}
|
34
|
+
withSettings(additionalSettings) {
|
35
|
+
return new JsonTextGenerationModel({
|
36
|
+
model: this.model.withSettings(additionalSettings),
|
37
|
+
format: this.format,
|
38
|
+
});
|
39
|
+
}
|
40
|
+
}
|
41
|
+
exports.JsonTextGenerationModel = JsonTextGenerationModel;
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
|
2
|
+
import { SchemaDefinition } from "./SchemaDefinition.js";
|
3
|
+
import { InstructionWithSchema } from "./InstructionWithSchemaPrompt.js";
|
4
|
+
import { GenerateJsonModel } from "./GenerateJsonModel.js";
|
5
|
+
import { FunctionOptions } from "../FunctionOptions.js";
|
6
|
+
export type JsonTextPromptFormat = {
|
7
|
+
createPrompt: (prompt: {
|
8
|
+
instruction: string;
|
9
|
+
schemaDefinition: SchemaDefinition<string, unknown>;
|
10
|
+
}) => string;
|
11
|
+
extractJson: (response: string) => unknown;
|
12
|
+
};
|
13
|
+
export declare class JsonTextGenerationModel<SETTINGS extends TextGenerationModelSettings, MODEL extends TextGenerationModel<string, any, any, SETTINGS>> implements GenerateJsonModel<InstructionWithSchema<string, unknown>, string, SETTINGS> {
|
14
|
+
private readonly model;
|
15
|
+
private readonly format;
|
16
|
+
constructor({ model, format, }: {
|
17
|
+
model: MODEL;
|
18
|
+
format: JsonTextPromptFormat;
|
19
|
+
});
|
20
|
+
get modelInformation(): import("../ModelInformation.js").ModelInformation;
|
21
|
+
get settings(): SETTINGS;
|
22
|
+
generateJsonResponse(prompt: InstructionWithSchema<string, unknown>, options?: FunctionOptions<SETTINGS> | undefined): Promise<string>;
|
23
|
+
extractJson(response: string): unknown;
|
24
|
+
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
25
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { generateText } from "../generate-text/generateText.js";
|
2
|
+
export class JsonTextGenerationModel {
|
3
|
+
constructor({ model, format, }) {
|
4
|
+
Object.defineProperty(this, "model", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "format", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
this.model = model;
|
17
|
+
this.format = format;
|
18
|
+
}
|
19
|
+
get modelInformation() {
|
20
|
+
return this.model.modelInformation;
|
21
|
+
}
|
22
|
+
get settings() {
|
23
|
+
return this.model.settings;
|
24
|
+
}
|
25
|
+
async generateJsonResponse(prompt, options) {
|
26
|
+
return await generateText(this.model, this.format.createPrompt(prompt), options);
|
27
|
+
}
|
28
|
+
extractJson(response) {
|
29
|
+
return this.format.extractJson(response);
|
30
|
+
}
|
31
|
+
withSettings(additionalSettings) {
|
32
|
+
return new JsonTextGenerationModel({
|
33
|
+
model: this.model.withSettings(additionalSettings),
|
34
|
+
format: this.format,
|
35
|
+
});
|
36
|
+
}
|
37
|
+
}
|
@@ -10,7 +10,7 @@ async function generateJson(model, schemaDefinition, prompt, options) {
|
|
10
10
|
options,
|
11
11
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
12
12
|
extractOutputValue: (response) => {
|
13
|
-
const json =
|
13
|
+
const json = model.extractJson(response);
|
14
14
|
const parseResult = schemaDefinition.schema.safeParse(json);
|
15
15
|
if (!parseResult.success) {
|
16
16
|
throw new SchemaValidationError_js_1.SchemaValidationError({
|
@@ -1,14 +1,14 @@
|
|
1
1
|
import { FunctionOptions } from "../FunctionOptions.js";
|
2
2
|
import { CallMetadata } from "../executeCall.js";
|
3
|
-
import { GenerateJsonModel, GenerateJsonModelSettings
|
3
|
+
import { GenerateJsonModel, GenerateJsonModelSettings } from "./GenerateJsonModel.js";
|
4
4
|
import { SchemaDefinition } from "./SchemaDefinition.js";
|
5
|
-
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT
|
5
|
+
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options: FunctionOptions<SETTINGS> & {
|
6
6
|
fullResponse: true;
|
7
7
|
}): Promise<{
|
8
8
|
value: STRUCTURE;
|
9
9
|
response: RESPONSE;
|
10
10
|
metadata: CallMetadata<GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>>;
|
11
11
|
}>;
|
12
|
-
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT
|
12
|
+
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: FunctionOptions<SETTINGS> & {
|
13
13
|
fullResponse?: false;
|
14
14
|
}): Promise<STRUCTURE>;
|
@@ -7,7 +7,7 @@ export async function generateJson(model, schemaDefinition, prompt, options) {
|
|
7
7
|
options,
|
8
8
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
9
9
|
extractOutputValue: (response) => {
|
10
|
-
const json =
|
10
|
+
const json = model.extractJson(response);
|
11
11
|
const parseResult = schemaDefinition.schema.safeParse(json);
|
12
12
|
if (!parseResult.success) {
|
13
13
|
throw new SchemaValidationError({
|
package/model-function/index.cjs
CHANGED
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./FunctionOptions.cjs"), exports);
|
18
18
|
__exportStar(require("./Model.cjs"), exports);
|
19
19
|
__exportStar(require("./ModelCallEvent.cjs"), exports);
|
20
|
-
__exportStar(require("../run/RunFunctionObserver.cjs"), exports);
|
21
20
|
__exportStar(require("./ModelInformation.cjs"), exports);
|
22
21
|
__exportStar(require("./SuccessfulModelCall.cjs"), exports);
|
23
22
|
__exportStar(require("./embed-text/TextEmbeddingEvent.cjs"), exports);
|
@@ -28,17 +27,19 @@ __exportStar(require("./generate-image/ImageGenerationModel.cjs"), exports);
|
|
28
27
|
__exportStar(require("./generate-image/generateImage.cjs"), exports);
|
29
28
|
__exportStar(require("./generate-json/GenerateJsonModel.cjs"), exports);
|
30
29
|
__exportStar(require("./generate-json/GenerateJsonOrTextModel.cjs"), exports);
|
30
|
+
__exportStar(require("./generate-json/InstructionWithSchemaPrompt.cjs"), exports);
|
31
31
|
__exportStar(require("./generate-json/JsonGenerationEvent.cjs"), exports);
|
32
|
+
__exportStar(require("./generate-json/JsonTextGenerationModel.cjs"), exports);
|
32
33
|
__exportStar(require("./generate-json/NoSuchSchemaError.cjs"), exports);
|
33
34
|
__exportStar(require("./generate-json/SchemaDefinition.cjs"), exports);
|
34
35
|
__exportStar(require("./generate-json/SchemaValidationError.cjs"), exports);
|
35
36
|
__exportStar(require("./generate-json/generateJson.cjs"), exports);
|
36
37
|
__exportStar(require("./generate-json/generateJsonOrText.cjs"), exports);
|
38
|
+
__exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
|
39
|
+
__exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
|
37
40
|
__exportStar(require("./generate-text/TextGenerationEvent.cjs"), exports);
|
38
41
|
__exportStar(require("./generate-text/TextGenerationModel.cjs"), exports);
|
39
42
|
__exportStar(require("./generate-text/generateText.cjs"), exports);
|
40
|
-
__exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
|
41
|
-
__exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
|
42
43
|
__exportStar(require("./generate-text/streamText.cjs"), exports);
|
43
44
|
__exportStar(require("./tokenize-text/Tokenizer.cjs"), exports);
|
44
45
|
__exportStar(require("./tokenize-text/countTokens.cjs"), exports);
|
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./FunctionOptions.js";
|
2
2
|
export * from "./Model.js";
|
3
3
|
export * from "./ModelCallEvent.js";
|
4
|
-
export * from "../run/RunFunctionObserver.js";
|
5
4
|
export * from "./ModelInformation.js";
|
6
5
|
export * from "./SuccessfulModelCall.js";
|
7
6
|
export * from "./embed-text/TextEmbeddingEvent.js";
|
@@ -12,17 +11,19 @@ export * from "./generate-image/ImageGenerationModel.js";
|
|
12
11
|
export * from "./generate-image/generateImage.js";
|
13
12
|
export * from "./generate-json/GenerateJsonModel.js";
|
14
13
|
export * from "./generate-json/GenerateJsonOrTextModel.js";
|
14
|
+
export * from "./generate-json/InstructionWithSchemaPrompt.js";
|
15
15
|
export * from "./generate-json/JsonGenerationEvent.js";
|
16
|
+
export * from "./generate-json/JsonTextGenerationModel.js";
|
16
17
|
export * from "./generate-json/NoSuchSchemaError.js";
|
17
18
|
export * from "./generate-json/SchemaDefinition.js";
|
18
19
|
export * from "./generate-json/SchemaValidationError.js";
|
19
20
|
export * from "./generate-json/generateJson.js";
|
20
21
|
export * from "./generate-json/generateJsonOrText.js";
|
22
|
+
export * from "./generate-text/DeltaEvent.js";
|
23
|
+
export * from "./generate-text/TextDeltaEventSource.js";
|
21
24
|
export * from "./generate-text/TextGenerationEvent.js";
|
22
25
|
export * from "./generate-text/TextGenerationModel.js";
|
23
26
|
export * from "./generate-text/generateText.js";
|
24
|
-
export * from "./generate-text/DeltaEvent.js";
|
25
|
-
export * from "./generate-text/TextDeltaEventSource.js";
|
26
27
|
export * from "./generate-text/streamText.js";
|
27
28
|
export * from "./tokenize-text/Tokenizer.js";
|
28
29
|
export * from "./tokenize-text/countTokens.js";
|
package/model-function/index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./FunctionOptions.js";
|
2
2
|
export * from "./Model.js";
|
3
3
|
export * from "./ModelCallEvent.js";
|
4
|
-
export * from "../run/RunFunctionObserver.js";
|
5
4
|
export * from "./ModelInformation.js";
|
6
5
|
export * from "./SuccessfulModelCall.js";
|
7
6
|
export * from "./embed-text/TextEmbeddingEvent.js";
|
@@ -12,17 +11,19 @@ export * from "./generate-image/ImageGenerationModel.js";
|
|
12
11
|
export * from "./generate-image/generateImage.js";
|
13
12
|
export * from "./generate-json/GenerateJsonModel.js";
|
14
13
|
export * from "./generate-json/GenerateJsonOrTextModel.js";
|
14
|
+
export * from "./generate-json/InstructionWithSchemaPrompt.js";
|
15
15
|
export * from "./generate-json/JsonGenerationEvent.js";
|
16
|
+
export * from "./generate-json/JsonTextGenerationModel.js";
|
16
17
|
export * from "./generate-json/NoSuchSchemaError.js";
|
17
18
|
export * from "./generate-json/SchemaDefinition.js";
|
18
19
|
export * from "./generate-json/SchemaValidationError.js";
|
19
20
|
export * from "./generate-json/generateJson.js";
|
20
21
|
export * from "./generate-json/generateJsonOrText.js";
|
22
|
+
export * from "./generate-text/DeltaEvent.js";
|
23
|
+
export * from "./generate-text/TextDeltaEventSource.js";
|
21
24
|
export * from "./generate-text/TextGenerationEvent.js";
|
22
25
|
export * from "./generate-text/TextGenerationModel.js";
|
23
26
|
export * from "./generate-text/generateText.js";
|
24
|
-
export * from "./generate-text/DeltaEvent.js";
|
25
|
-
export * from "./generate-text/TextDeltaEventSource.js";
|
26
27
|
export * from "./generate-text/streamText.js";
|
27
28
|
export * from "./tokenize-text/Tokenizer.js";
|
28
29
|
export * from "./tokenize-text/countTokens.js";
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.OpenAIChatResponseFormat = exports.OpenAIChatModel = exports.calculateOpenAIChatCostInMillicents = exports.isOpenAIChatModel = exports.OPENAI_CHAT_MODELS = void 0;
|
7
|
+
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
7
8
|
const zod_1 = __importDefault(require("zod"));
|
8
9
|
const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
|
9
10
|
const PromptMappingTextGenerationModel_js_1 = require("../../../prompt/PromptMappingTextGenerationModel.cjs");
|
@@ -200,6 +201,10 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
200
201
|
run: options?.run,
|
201
202
|
});
|
202
203
|
}
|
204
|
+
extractJson(response) {
|
205
|
+
const jsonText = response.choices[0].message.function_call.arguments;
|
206
|
+
return secure_json_parse_1.default.parse(jsonText);
|
207
|
+
}
|
203
208
|
mapPrompt(promptMapping) {
|
204
209
|
return new PromptMappingTextGenerationModel_js_1.PromptMappingTextGenerationModel({
|
205
210
|
model: this.withStopTokens(promptMapping.stopTokens),
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import z from "zod";
|
2
2
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
3
3
|
import { FunctionOptions } from "../../../model-function/FunctionOptions.js";
|
4
|
+
import { GenerateJsonModel } from "../../../model-function/generate-json/GenerateJsonModel.js";
|
4
5
|
import { GenerateJsonOrTextModel } from "../../../model-function/generate-json/GenerateJsonOrTextModel.js";
|
5
6
|
import { DeltaEvent } from "../../../model-function/generate-text/DeltaEvent.js";
|
6
7
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../../model-function/generate-text/TextGenerationModel.js";
|
@@ -116,7 +117,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, OpenAIM
|
|
116
117
|
* ),
|
117
118
|
* ]);
|
118
119
|
*/
|
119
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>,
|
120
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, GenerateJsonModel<OpenAIChatSingleFunctionPrompt<unknown>, OpenAIChatResponse, OpenAIChatSettings>, GenerateJsonOrTextModel<OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, OpenAIChatResponse, OpenAIChatSettings> {
|
120
121
|
constructor(settings: OpenAIChatSettings);
|
121
122
|
readonly provider: "openai";
|
122
123
|
get modelName(): "gpt-4" | "gpt-4-0314" | "gpt-4-0613" | "gpt-4-32k" | "gpt-4-32k-0314" | "gpt-4-32k-0613" | "gpt-3.5-turbo" | "gpt-3.5-turbo-0301" | "gpt-3.5-turbo-0613" | "gpt-3.5-turbo-16k" | "gpt-3.5-turbo-16k-0613";
|
@@ -168,6 +169,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
168
169
|
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
169
170
|
*/
|
170
171
|
generateJsonResponse(prompt: OpenAIChatSingleFunctionPrompt<unknown> | OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, options?: FunctionOptions<OpenAIChatSettings> | undefined): PromiseLike<OpenAIChatResponse>;
|
172
|
+
extractJson(response: OpenAIChatResponse): unknown;
|
171
173
|
mapPrompt<INPUT_PROMPT>(promptMapping: PromptMapping<INPUT_PROMPT, OpenAIChatMessage[]>): PromptMappingTextGenerationModel<INPUT_PROMPT, OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings, this>;
|
172
174
|
withSettings(additionalSettings: Partial<OpenAIChatSettings>): this;
|
173
175
|
get maxCompletionTokens(): number | undefined;
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
1
2
|
import z from "zod";
|
2
3
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
3
4
|
import { PromptMappingTextGenerationModel } from "../../../prompt/PromptMappingTextGenerationModel.js";
|
@@ -192,6 +193,10 @@ export class OpenAIChatModel extends AbstractModel {
|
|
192
193
|
run: options?.run,
|
193
194
|
});
|
194
195
|
}
|
196
|
+
extractJson(response) {
|
197
|
+
const jsonText = response.choices[0].message.function_call.arguments;
|
198
|
+
return SecureJSON.parse(jsonText);
|
199
|
+
}
|
195
200
|
mapPrompt(promptMapping) {
|
196
201
|
return new PromptMappingTextGenerationModel({
|
197
202
|
model: this.withStopTokens(promptMapping.stopTokens),
|
@@ -78,10 +78,6 @@ class OpenAIChatSingleFunctionPrompt {
|
|
78
78
|
this.messages = messages;
|
79
79
|
this.fn = fn;
|
80
80
|
}
|
81
|
-
extractJson(response) {
|
82
|
-
const jsonText = response.choices[0].message.function_call.arguments;
|
83
|
-
return secure_json_parse_1.default.parse(jsonText);
|
84
|
-
}
|
85
81
|
get functionCall() {
|
86
82
|
return { name: this.fn.name };
|
87
83
|
}
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import z from "zod";
|
2
|
-
import { GenerateJsonPrompt } from "../../../model-function/generate-json/GenerateJsonModel.js";
|
3
2
|
import { GenerateJsonOrTextPrompt } from "../../../model-function/generate-json/GenerateJsonOrTextModel.js";
|
4
3
|
import { SchemaDefinition } from "../../../model-function/generate-json/SchemaDefinition.js";
|
5
4
|
import { Tool } from "../../../tool/Tool.js";
|
@@ -56,14 +55,13 @@ export declare const OpenAIChatFunctionPrompt: {
|
|
56
55
|
parameters: z.ZodType<any, z.ZodTypeDef, any>;
|
57
56
|
}[]>;
|
58
57
|
};
|
59
|
-
export declare class OpenAIChatSingleFunctionPrompt<
|
58
|
+
export declare class OpenAIChatSingleFunctionPrompt<FUNCTION> {
|
60
59
|
readonly messages: OpenAIChatMessage[];
|
61
|
-
readonly fn: OpenAIFunctionDescription<
|
60
|
+
readonly fn: OpenAIFunctionDescription<FUNCTION>;
|
62
61
|
constructor({ messages, fn, }: {
|
63
62
|
messages: OpenAIChatMessage[];
|
64
|
-
fn: OpenAIFunctionDescription<
|
63
|
+
fn: OpenAIFunctionDescription<FUNCTION>;
|
65
64
|
});
|
66
|
-
extractJson(response: OpenAIChatResponse): any;
|
67
65
|
get functionCall(): {
|
68
66
|
name: string;
|
69
67
|
};
|
@@ -72,10 +72,6 @@ export class OpenAIChatSingleFunctionPrompt {
|
|
72
72
|
this.messages = messages;
|
73
73
|
this.fn = fn;
|
74
74
|
}
|
75
|
-
extractJson(response) {
|
76
|
-
const jsonText = response.choices[0].message.function_call.arguments;
|
77
|
-
return SecureJSON.parse(jsonText);
|
78
|
-
}
|
79
75
|
get functionCall() {
|
80
76
|
return { name: this.fn.name };
|
81
77
|
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.12.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -71,7 +71,7 @@
|
|
71
71
|
"prettier": "3.0.1",
|
72
72
|
"rimraf": "5.0.1",
|
73
73
|
"typescript": "5.1.6",
|
74
|
-
"zod": "3.
|
74
|
+
"zod": "3.22.0",
|
75
75
|
"zod-to-json-schema": "3.21.4"
|
76
76
|
},
|
77
77
|
"peerDependencies": {
|
package/tool/useTool.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FunctionOptions } from "../model-function/FunctionOptions.js";
|
2
|
-
import { GenerateJsonModel, GenerateJsonModelSettings
|
2
|
+
import { GenerateJsonModel, GenerateJsonModelSettings } from "../model-function/generate-json/GenerateJsonModel.js";
|
3
3
|
import { Tool } from "./Tool.js";
|
4
4
|
/**
|
5
5
|
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
|
@@ -8,7 +8,7 @@ import { Tool } from "./Tool.js";
|
|
8
8
|
* the parameters (`parameters` property, typed),
|
9
9
|
* and the result of the tool execution (`result` property, typed).
|
10
10
|
*/
|
11
|
-
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT
|
11
|
+
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT, options?: FunctionOptions<SETTINGS>): Promise<{
|
12
12
|
tool: TOOL["name"];
|
13
13
|
parameters: TOOL["inputSchema"];
|
14
14
|
result: Awaited<ReturnType<TOOL["execute"]>>;
|