@axflow/models
Version:
Zero-dependency, modular SDK for building robust natural language applications
144 lines (140 loc) • 4.41 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/azure-openai/chat.ts
var chat_exports = {};
__export(chat_exports, {
AzureOpenAIChat: () => AzureOpenAIChat,
headers: () => headers
});
module.exports = __toCommonJS(chat_exports);
var import_shared = require("@axflow/models/shared");
// src/openai/shared.ts
function streamTransformer(map) {
let buffer = [];
const decoder = new TextDecoder();
return (bytes, controller) => {
const chunk = decoder.decode(bytes);
for (let i = 0, len = chunk.length; i < len; ++i) {
const isChunkSeparator = chunk[i] === "\n" && buffer[buffer.length - 1] === "\n";
if (!isChunkSeparator) {
buffer.push(chunk[i]);
continue;
}
const parsedChunk = parseChunk(buffer.join(""));
if (parsedChunk) {
controller.enqueue(map(parsedChunk));
}
buffer = [];
}
};
}
var DATA_RE = /data:\s*(.+)/;
function parseChunk(chunk) {
chunk = chunk.trim();
if (chunk.length === 0) {
return null;
}
const match = chunk.match(DATA_RE);
try {
const data = match[1];
return data === "[DONE]" ? null : JSON.parse(data);
} catch (error) {
throw new Error(
`Encountered unexpected chunk while parsing OpenAI streaming response: ${JSON.stringify(
chunk
)}`
);
}
}
// src/azure-openai/chat.ts
function headers(apiKey, customHeaders) {
const headers2 = {
accept: "application/json",
"content-type": "application/json",
...customHeaders
};
if (typeof apiKey === "string") {
headers2["api-key"] = apiKey;
}
return headers2;
}
var API_VERSION = "2023-08-01-preview";
var createUrl = (apiUrl) => {
if (typeof apiUrl === "string") {
return apiUrl;
} else {
return `https://${apiUrl.resourceName}.openai.azure.com/openai/deployments/${apiUrl.deploymentId}/chat/completions?api-version=${API_VERSION}`;
}
};
async function run(request, options) {
const url = createUrl(options.apiUrl);
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey, options.headers),
body: JSON.stringify({ ...request, stream: false }),
fetch: options.fetch,
signal: options.signal
});
return response.json();
}
async function streamBytes(request, options) {
const url = createUrl(options.apiUrl);
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey, options.headers),
body: JSON.stringify({ ...request, stream: true }),
fetch: options.fetch,
signal: options.signal
});
if (!response.body) {
throw new import_shared.HttpError("Expected response body to be a ReadableStream", response);
}
return response.body;
}
function noop(chunk) {
return chunk;
}
async function stream(request, options) {
const byteStream = await streamBytes(request, options);
return byteStream.pipeThrough(new OpenAIChatDecoderStream(noop));
}
async function streamTokens(request, options) {
const byteStream = await streamBytes(request, options);
return byteStream.pipeThrough(new OpenAIChatDecoderStream(chunkToToken));
}
var OpenAIChatDecoderStream = class extends TransformStream {
constructor(map) {
super({ transform: streamTransformer(map) });
}
};
function chunkToToken(chunk) {
if (!chunk.choices || chunk.choices.length === 0) {
return "";
}
return chunk.choices[0].delta.content || "";
}
var AzureOpenAIChat = class {
static run = run;
static streamBytes = streamBytes;
static stream = stream;
static streamTokens = streamTokens;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
AzureOpenAIChat,
headers
});
;