-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(inputs): allow stream processing of inputs (#37)
-off-by: Tomas Pilar <tomas.pilar@ibm.com>
- Loading branch information
1 parent
1b110e5
commit 16ec0ca
Showing
5 changed files
with
127 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
import { | ||
BaseError, | ||
InvalidInputError as InvalidInputSDKError, | ||
} from "@ibm-generative-ai/node-sdk"; | ||
|
||
export class InvalidInputError extends Error {} | ||
import { BaseError, InvalidInputError } from "@ibm-generative-ai/node-sdk"; | ||
|
||
export const isUsageError = (err) => | ||
!(err instanceof BaseError) || | ||
err instanceof InvalidInputSDKError || | ||
err instanceof InvalidInputError; | ||
!(err instanceof BaseError) || err instanceof InvalidInputError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import { pipeline } from "stream/promises"; | ||
import { compose } from "node:stream"; | ||
|
||
import Parser from "stream-json/Parser.js"; | ||
import StreamValues from "stream-json/streamers/StreamValues.js"; | ||
import Parser from "stream-json/jsonl/Parser.js"; | ||
|
||
import { InvalidInputError } from "../errors.js"; | ||
import { parseInput } from "./parsers.js"; | ||
|
||
export const readJSONStream = async (stream) => { | ||
const values = []; | ||
try { | ||
await pipeline( | ||
stream, | ||
new Parser({ jsonStreaming: true }), | ||
new StreamValues(), | ||
async function (source) { | ||
for await (const value of source) { | ||
values.push(value.value); | ||
} | ||
export const createInputStream = (stream) => | ||
compose( | ||
stream, | ||
new Parser({ | ||
checkErrors: true, | ||
}), | ||
async function* (source) { | ||
for await (const value of source) { | ||
yield parseInput(value.value); | ||
} | ||
); | ||
} catch (err) { | ||
throw new InvalidInputError( | ||
"Failed to parse JSON stream, please check your input", | ||
{ cause: err } | ||
); | ||
} | ||
return values; | ||
}; | ||
} | ||
); | ||
|
||
export function createBatchTransform({ batchSize }) { | ||
if (batchSize < 1) throw new Error("Batch size must be positive"); | ||
|
||
return async function* (source) { | ||
let batch = []; | ||
for await (const item of source) { | ||
if (batch.length >= batchSize) { | ||
yield batch; | ||
batch = []; | ||
} | ||
batch.push(item); | ||
} | ||
if (batch.length > 0) yield batch; | ||
}; | ||
} |