@axflow/models
Version:
Zero-dependency, modular SDK for building robust natural language applications
104 lines (102 loc) • 3.28 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/ollama/generation.ts
var generation_exports = {};
__export(generation_exports, {
OllamaGeneration: () => OllamaGeneration
});
module.exports = __toCommonJS(generation_exports);
var import_shared = require("@axflow/models/shared");
var OLLAMA_URL = "http://127.0.0.1:11434/api/generate";
async function streamBytes(request, options) {
const url = options.apiUrl || OLLAMA_URL;
const body = JSON.stringify(request);
const response = await (0, import_shared.POST)(url, {
headers: options.headers || {},
body,
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;
}
function chunkToToken(chunk) {
if (!chunk.done) {
return chunk.response;
} else {
return "";
}
}
async function stream(request, options) {
const byteStream = await streamBytes(request, options);
return byteStream.pipeThrough(new OllamaDecoderStream(noop));
}
async function streamTokens(request, options) {
const byteStream = await streamBytes(request, options);
return byteStream.pipeThrough(new OllamaDecoderStream(chunkToToken));
}
var OllamaGeneration = class {
static streamBytes = streamBytes;
static stream = stream;
static streamTokens = streamTokens;
};
var OllamaDecoderStream = class _OllamaDecoderStream extends TransformStream {
static parseChunk(line) {
line = line.trim();
if (line.length === 0) {
return null;
}
try {
return JSON.parse(line);
} catch (e) {
throw new Error(`Malformed streaming data from Ollama: ${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 isSeparator = chunk[i] === "\n";
if (!isSeparator) {
buffer.push(chunk[i]);
continue;
}
const parsedChunk = _OllamaDecoderStream.parseChunk(buffer.join(""));
if (parsedChunk) {
controller.enqueue(map(parsedChunk));
}
buffer = [];
}
};
}
constructor(map) {
super({ transform: _OllamaDecoderStream.transformer(map) });
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OllamaGeneration
});
;