@axflow/models
Version:
Zero-dependency, modular SDK for building robust natural language applications
79 lines (78 loc) • 2.22 kB
JavaScript
// src/ollama/generation.ts
import { HttpError, POST } from "@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 POST(url, {
headers: options.headers || {},
body,
fetch: options.fetch,
signal: options.signal
});
if (!response.body) {
throw new 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) });
}
};
export {
OllamaGeneration
};