@axflow/models
Advanced tools
Comparing version 0.0.1-alpha.9 to 0.0.1-y.0
@@ -24,5 +24,9 @@ declare class HttpError extends Error { | ||
}>; | ||
static response<T>(stream: ReadableStream<T>, options?: ResponseInit & { | ||
map?: (value: T) => JSONValueType | Promise<JSONValueType>; | ||
data?: JSONValueType[]; | ||
}): Response; | ||
static encode<T = any>(stream: ReadableStream<T>, options?: { | ||
map?: (value: T) => JSONValueType; | ||
data?: Record<string, JSONValueType>[]; | ||
map?: (value: T) => JSONValueType | Promise<JSONValueType>; | ||
data?: JSONValueType[]; | ||
}): ReadableStream<Uint8Array>; | ||
@@ -29,0 +33,0 @@ static decode(stream: ReadableStream<Uint8Array>): ReadableStream<NdJsonValueType>; |
@@ -81,5 +81,36 @@ "use strict"; | ||
} | ||
var NdJsonStream = class { | ||
var NdJsonStream = class _NdJsonStream { | ||
static headers = Object.freeze({ "content-type": "application/x-ndjson; charset=utf-8" }); | ||
/** | ||
* Returns a streaming newline-delimited JSON `Response` object. | ||
* | ||
* Example: | ||
* | ||
* export async function POST(request: Request) { | ||
* const req = await request.json(); | ||
* const stream = await OpenAIChat.stream(req, { apiKey: process.env.OPENAI_API_KEY! }); | ||
* return NdJsonStream.response(stream, { | ||
* map: (chunk) => chunk.choices[0].delta.content ?? '', | ||
* }); | ||
* } | ||
* | ||
* @param stream A readable stream of chunks to encode as ndjson | ||
* @param options | ||
* @param options.status HTTP response status | ||
* @param options.statusText HTTP response status text | ||
* @param options.headers HTTP response headers | ||
* @param options.map A function to map input chunks to output chunks. The return value must be either a JSON-serializable object or a Promise that resolves to a JSON-serializable object. | ||
* @param options.data Additional data to prepend to the output stream | ||
* @returns A streaming newline-delimited JSON `Response` object | ||
*/ | ||
static response(stream, options) { | ||
options = options ?? {}; | ||
const ndjson = _NdJsonStream.encode(stream, { map: options.map, data: options.data }); | ||
return new Response(ndjson, { | ||
status: options.status, | ||
statusText: options.statusText, | ||
headers: { ...options.headers, ..._NdJsonStream.headers } | ||
}); | ||
} | ||
/** | ||
* Transforms a stream of JSON-serializable objects to stream of newline-delimited JSON. | ||
@@ -118,5 +149,5 @@ * | ||
* | ||
* @param stream A readable stream of JSON-serializable chunks to encode as ndjson | ||
* @param stream A readable stream of chunks to encode as ndjson | ||
* @param options | ||
* @param options.map A function to map stream chunks to desired, json-serializable outputs | ||
* @param options.map A function to map input chunks to output chunks. The return value must be either a JSON-serializable object or a Promise that resolves to a JSON-serializable object | ||
* @param options.data Additional data to prepend to the output stream | ||
@@ -141,4 +172,5 @@ * @returns A readable stream of newline-delimited JSON | ||
}, | ||
transform(value, controller) { | ||
controller.enqueue(serialize({ type: "chunk", value: map(value) })); | ||
async transform(chunk, controller) { | ||
const value = await Promise.resolve(map(chunk)); | ||
controller.enqueue(serialize({ type: "chunk", value })); | ||
} | ||
@@ -145,0 +177,0 @@ }); |
{ | ||
"name": "@axflow/models", | ||
"version": "0.0.1-alpha.9", | ||
"version": "0.0.1-y.0", | ||
"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": "cb659f43473fce49cd3283e3a95d3b947dd683a4" | ||
"gitHead": "9de9a6dd04f2925515996e0020844c6cb5c8ec11" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80201
1754