modelfusion 0.34.0 → 0.35.1
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 +47 -1
- package/model-function/AsyncIterableResultPromise.cjs +37 -0
- package/model-function/AsyncIterableResultPromise.d.ts +16 -0
- package/model-function/AsyncIterableResultPromise.js +33 -0
- package/model-function/{generate-text/DeltaEvent.d.ts → DeltaEvent.d.ts} +1 -1
- package/model-function/ModelCallEvent.d.ts +3 -2
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +10 -1
- package/model-function/generate-structure/StructureStreamingEvent.cjs +2 -0
- package/model-function/generate-structure/StructureStreamingEvent.d.ts +7 -0
- package/model-function/generate-structure/StructureStreamingEvent.js +1 -0
- package/model-function/generate-structure/fixJson.cjs +327 -0
- package/model-function/generate-structure/fixJson.d.ts +1 -0
- package/model-function/generate-structure/fixJson.js +323 -0
- package/model-function/generate-structure/fixJson.test.cjs +186 -0
- package/model-function/generate-structure/fixJson.test.d.ts +1 -0
- package/model-function/generate-structure/fixJson.test.js +184 -0
- package/model-function/generate-structure/generateStructure.cjs +3 -1
- package/model-function/generate-structure/generateStructure.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.js +3 -1
- package/model-function/generate-structure/parsePartialJson.cjs +29 -0
- package/model-function/generate-structure/parsePartialJson.d.ts +1 -0
- package/model-function/generate-structure/parsePartialJson.js +22 -0
- package/model-function/generate-structure/streamStructure.cjs +167 -0
- package/model-function/generate-structure/streamStructure.d.ts +16 -0
- package/model-function/generate-structure/streamStructure.js +160 -0
- package/model-function/generate-text/TextGenerationModel.d.ts +4 -4
- package/model-function/generate-text/streamText.cjs +47 -68
- package/model-function/generate-text/streamText.d.ts +3 -18
- package/model-function/generate-text/streamText.js +46 -66
- package/model-function/index.cjs +3 -1
- package/model-function/index.d.ts +3 -1
- package/model-function/index.js +3 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.js +3 -3
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +0 -12
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +0 -2
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +0 -12
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +3 -3
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.cjs +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.js +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +23 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -2
- package/model-provider/openai/chat/OpenAIChatModel.js +23 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +1 -1
- package/package.json +8 -4
- package/prompt/PromptFormatTextGenerationModel.d.ts +1 -1
- package/tool/useTool.cjs +3 -4
- package/tool/useTool.d.ts +1 -1
- package/tool/useTool.js +3 -4
- package/model-function/generate-text/extractTextDeltas.cjs +0 -23
- package/model-function/generate-text/extractTextDeltas.d.ts +0 -7
- package/model-function/generate-text/extractTextDeltas.js +0 -19
- /package/model-function/{generate-text/DeltaEvent.cjs → DeltaEvent.cjs} +0 -0
- /package/model-function/{generate-text/DeltaEvent.js → DeltaEvent.js} +0 -0
@@ -3,10 +3,10 @@ import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
|
|
3
3
|
import { ResponseHandler } from "../../../core/api/postToApi.js";
|
4
4
|
import { StructureDefinition } from "../../../core/structure/StructureDefinition.js";
|
5
5
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
6
|
+
import { DeltaEvent } from "../../../model-function/DeltaEvent.js";
|
6
7
|
import { ModelFunctionOptions } from "../../../model-function/ModelFunctionOptions.js";
|
7
8
|
import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
|
8
9
|
import { StructureOrTextGenerationModel } from "../../../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
9
|
-
import { DeltaEvent } from "../../../model-function/generate-text/DeltaEvent.js";
|
10
10
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../../model-function/generate-text/TextGenerationModel.js";
|
11
11
|
import { PromptFormat } from "../../../prompt/PromptFormat.js";
|
12
12
|
import { PromptFormatTextGenerationModel } from "../../../prompt/PromptFormatTextGenerationModel.js";
|
@@ -132,7 +132,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
132
132
|
* ),
|
133
133
|
* ]);
|
134
134
|
*/
|
135
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatSettings>, StructureOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatSettings> {
|
135
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, StructureOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatSettings> {
|
136
136
|
constructor(settings: OpenAIChatSettings);
|
137
137
|
readonly provider: "openai";
|
138
138
|
get modelName(): OpenAIChatModelType;
|
@@ -185,6 +185,8 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
185
185
|
*/
|
186
186
|
generateStructureResponse(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: ModelFunctionOptions<OpenAIChatSettings> | undefined): PromiseLike<OpenAIChatResponse>;
|
187
187
|
extractStructure(response: OpenAIChatResponse): unknown;
|
188
|
+
generateStructureStreamResponse(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: ModelFunctionOptions<OpenAIChatSettings>): Promise<AsyncIterable<DeltaEvent<OpenAIChatDelta>>>;
|
189
|
+
extractPartialStructure(fullDelta: OpenAIChatDelta): unknown | undefined;
|
188
190
|
generateStructureOrTextResponse(structureDefinitions: Array<StructureDefinition<string, unknown>>, prompt: OpenAIChatMessage[], options?: ModelFunctionOptions<OpenAIChatSettings> | undefined): PromiseLike<OpenAIChatResponse>;
|
189
191
|
extractStructureAndText(response: OpenAIChatResponse): {
|
190
192
|
structure: null;
|
@@ -3,6 +3,7 @@ import z from "zod";
|
|
3
3
|
import { callWithRetryAndThrottle } from "../../../core/api/callWithRetryAndThrottle.js";
|
4
4
|
import { createJsonResponseHandler, postJsonToApi, } from "../../../core/api/postToApi.js";
|
5
5
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
6
|
+
import { parsePartialJson } from "../../../model-function/generate-structure/parsePartialJson.js";
|
6
7
|
import { PromptFormatTextGenerationModel } from "../../../prompt/PromptFormatTextGenerationModel.js";
|
7
8
|
import { OpenAIApiConfiguration } from "../OpenAIApiConfiguration.js";
|
8
9
|
import { failedOpenAICallResponseHandler } from "../OpenAIError.js";
|
@@ -255,8 +256,28 @@ export class OpenAIChatModel extends AbstractModel {
|
|
255
256
|
});
|
256
257
|
}
|
257
258
|
extractStructure(response) {
|
258
|
-
|
259
|
-
|
259
|
+
return SecureJSON.parse(response.choices[0].message.function_call.arguments);
|
260
|
+
}
|
261
|
+
generateStructureStreamResponse(structureDefinition, prompt, options) {
|
262
|
+
return this.callAPI(prompt, {
|
263
|
+
responseFormat: OpenAIChatResponseFormat.deltaIterable,
|
264
|
+
functionId: options?.functionId,
|
265
|
+
settings: {
|
266
|
+
...options,
|
267
|
+
functionCall: { name: structureDefinition.name },
|
268
|
+
functions: [
|
269
|
+
{
|
270
|
+
name: structureDefinition.name,
|
271
|
+
description: structureDefinition.description,
|
272
|
+
parameters: structureDefinition.schema.getJsonSchema(),
|
273
|
+
},
|
274
|
+
],
|
275
|
+
},
|
276
|
+
run: options?.run,
|
277
|
+
});
|
278
|
+
}
|
279
|
+
extractPartialStructure(fullDelta) {
|
280
|
+
return parsePartialJson(fullDelta[0]?.function_call?.arguments);
|
260
281
|
}
|
261
282
|
generateStructureOrTextResponse(structureDefinitions, prompt, options) {
|
262
283
|
return this.callAPI(prompt, {
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.35.1",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -47,10 +47,12 @@
|
|
47
47
|
"build": "npm run build:esm && npm run build:cjs",
|
48
48
|
"build:esm": "tsc --outDir dist/",
|
49
49
|
"build:cjs": "tsc --outDir build/cjs/ -p tsconfig.cjs.json && node bin/prepare-cjs.js",
|
50
|
-
"
|
51
|
-
"dist": "npm run clean && npm run lint && npm run build && npm run dist:copy-files"
|
50
|
+
"test": "vitest",
|
51
|
+
"dist": "npm run clean && npm run lint && npm run build && npm run dist:copy-files",
|
52
|
+
"dist:copy-files": "copyfiles package.json README.md LICENSE dist"
|
52
53
|
},
|
53
54
|
"dependencies": {
|
55
|
+
"deep-equal": "2.2.2",
|
54
56
|
"eventsource-parser": "1.1.1",
|
55
57
|
"js-tiktoken": "1.0.7",
|
56
58
|
"nanoid": "3.3.6",
|
@@ -61,6 +63,7 @@
|
|
61
63
|
"devDependencies": {
|
62
64
|
"@pinecone-database/pinecone": "^0.1.6",
|
63
65
|
"@tsconfig/recommended": "1.0.3",
|
66
|
+
"@types/deep-equal": "^1.0.2",
|
64
67
|
"@types/node": "18.11.9",
|
65
68
|
"@typescript-eslint/eslint-plugin": "^6.1.0",
|
66
69
|
"@typescript-eslint/parser": "^6.1.0",
|
@@ -71,7 +74,8 @@
|
|
71
74
|
"lint-staged": "14.0.1",
|
72
75
|
"prettier": "3.0.3",
|
73
76
|
"rimraf": "5.0.1",
|
74
|
-
"typescript": "5.2.2"
|
77
|
+
"typescript": "5.2.2",
|
78
|
+
"vitest": "^0.34.5"
|
75
79
|
},
|
76
80
|
"peerDependencies": {
|
77
81
|
"@pinecone-database/pinecone": "0.1.6"
|
@@ -1,5 +1,5 @@
|
|
1
|
+
import { DeltaEvent } from "../model-function/DeltaEvent.js";
|
1
2
|
import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
|
2
|
-
import { DeltaEvent } from "../model-function/generate-text/DeltaEvent.js";
|
3
3
|
import { TextGenerationModel, TextGenerationModelSettings } from "../model-function/generate-text/TextGenerationModel.js";
|
4
4
|
import { PromptFormat } from "./PromptFormat.js";
|
5
5
|
export declare class PromptFormatTextGenerationModel<PROMPT, MODEL_PROMPT, RESPONSE, FULL_DELTA, SETTINGS extends TextGenerationModelSettings, MODEL extends TextGenerationModel<MODEL_PROMPT, RESPONSE, FULL_DELTA, SETTINGS>> implements TextGenerationModel<PROMPT, RESPONSE, FULL_DELTA, SETTINGS> {
|
package/tool/useTool.cjs
CHANGED
@@ -3,9 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useTool = void 0;
|
4
4
|
const generateStructure_js_1 = require("../model-function/generate-structure/generateStructure.cjs");
|
5
5
|
const executeTool_js_1 = require("./executeTool.cjs");
|
6
|
-
// In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
|
7
|
-
// retrieved through lookups such as TOOL["name"], such that any does not affect any client.
|
8
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
9
6
|
/**
|
10
7
|
* `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
|
11
8
|
*
|
@@ -13,7 +10,9 @@ const executeTool_js_1 = require("./executeTool.cjs");
|
|
13
10
|
* the parameters (`parameters` property, typed),
|
14
11
|
* and the result of the tool execution (`result` property, typed).
|
15
12
|
*/
|
16
|
-
async function useTool(
|
13
|
+
async function useTool(
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
15
|
+
model, tool, prompt, options) {
|
17
16
|
// Note: PROMPT must not be a function.
|
18
17
|
const expandedPrompt = typeof prompt === "function"
|
19
18
|
? prompt(tool)
|
package/tool/useTool.d.ts
CHANGED
@@ -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 StructureGenerationModelSettings, TOOL extends Tool<any, any, any>>(model: StructureGenerationModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: PROMPT | ((tool: TOOL) => PROMPT), options?: ModelFunctionOptions<SETTINGS>): Promise<{
|
11
|
+
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends StructureGenerationModelSettings, TOOL extends Tool<any, any, any>>(model: StructureGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, tool: TOOL, prompt: PROMPT | ((tool: TOOL) => PROMPT), options?: ModelFunctionOptions<SETTINGS>): Promise<{
|
12
12
|
tool: TOOL["name"];
|
13
13
|
parameters: TOOL["inputSchema"];
|
14
14
|
result: Awaited<ReturnType<TOOL["execute"]>>;
|
package/tool/useTool.js
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
import { generateStructure } from "../model-function/generate-structure/generateStructure.js";
|
2
2
|
import { executeTool } from "./executeTool.js";
|
3
|
-
// In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
|
4
|
-
// retrieved through lookups such as TOOL["name"], such that any does not affect any client.
|
5
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
6
3
|
/**
|
7
4
|
* `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
|
8
5
|
*
|
@@ -10,7 +7,9 @@ import { executeTool } from "./executeTool.js";
|
|
10
7
|
* the parameters (`parameters` property, typed),
|
11
8
|
* and the result of the tool execution (`result` property, typed).
|
12
9
|
*/
|
13
|
-
export async function useTool(
|
10
|
+
export async function useTool(
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
12
|
+
model, tool, prompt, options) {
|
14
13
|
// Note: PROMPT must not be a function.
|
15
14
|
const expandedPrompt = typeof prompt === "function"
|
16
15
|
? prompt(tool)
|
@@ -1,23 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.extractTextDeltas = void 0;
|
4
|
-
async function* extractTextDeltas({ deltaIterable, extractDelta, onDone, onError, }) {
|
5
|
-
let accumulatedText = "";
|
6
|
-
let lastFullDelta;
|
7
|
-
for await (const event of deltaIterable) {
|
8
|
-
if (event?.type === "error") {
|
9
|
-
onError(event.error);
|
10
|
-
throw event.error;
|
11
|
-
}
|
12
|
-
if (event?.type === "delta") {
|
13
|
-
lastFullDelta = event.fullDelta;
|
14
|
-
const delta = extractDelta(lastFullDelta);
|
15
|
-
if (delta != null && delta.length > 0) {
|
16
|
-
accumulatedText += delta;
|
17
|
-
yield delta;
|
18
|
-
}
|
19
|
-
}
|
20
|
-
}
|
21
|
-
onDone(accumulatedText, lastFullDelta);
|
22
|
-
}
|
23
|
-
exports.extractTextDeltas = extractTextDeltas;
|
@@ -1,7 +0,0 @@
|
|
1
|
-
import { DeltaEvent } from "./DeltaEvent.js";
|
2
|
-
export declare function extractTextDeltas<FULL_DELTA>({ deltaIterable, extractDelta, onDone, onError, }: {
|
3
|
-
deltaIterable: AsyncIterable<DeltaEvent<FULL_DELTA>>;
|
4
|
-
extractDelta: (fullDelta: FULL_DELTA) => string | undefined;
|
5
|
-
onDone: (fullText: string, lastFullDelta: FULL_DELTA | undefined) => void;
|
6
|
-
onError: (error: unknown) => void;
|
7
|
-
}): AsyncIterable<string>;
|
@@ -1,19 +0,0 @@
|
|
1
|
-
export async function* extractTextDeltas({ deltaIterable, extractDelta, onDone, onError, }) {
|
2
|
-
let accumulatedText = "";
|
3
|
-
let lastFullDelta;
|
4
|
-
for await (const event of deltaIterable) {
|
5
|
-
if (event?.type === "error") {
|
6
|
-
onError(event.error);
|
7
|
-
throw event.error;
|
8
|
-
}
|
9
|
-
if (event?.type === "delta") {
|
10
|
-
lastFullDelta = event.fullDelta;
|
11
|
-
const delta = extractDelta(lastFullDelta);
|
12
|
-
if (delta != null && delta.length > 0) {
|
13
|
-
accumulatedText += delta;
|
14
|
-
yield delta;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
}
|
18
|
-
onDone(accumulatedText, lastFullDelta);
|
19
|
-
}
|
File without changes
|
File without changes
|