@axflow/models
Version:
Zero-dependency, modular SDK for building robust natural language applications
127 lines (123 loc) • 4.03 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/cohere/generation.ts
var generation_exports = {};
__export(generation_exports, {
CohereGeneration: () => CohereGeneration
});
module.exports = __toCommonJS(generation_exports);
var import_shared = require("@axflow/models/shared");
// src/cohere/shared.ts
function headers(apiKey, customHeaders) {
const headers2 = {
accept: "application/json",
"content-type": "application/json",
...customHeaders
};
if (typeof apiKey === "string") {
headers2.authorization = `Bearer ${apiKey}`;
}
return headers2;
}
// src/cohere/generation.ts
var COHERE_API_URL = "https://api.cohere.ai/v1/generate";
async function run(request, options) {
const url = options.apiUrl || COHERE_API_URL;
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 = options.apiUrl || COHERE_API_URL;
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 CohereGenerationDecoderStream(noop));
}
function chunkToToken(chunk) {
return chunk.text || "";
}
async function streamTokens(request, options) {
const byteStream = await streamBytes(request, options);
return byteStream.pipeThrough(new CohereGenerationDecoderStream(chunkToToken));
}
var CohereGeneration = class {
static run = run;
static stream = stream;
static streamBytes = streamBytes;
static streamTokens = streamTokens;
};
var CohereGenerationDecoderStream = class _CohereGenerationDecoderStream extends TransformStream {
static parse(line) {
line = line.trim();
if (line.length === 0) {
return null;
}
try {
return JSON.parse(line);
} catch (error) {
throw new Error(
`Invalid event: expected well-formed event lines but got ${JSON.stringify(line)}`
);
}
}
static transformer(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 isEventSeparator = chunk[i] === "\n";
if (!isEventSeparator) {
buffer.push(chunk[i]);
continue;
}
const event = _CohereGenerationDecoderStream.parse(buffer.join(""));
if (event) {
controller.enqueue(map(event));
}
buffer = [];
}
};
}
constructor(map) {
super({ transform: _CohereGenerationDecoderStream.transformer(map) });
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CohereGeneration
});
;