modelfusion 0.35.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/FunctionEvent.d.ts +3 -2
- package/core/getFunctionCallLogger.cjs +3 -2
- package/core/getFunctionCallLogger.js +3 -2
- package/model-function/generate-structure/fixJson.cjs +8 -1
- package/model-function/generate-structure/fixJson.js +8 -1
- package/model-provider/openai/OpenAITextGenerationModel.cjs +4 -0
- package/model-provider/openai/OpenAITextGenerationModel.js +4 -0
- package/model-provider/openai/chat/OpenAIChatModel.cjs +4 -0
- package/model-provider/openai/chat/OpenAIChatModel.js +4 -0
- package/package.json +1 -1
- package/retriever/RetrieveEvent.cjs +2 -0
- package/retriever/RetrieveEvent.d.ts +10 -0
- package/retriever/RetrieveEvent.js +1 -0
- package/retriever/retrieve.cjs +71 -3
- package/retriever/retrieve.js +71 -3
package/core/FunctionEvent.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
|
2
1
|
import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
|
2
|
+
import { RetrieveFinishedEvent, RetrieveStartedEvent } from "../retriever/RetrieveEvent.js";
|
3
|
+
import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
|
3
4
|
export interface BaseFunctionEvent {
|
4
5
|
/**
|
5
6
|
* Unique identifier for the function call.
|
@@ -72,4 +73,4 @@ export interface BaseFunctionFinishedEvent extends BaseFunctionEvent {
|
|
72
73
|
*/
|
73
74
|
result: BaseFunctionFinishedEventResult;
|
74
75
|
}
|
75
|
-
export type FunctionEvent = ModelCallStartedEvent | ExecuteToolStartedEvent | ModelCallFinishedEvent | ExecuteToolFinishedEvent;
|
76
|
+
export type FunctionEvent = ModelCallStartedEvent | ExecuteToolStartedEvent | RetrieveStartedEvent | ModelCallFinishedEvent | ExecuteToolFinishedEvent | RetrieveFinishedEvent;
|
@@ -17,14 +17,15 @@ function getFunctionCallLogger(logging) {
|
|
17
17
|
exports.getFunctionCallLogger = getFunctionCallLogger;
|
18
18
|
const basicTextObserver = {
|
19
19
|
onFunctionEvent(event) {
|
20
|
+
const text = `[${event.timestamp.toISOString()}] ${event.callId}${event.functionId != null ? ` (${event.functionId})` : ""} - ${event.functionType} ${event.eventType}`;
|
20
21
|
// log based on event type:
|
21
22
|
switch (event.eventType) {
|
22
23
|
case "started": {
|
23
|
-
console.log(
|
24
|
+
console.log(text);
|
24
25
|
break;
|
25
26
|
}
|
26
27
|
case "finished": {
|
27
|
-
console.log(
|
28
|
+
console.log(`${text} in ${event.durationInMs}ms`);
|
28
29
|
break;
|
29
30
|
}
|
30
31
|
}
|
@@ -13,14 +13,15 @@ export function getFunctionCallLogger(logging) {
|
|
13
13
|
}
|
14
14
|
const basicTextObserver = {
|
15
15
|
onFunctionEvent(event) {
|
16
|
+
const text = `[${event.timestamp.toISOString()}] ${event.callId}${event.functionId != null ? ` (${event.functionId})` : ""} - ${event.functionType} ${event.eventType}`;
|
16
17
|
// log based on event type:
|
17
18
|
switch (event.eventType) {
|
18
19
|
case "started": {
|
19
|
-
console.log(
|
20
|
+
console.log(text);
|
20
21
|
break;
|
21
22
|
}
|
22
23
|
case "finished": {
|
23
|
-
console.log(
|
24
|
+
console.log(`${text} in ${event.durationInMs}ms`);
|
24
25
|
break;
|
25
26
|
}
|
26
27
|
}
|
@@ -2,7 +2,14 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.fixJson = void 0;
|
4
4
|
// Implemented as a scanner with additional fixing
|
5
|
-
// that performs a single linear time scan pass over the partial JSON
|
5
|
+
// that performs a single linear time scan pass over the partial JSON.
|
6
|
+
//
|
7
|
+
// The states should ideally match relevant states from the JSON spec:
|
8
|
+
// https://www.json.org/json-en.html
|
9
|
+
//
|
10
|
+
// Please note that invalid JSON is not considered/covered, because it
|
11
|
+
// is assumed that the resulting JSON will be processed by a standard
|
12
|
+
// JSON parser that will detect any invalid JSON.
|
6
13
|
function fixJson(input) {
|
7
14
|
const stack = ["ROOT"];
|
8
15
|
let lastValidIndex = -1;
|
@@ -1,5 +1,12 @@
|
|
1
1
|
// Implemented as a scanner with additional fixing
|
2
|
-
// that performs a single linear time scan pass over the partial JSON
|
2
|
+
// that performs a single linear time scan pass over the partial JSON.
|
3
|
+
//
|
4
|
+
// The states should ideally match relevant states from the JSON spec:
|
5
|
+
// https://www.json.org/json-en.html
|
6
|
+
//
|
7
|
+
// Please note that invalid JSON is not considered/covered, because it
|
8
|
+
// is assumed that the resulting JSON will be processed by a standard
|
9
|
+
// JSON parser that will detect any invalid JSON.
|
3
10
|
export function fixJson(input) {
|
4
11
|
const stack = ["ROOT"];
|
5
12
|
let lastValidIndex = -1;
|
@@ -277,6 +277,10 @@ const openAITextGenerationResponseSchema = zod_1.default.object({
|
|
277
277
|
}),
|
278
278
|
});
|
279
279
|
async function callOpenAITextGenerationAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, user, }) {
|
280
|
+
// empty arrays are not allowed for stop:
|
281
|
+
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
282
|
+
stop = undefined;
|
283
|
+
}
|
280
284
|
return (0, postToApi_js_1.postJsonToApi)({
|
281
285
|
url: api.assembleUrl("/completions"),
|
282
286
|
headers: api.headers,
|
@@ -267,6 +267,10 @@ const openAITextGenerationResponseSchema = z.object({
|
|
267
267
|
}),
|
268
268
|
});
|
269
269
|
async function callOpenAITextGenerationAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, user, }) {
|
270
|
+
// empty arrays are not allowed for stop:
|
271
|
+
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
272
|
+
stop = undefined;
|
273
|
+
}
|
270
274
|
return postJsonToApi({
|
271
275
|
url: api.assembleUrl("/completions"),
|
272
276
|
headers: api.headers,
|
@@ -365,6 +365,10 @@ const openAIChatResponseSchema = zod_1.default.object({
|
|
365
365
|
}),
|
366
366
|
});
|
367
367
|
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
|
368
|
+
// empty arrays are not allowed for stop:
|
369
|
+
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
370
|
+
stop = undefined;
|
371
|
+
}
|
368
372
|
return (0, postToApi_js_1.postJsonToApi)({
|
369
373
|
url: api.assembleUrl("/chat/completions"),
|
370
374
|
headers: api.headers,
|
@@ -355,6 +355,10 @@ const openAIChatResponseSchema = z.object({
|
|
355
355
|
}),
|
356
356
|
});
|
357
357
|
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
|
358
|
+
// empty arrays are not allowed for stop:
|
359
|
+
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
360
|
+
stop = undefined;
|
361
|
+
}
|
358
362
|
return postJsonToApi({
|
359
363
|
url: api.assembleUrl("/chat/completions"),
|
360
364
|
headers: api.headers,
|
package/package.json
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
|
+
export interface RetrieveStartedEvent extends BaseFunctionStartedEvent {
|
3
|
+
functionType: "retrieve";
|
4
|
+
query: unknown;
|
5
|
+
}
|
6
|
+
export interface RetrieveFinishedEvent extends BaseFunctionFinishedEvent {
|
7
|
+
functionType: "retrieve";
|
8
|
+
query: unknown;
|
9
|
+
results: unknown;
|
10
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/retriever/retrieve.cjs
CHANGED
@@ -1,9 +1,77 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.retrieve = void 0;
|
4
|
+
const nanoid_1 = require("nanoid");
|
5
|
+
const FunctionEventSource_js_1 = require("../core/FunctionEventSource.cjs");
|
6
|
+
const GlobalFunctionLogging_js_1 = require("../core/GlobalFunctionLogging.cjs");
|
7
|
+
const GlobalFunctionObservers_js_1 = require("../core/GlobalFunctionObservers.cjs");
|
8
|
+
const AbortError_js_1 = require("../core/api/AbortError.cjs");
|
9
|
+
const getFunctionCallLogger_js_1 = require("../core/getFunctionCallLogger.cjs");
|
10
|
+
const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
|
11
|
+
const runSafe_js_1 = require("../util/runSafe.cjs");
|
4
12
|
async function retrieve(retriever, query, options) {
|
5
|
-
|
6
|
-
|
7
|
-
|
13
|
+
const run = options?.run;
|
14
|
+
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
15
|
+
observers: [
|
16
|
+
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
|
17
|
+
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
18
|
+
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
19
|
+
...(options?.observers ?? []),
|
20
|
+
],
|
21
|
+
errorHandler: run?.errorHandler,
|
22
|
+
});
|
23
|
+
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
24
|
+
const startMetadata = {
|
25
|
+
functionType: "retrieve",
|
26
|
+
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
27
|
+
runId: run?.runId,
|
28
|
+
sessionId: run?.sessionId,
|
29
|
+
userId: run?.userId,
|
30
|
+
functionId: options?.functionId,
|
31
|
+
query,
|
32
|
+
timestamp: durationMeasurement.startDate,
|
33
|
+
startTimestamp: durationMeasurement.startDate,
|
34
|
+
};
|
35
|
+
eventSource.notify({
|
36
|
+
eventType: "started",
|
37
|
+
...startMetadata,
|
38
|
+
});
|
39
|
+
const result = await (0, runSafe_js_1.runSafe)(() => retriever.retrieve(query, options));
|
40
|
+
const finishMetadata = {
|
41
|
+
eventType: "finished",
|
42
|
+
...startMetadata,
|
43
|
+
finishTimestamp: new Date(),
|
44
|
+
durationInMs: durationMeasurement.durationInMs,
|
45
|
+
};
|
46
|
+
if (!result.ok) {
|
47
|
+
if (result.isAborted) {
|
48
|
+
eventSource.notify({
|
49
|
+
...finishMetadata,
|
50
|
+
eventType: "finished",
|
51
|
+
result: {
|
52
|
+
status: "abort",
|
53
|
+
},
|
54
|
+
});
|
55
|
+
throw new AbortError_js_1.AbortError();
|
56
|
+
}
|
57
|
+
eventSource.notify({
|
58
|
+
...finishMetadata,
|
59
|
+
eventType: "finished",
|
60
|
+
result: {
|
61
|
+
status: "error",
|
62
|
+
error: result.error,
|
63
|
+
},
|
64
|
+
});
|
65
|
+
throw result.error;
|
66
|
+
}
|
67
|
+
eventSource.notify({
|
68
|
+
...finishMetadata,
|
69
|
+
eventType: "finished",
|
70
|
+
result: {
|
71
|
+
status: "success",
|
72
|
+
output: result.output,
|
73
|
+
},
|
74
|
+
});
|
75
|
+
return result.output;
|
8
76
|
}
|
9
77
|
exports.retrieve = retrieve;
|
package/retriever/retrieve.js
CHANGED
@@ -1,5 +1,73 @@
|
|
1
|
+
import { nanoid as createId } from "nanoid";
|
2
|
+
import { FunctionEventSource } from "../core/FunctionEventSource.js";
|
3
|
+
import { getGlobalFunctionLogging } from "../core/GlobalFunctionLogging.js";
|
4
|
+
import { getGlobalFunctionObservers } from "../core/GlobalFunctionObservers.js";
|
5
|
+
import { AbortError } from "../core/api/AbortError.js";
|
6
|
+
import { getFunctionCallLogger } from "../core/getFunctionCallLogger.js";
|
7
|
+
import { startDurationMeasurement } from "../util/DurationMeasurement.js";
|
8
|
+
import { runSafe } from "../util/runSafe.js";
|
1
9
|
export async function retrieve(retriever, query, options) {
|
2
|
-
|
3
|
-
|
4
|
-
|
10
|
+
const run = options?.run;
|
11
|
+
const eventSource = new FunctionEventSource({
|
12
|
+
observers: [
|
13
|
+
...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
|
14
|
+
...getGlobalFunctionObservers(),
|
15
|
+
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
16
|
+
...(options?.observers ?? []),
|
17
|
+
],
|
18
|
+
errorHandler: run?.errorHandler,
|
19
|
+
});
|
20
|
+
const durationMeasurement = startDurationMeasurement();
|
21
|
+
const startMetadata = {
|
22
|
+
functionType: "retrieve",
|
23
|
+
callId: `call-${createId()}`,
|
24
|
+
runId: run?.runId,
|
25
|
+
sessionId: run?.sessionId,
|
26
|
+
userId: run?.userId,
|
27
|
+
functionId: options?.functionId,
|
28
|
+
query,
|
29
|
+
timestamp: durationMeasurement.startDate,
|
30
|
+
startTimestamp: durationMeasurement.startDate,
|
31
|
+
};
|
32
|
+
eventSource.notify({
|
33
|
+
eventType: "started",
|
34
|
+
...startMetadata,
|
35
|
+
});
|
36
|
+
const result = await runSafe(() => retriever.retrieve(query, options));
|
37
|
+
const finishMetadata = {
|
38
|
+
eventType: "finished",
|
39
|
+
...startMetadata,
|
40
|
+
finishTimestamp: new Date(),
|
41
|
+
durationInMs: durationMeasurement.durationInMs,
|
42
|
+
};
|
43
|
+
if (!result.ok) {
|
44
|
+
if (result.isAborted) {
|
45
|
+
eventSource.notify({
|
46
|
+
...finishMetadata,
|
47
|
+
eventType: "finished",
|
48
|
+
result: {
|
49
|
+
status: "abort",
|
50
|
+
},
|
51
|
+
});
|
52
|
+
throw new AbortError();
|
53
|
+
}
|
54
|
+
eventSource.notify({
|
55
|
+
...finishMetadata,
|
56
|
+
eventType: "finished",
|
57
|
+
result: {
|
58
|
+
status: "error",
|
59
|
+
error: result.error,
|
60
|
+
},
|
61
|
+
});
|
62
|
+
throw result.error;
|
63
|
+
}
|
64
|
+
eventSource.notify({
|
65
|
+
...finishMetadata,
|
66
|
+
eventType: "finished",
|
67
|
+
result: {
|
68
|
+
status: "success",
|
69
|
+
output: result.output,
|
70
|
+
},
|
71
|
+
});
|
72
|
+
return result.output;
|
5
73
|
}
|