@axflow/models
Advanced tools
Comparing version 0.0.1-alpha.5 to 0.0.1-alpha.6
@@ -13,3 +13,14 @@ declare class HttpError extends Error { | ||
declare function StreamToIterable<T>(stream: ReadableStream<T>): AsyncIterable<T>; | ||
type JSONValueType = null | string | number | boolean | { | ||
[x: string]: JSONValueType; | ||
} | Array<JSONValueType>; | ||
declare class NdJsonStream { | ||
static headers: Readonly<{ | ||
'content-type': "application/x-ndjson"; | ||
}>; | ||
static from<T extends { | ||
[x: string]: JSONValueType; | ||
}>(stream: ReadableStream<T>, data?: Record<string, JSONValueType> | Record<string, JSONValueType>[]): ReadableStream<Uint8Array>; | ||
} | ||
export { HttpError, POST, StreamToIterable, isHttpError }; | ||
export { HttpError, NdJsonStream, POST, StreamToIterable, isHttpError }; |
@@ -24,2 +24,3 @@ "use strict"; | ||
HttpError: () => HttpError, | ||
NdJsonStream: () => NdJsonStream, | ||
POST: () => POST, | ||
@@ -78,5 +79,69 @@ StreamToIterable: () => StreamToIterable, | ||
} | ||
var NdJsonStream = class { | ||
static headers = Object.freeze({ "content-type": "application/x-ndjson" }); | ||
/** | ||
* Converts a stream of JSON-serializable objects to newline-delimited JSON. | ||
* | ||
* Each object is wrapped with an object that specifies the `type` and references | ||
* the `value`. The `type` is one of `chunk` or `data`. A type of `chunk` means that | ||
* the `value` corresponds to chunks from the input stream. A type of `data` means | ||
* that the `value` corresponds to the additional data provided as the second argument | ||
* to this function. | ||
* | ||
* | ||
* Example WITHOUT additional data: | ||
* | ||
* const chunk = { key: 'value' }; | ||
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }}); | ||
* const ndJsonStream = NdJsonStream.from(stream); | ||
* const entries = []; | ||
* for await (const chunk of stream) { | ||
* entry.push(new TextDecoder().decode(chunk)); | ||
* } | ||
* console.log(entries); // [ "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ] | ||
* | ||
* | ||
* Example WITH additional data: | ||
* | ||
* const chunk = { key: 'value' }; | ||
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }}); | ||
* const ndJsonStream = NdJsonStream.from(stream, [{ extra: 'data' }]); | ||
* const entries = []; | ||
* for await (const chunk of stream) { | ||
* entry.push(new TextDecoder().decode(chunk)); | ||
* } | ||
* console.log(entries); // [ "{\"type\":\"data\",\"value\":{\"extra\":\"data\"}}\n", "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ] | ||
* | ||
* | ||
* @param stream A readable stream of JSON-serializable chunks to encode as ndjson | ||
* @param data Optional, additional data to prepend to the output stream | ||
* @returns A readable stream of newline-delimited JSON | ||
*/ | ||
static from(stream, data) { | ||
const encoder = new TextEncoder(); | ||
function serialize(obj) { | ||
const serialized = JSON.stringify(obj); | ||
return encoder.encode(`${serialized} | ||
`); | ||
} | ||
const ndJsonEncode = new TransformStream({ | ||
start(controller) { | ||
if (!data) { | ||
return; | ||
} | ||
for (const entry of Array.isArray(data) ? data : [data]) { | ||
controller.enqueue(serialize({ type: "data", value: entry })); | ||
} | ||
}, | ||
transform(chunk, controller) { | ||
controller.enqueue(serialize({ type: "chunk", value: chunk })); | ||
} | ||
}); | ||
return stream.pipeThrough(ndJsonEncode); | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
HttpError, | ||
NdJsonStream, | ||
POST, | ||
@@ -83,0 +148,0 @@ StreamToIterable, |
{ | ||
"name": "@axflow/models", | ||
"version": "0.0.1-alpha.5", | ||
"version": "0.0.1-alpha.6", | ||
"description": "Zero-dependency module to run, stream, and render results across the most popular LLMs and embedding models", | ||
@@ -123,3 +123,3 @@ "author": "Axilla (https://axilla.io)", | ||
}, | ||
"gitHead": "df6d224ca97e68b5c0e2856a00f5a4a5329fe372" | ||
"gitHead": "b929430e216d68aa9367ed03acc3f0314890c72e" | ||
} |
@@ -180,3 +180,3 @@ # @axflow/models | ||
```ts | ||
import {StreamToIterable, HttpError, isHttpError} from '@axflow/models/anthropic/completion'; | ||
import {StreamToIterable, NdJsonStream, HttpError, isHttpError} from '@axflow/models/anthropic/completion'; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
74307
1618