@alpic80/rivet-core 1.19.1-aidon.1 → 1.19.1-aidon.2
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/dist/cjs/bundle.cjs
CHANGED
|
@@ -2178,7 +2178,10 @@ async function* streamChatCompletions({
|
|
|
2178
2178
|
},
|
|
2179
2179
|
body: JSON.stringify({
|
|
2180
2180
|
...rest,
|
|
2181
|
-
stream: true
|
|
2181
|
+
stream: true,
|
|
2182
|
+
stream_options: {
|
|
2183
|
+
"include_usage": true
|
|
2184
|
+
}
|
|
2182
2185
|
}),
|
|
2183
2186
|
signal: abortSignal
|
|
2184
2187
|
},
|
|
@@ -2970,6 +2973,8 @@ var ChatNodeImpl = class extends NodeImpl {
|
|
|
2970
2973
|
}
|
|
2971
2974
|
}
|
|
2972
2975
|
const startTime = Date.now();
|
|
2976
|
+
let usagePromptTokens = -1;
|
|
2977
|
+
let usageCompletionTokens = -1;
|
|
2973
2978
|
const chunks = streamChatCompletions({
|
|
2974
2979
|
auth: {
|
|
2975
2980
|
apiKey: context.settings.openAiKey ?? "",
|
|
@@ -2986,6 +2991,10 @@ var ChatNodeImpl = class extends NodeImpl {
|
|
|
2986
2991
|
if (!chunk.choices) {
|
|
2987
2992
|
continue;
|
|
2988
2993
|
}
|
|
2994
|
+
if (chunk.choices.length == 0 && chunk.usage) {
|
|
2995
|
+
usagePromptTokens = chunk.usage.prompt_tokens;
|
|
2996
|
+
usageCompletionTokens = chunk.usage.completion_tokens;
|
|
2997
|
+
}
|
|
2989
2998
|
for (const { delta, index } of chunk.choices) {
|
|
2990
2999
|
if (delta.content != null) {
|
|
2991
3000
|
responseChoicesParts[index] ??= [];
|
|
@@ -3081,11 +3090,22 @@ var ChatNodeImpl = class extends NodeImpl {
|
|
|
3081
3090
|
throw new Error("No response from OpenAI");
|
|
3082
3091
|
}
|
|
3083
3092
|
output["in-messages"] = { type: "chat-message[]", value: messages };
|
|
3084
|
-
|
|
3093
|
+
let finalTokenCount = tokenCount * (numberOfChoices ?? 1);
|
|
3085
3094
|
let responseTokenCount = 0;
|
|
3086
3095
|
for (const choiceParts of responseChoicesParts) {
|
|
3087
|
-
responseTokenCount += await context.tokenizer.getTokenCountForString(choiceParts.join(), tokenizerInfo);
|
|
3096
|
+
responseTokenCount += await context.tokenizer.getTokenCountForString(choiceParts.join(""), tokenizerInfo);
|
|
3088
3097
|
}
|
|
3098
|
+
if (usagePromptTokens != -1 && usageCompletionTokens != -1) {
|
|
3099
|
+
if (finalTokenCount != usagePromptTokens) {
|
|
3100
|
+
console.log(`calculated token count:${finalTokenCount}, usage:${usagePromptTokens}`);
|
|
3101
|
+
finalTokenCount = usagePromptTokens;
|
|
3102
|
+
}
|
|
3103
|
+
if (responseTokenCount != usageCompletionTokens) {
|
|
3104
|
+
console.log(`calculated response token count:${responseTokenCount}, usage:${usageCompletionTokens}`);
|
|
3105
|
+
responseTokenCount = usageCompletionTokens;
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
output["requestTokens"] = { type: "number", value: finalTokenCount };
|
|
3089
3109
|
output["responseTokens"] = { type: "number", value: responseTokenCount };
|
|
3090
3110
|
const promptCostPerThousand = model in openaiModels ? openaiModels[model].cost.prompt : 0;
|
|
3091
3111
|
const completionCostPerThousand = model in openaiModels ? openaiModels[model].cost.completion : 0;
|
|
@@ -11184,6 +11204,8 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11184
11204
|
#aborted = false;
|
|
11185
11205
|
#abortSuccessfully = false;
|
|
11186
11206
|
#abortError = void 0;
|
|
11207
|
+
#totalRequestTokens = 0;
|
|
11208
|
+
#totalResponseTokens = 0;
|
|
11187
11209
|
#totalCost = 0;
|
|
11188
11210
|
#ignoreNodes = void 0;
|
|
11189
11211
|
#nodeAbortControllers = /* @__PURE__ */ new Map();
|
|
@@ -11575,6 +11597,18 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11575
11597
|
}
|
|
11576
11598
|
throw error;
|
|
11577
11599
|
}
|
|
11600
|
+
if (this.#graphOutputs["requestTokens"] == null) {
|
|
11601
|
+
this.#graphOutputs["requestTokens"] = {
|
|
11602
|
+
type: "number",
|
|
11603
|
+
value: this.#totalRequestTokens
|
|
11604
|
+
};
|
|
11605
|
+
}
|
|
11606
|
+
if (this.#graphOutputs["responseTokens"] == null) {
|
|
11607
|
+
this.#graphOutputs["responseTokens"] = {
|
|
11608
|
+
type: "number",
|
|
11609
|
+
value: this.#totalResponseTokens
|
|
11610
|
+
};
|
|
11611
|
+
}
|
|
11578
11612
|
if (this.#graphOutputs["cost"] == null) {
|
|
11579
11613
|
this.#graphOutputs["cost"] = {
|
|
11580
11614
|
type: "number",
|
|
@@ -11886,7 +11920,7 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11886
11920
|
}
|
|
11887
11921
|
}
|
|
11888
11922
|
async #processSplitRunNode(node, processId) {
|
|
11889
|
-
var _a;
|
|
11923
|
+
var _a, _b, _c;
|
|
11890
11924
|
const inputValues = this.#getInputValuesForNode(node);
|
|
11891
11925
|
if (this.#excludedDueToControlFlow(node, inputValues, processId)) {
|
|
11892
11926
|
return;
|
|
@@ -11917,7 +11951,13 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11917
11951
|
processId,
|
|
11918
11952
|
(node2, partialOutputs, index) => this.#emitter.emit("partialOutput", { node: node2, outputs: partialOutputs, index, processId })
|
|
11919
11953
|
);
|
|
11920
|
-
if (((_a = output["
|
|
11954
|
+
if (((_a = output["requestTokens"]) == null ? void 0 : _a.type) === "number") {
|
|
11955
|
+
this.#totalRequestTokens += coerceTypeOptional(output["requestTokens"], "number") ?? 0;
|
|
11956
|
+
}
|
|
11957
|
+
if (((_b = output["responseTokens"]) == null ? void 0 : _b.type) === "number") {
|
|
11958
|
+
this.#totalResponseTokens += coerceTypeOptional(output["responseTokens"], "number") ?? 0;
|
|
11959
|
+
}
|
|
11960
|
+
if (((_c = output["cost"]) == null ? void 0 : _c.type) === "number") {
|
|
11921
11961
|
this.#totalCost += coerceTypeOptional(output["cost"], "number") ?? 0;
|
|
11922
11962
|
}
|
|
11923
11963
|
results.push({ type: "output", output });
|
|
@@ -11928,7 +11968,7 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11928
11968
|
} else {
|
|
11929
11969
|
results = await Promise.all(
|
|
11930
11970
|
(0, import_lodash_es13.range)(0, splittingAmount).map(async (i) => {
|
|
11931
|
-
var _a2;
|
|
11971
|
+
var _a2, _b2, _c2;
|
|
11932
11972
|
const inputs = fromEntries(
|
|
11933
11973
|
entries(inputValues).map(([port, value]) => [
|
|
11934
11974
|
port,
|
|
@@ -11943,7 +11983,13 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11943
11983
|
processId,
|
|
11944
11984
|
(node2, partialOutputs, index) => this.#emitter.emit("partialOutput", { node: node2, outputs: partialOutputs, index, processId })
|
|
11945
11985
|
);
|
|
11946
|
-
if (((_a2 = output["
|
|
11986
|
+
if (((_a2 = output["requestTokens"]) == null ? void 0 : _a2.type) === "number") {
|
|
11987
|
+
this.#totalRequestTokens += coerceTypeOptional(output["requestTokens"], "number") ?? 0;
|
|
11988
|
+
}
|
|
11989
|
+
if (((_b2 = output["responseTokens"]) == null ? void 0 : _b2.type) === "number") {
|
|
11990
|
+
this.#totalResponseTokens += coerceTypeOptional(output["responseTokens"], "number") ?? 0;
|
|
11991
|
+
}
|
|
11992
|
+
if (((_c2 = output["cost"]) == null ? void 0 : _c2.type) === "number") {
|
|
11947
11993
|
this.#totalCost += coerceTypeOptional(output["cost"], "number") ?? 0;
|
|
11948
11994
|
}
|
|
11949
11995
|
return { type: "output", output };
|
|
@@ -11969,6 +12015,14 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11969
12015
|
}, {});
|
|
11970
12016
|
this.#nodeResults.set(node.id, aggregateResults);
|
|
11971
12017
|
this.#visitedNodes.add(node.id);
|
|
12018
|
+
this.#totalRequestTokens += (0, import_lodash_es13.sum)(results.map((r) => {
|
|
12019
|
+
var _a2;
|
|
12020
|
+
return coerceTypeOptional((_a2 = r.output) == null ? void 0 : _a2["requestTokens"], "number") ?? 0;
|
|
12021
|
+
}));
|
|
12022
|
+
this.#totalResponseTokens += (0, import_lodash_es13.sum)(results.map((r) => {
|
|
12023
|
+
var _a2;
|
|
12024
|
+
return coerceTypeOptional((_a2 = r.output) == null ? void 0 : _a2["responseTokens"], "number") ?? 0;
|
|
12025
|
+
}));
|
|
11972
12026
|
this.#totalCost += (0, import_lodash_es13.sum)(results.map((r) => {
|
|
11973
12027
|
var _a2;
|
|
11974
12028
|
return coerceTypeOptional((_a2 = r.output) == null ? void 0 : _a2["cost"], "number") ?? 0;
|
|
@@ -11979,7 +12033,7 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11979
12033
|
}
|
|
11980
12034
|
}
|
|
11981
12035
|
async #processNormalNode(node, processId) {
|
|
11982
|
-
var _a;
|
|
12036
|
+
var _a, _b, _c;
|
|
11983
12037
|
const inputValues = this.#getInputValuesForNode(node);
|
|
11984
12038
|
if (this.#excludedDueToControlFlow(node, inputValues, processId)) {
|
|
11985
12039
|
return;
|
|
@@ -11995,7 +12049,13 @@ var GraphProcessor = class _GraphProcessor {
|
|
|
11995
12049
|
);
|
|
11996
12050
|
this.#nodeResults.set(node.id, outputValues);
|
|
11997
12051
|
this.#visitedNodes.add(node.id);
|
|
11998
|
-
if (((_a = outputValues["
|
|
12052
|
+
if (((_a = outputValues["requestTokens"]) == null ? void 0 : _a.type) === "number") {
|
|
12053
|
+
this.#totalRequestTokens += coerceTypeOptional(outputValues["requestTokens"], "number") ?? 0;
|
|
12054
|
+
}
|
|
12055
|
+
if (((_b = outputValues["responseTokens"]) == null ? void 0 : _b.type) === "number") {
|
|
12056
|
+
this.#totalResponseTokens += coerceTypeOptional(outputValues["responseTokens"], "number") ?? 0;
|
|
12057
|
+
}
|
|
12058
|
+
if (((_c = outputValues["cost"]) == null ? void 0 : _c.type) === "number") {
|
|
11999
12059
|
this.#totalCost += coerceTypeOptional(outputValues["cost"], "number") ?? 0;
|
|
12000
12060
|
}
|
|
12001
12061
|
this.#emitter.emit("nodeFinish", { node, outputs: outputValues, processId });
|