openpipe
Version:
OpenPipe TypeScript SDK: Fine-Tuning, Inference, and Metrics for Production Apps
102 lines (101 loc) • 3.59 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/openai/mergeChunks.ts
var mergeChunks_exports = {};
__export(mergeChunks_exports, {
default: () => mergeChunks
});
module.exports = __toCommonJS(mergeChunks_exports);
var omit = (obj, ...keys) => {
const ret = { ...obj };
for (const key of keys) {
delete ret[key];
}
return ret;
};
function mergeChunks(base, chunk) {
if (base === null) {
return mergeChunks(
{ ...chunk, object: "chat.completion", choices: [], usage: chunk.usage ?? void 0 },
// Prevent function call and tool call arguments from being double-merged
{
...chunk,
choices: chunk.choices.map((c) => ({
...c,
delta: {
...c.delta,
function_call: c.delta.function_call ? {
...c.delta.function_call
} : void 0,
tool_calls: c.delta.tool_calls?.map((tc) => ({
...tc,
function: {
...tc.function
}
}))
}
}))
}
);
}
const choices = [...base.choices];
for (const choice of chunk.choices) {
const baseChoice = choices.find((c) => c.index === choice.index);
if (baseChoice) {
baseChoice.finish_reason = choice.finish_reason ?? baseChoice.finish_reason;
baseChoice.message = { ...baseChoice.message, refusal: null };
if (choice.delta?.content)
baseChoice.message.content = (baseChoice.message.content ?? "") + (choice.delta.content ?? "");
if (choice.delta?.function_call) {
const fnCall = baseChoice.message.function_call ?? {
name: "",
arguments: ""
};
fnCall.name = fnCall.name + (choice.delta.function_call.name ?? "");
fnCall.arguments = fnCall.arguments + (choice.delta.function_call.arguments ?? "");
}
if (choice.delta?.tool_calls) {
const toolCalls = baseChoice.message.tool_calls ?? [];
const toolCallDelta = { ...choice.delta.tool_calls[0] };
if (toolCallDelta?.function?.name) {
toolCalls.push({
id: toolCallDelta.id,
type: "function",
function: {
name: toolCallDelta.function.name ?? "",
arguments: toolCallDelta.function.arguments ?? ""
}
});
} else if (toolCalls[toolCalls.length - 1] && toolCallDelta) {
toolCalls[toolCalls.length - 1].function.arguments += toolCallDelta.function?.arguments ?? "";
}
baseChoice.message.tool_calls = toolCalls;
}
} else {
choices.push({ ...omit(choice, "delta"), message: { role: "assistant", ...choice.delta } });
}
}
const merged = {
...base,
choices,
usage: chunk.usage ?? void 0
};
return merged;
}
//# sourceMappingURL=mergeChunks.cjs.map