Skip to content

Commit 850fe4f

Browse files
committed
simplify text streaming in sdks
1 parent ac24dd4 commit 850fe4f

15 files changed

Lines changed: 142 additions & 95 deletions

File tree

docs/model-api/docs/task/audio-text-to-text.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ mode: 'wide'
242242

243243
let text = '';
244244

245-
for await (const chunk of readStream) {
246-
const chunkText = chunk.toString();
247-
console.log(chunkText);
248-
text += chunkText;
245+
for await (const tokens of readStream) {
246+
text += tokens;
247+
248+
console.log(tokens);
249249
}
250250

251251
// observe the output
@@ -286,9 +286,9 @@ mode: 'wide'
286286

287287
text = ""
288288

289-
for chunk in readStream:
290-
tokens = chunk.decode("utf-8")
289+
for tokens in readStream:
291290
text += tokens
291+
292292
print(tokens)
293293

294294
# observe the output
@@ -362,10 +362,10 @@ mode: 'wide'
362362

363363
let text = '';
364364

365-
for await (const chunk of readStream) {
366-
const chunkText = chunk.toString();
367-
console.log(chunkText);
368-
text += chunkText;
365+
for await (const tokens of readStream) {
366+
text += tokens;
367+
368+
console.log(tokens);
369369
}
370370

371371
// observe the output
@@ -411,9 +411,9 @@ mode: 'wide'
411411

412412
text = ""
413413

414-
for chunk in readStream:
415-
tokens = chunk.decode("utf-8")
414+
for tokens in readStream:
416415
text += tokens
416+
417417
print(tokens)
418418

419419
# observe the output

docs/model-api/docs/task/chat.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ mode: 'wide'
235235

236236
let text = '';
237237

238-
for await (const chunk of readStream) {
239-
const chunkText = chunk.toString();
240-
console.log(chunkText);
241-
text += chunkText;
238+
for await (const tokens of readStream) {
239+
text += tokens;
240+
241+
console.log(tokens);
242242
}
243243

244244
// observe the output
@@ -278,9 +278,9 @@ mode: 'wide'
278278

279279
text = ""
280280

281-
for chunk in readStream:
282-
tokens = chunk.decode("utf-8")
281+
for tokens in readStream:
283282
text += tokens
283+
284284
print(tokens)
285285

286286
# observe the output
@@ -352,10 +352,10 @@ mode: 'wide'
352352
353353
let text = '';
354354
355-
for await (const chunk of readStream) {
356-
const chunkText = chunk.toString();
357-
console.log(chunkText);
358-
text += chunkText;
355+
for await (const tokens of readStream) {
356+
text += tokens;
357+
358+
console.log(tokens);
359359
}
360360
361361
// observe the output
@@ -400,9 +400,9 @@ mode: 'wide'
400400
401401
text = ""
402402
403-
for chunk in readStream:
404-
tokens = chunk.decode("utf-8")
403+
for tokens in readStream:
405404
text += tokens
405+
406406
print(tokens)
407407
408408
# observe the output

docs/model-api/docs/task/image-text-to-text.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ mode: 'wide'
242242

243243
let text = '';
244244

245-
for await (const chunk of readStream) {
246-
const chunkText = chunk.toString();
247-
console.log(chunkText);
248-
text += chunkText;
245+
for await (const tokens of readStream) {
246+
text += tokens;
247+
248+
console.log(tokens);
249249
}
250250

251251
// observe the output
@@ -286,9 +286,9 @@ mode: 'wide'
286286

287287
text = ""
288288

289-
for chunk in readStream:
290-
tokens = chunk.decode("utf-8")
289+
for tokens in readStream:
291290
text += tokens
291+
292292
print(tokens)
293293

294294
# observe the output
@@ -362,10 +362,10 @@ mode: 'wide'
362362

363363
let text = '';
364364

365-
for await (const chunk of readStream) {
366-
const chunkText = chunk.toString();
367-
console.log(chunkText);
368-
text += chunkText;
365+
for await (const tokens of readStream) {
366+
text += tokens;
367+
368+
console.log(tokens);
369369
}
370370

371371
// observe the output
@@ -411,9 +411,9 @@ mode: 'wide'
411411

412412
text = ""
413413

414-
for chunk in readStream:
415-
tokens = chunk.decode("utf-8")
414+
for tokens in readStream:
416415
text += tokens
416+
417417
print(tokens)
418418

419419
# observe the output

docs/model-api/docs/task/video-text-to-text.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ mode: 'wide'
242242

243243
let text = '';
244244

245-
for await (const chunk of readStream) {
246-
const chunkText = chunk.toString();
247-
console.log(chunkText);
248-
text += chunkText;
245+
for await (const tokens of readStream) {
246+
text += tokens;
247+
248+
console.log(tokens);
249249
}
250250

251251
// observe the output
@@ -286,9 +286,9 @@ mode: 'wide'
286286

287287
text = ""
288288

289-
for chunk in readStream:
290-
tokens = chunk.decode("utf-8")
289+
for tokens in readStream:
291290
text += tokens
291+
292292
print(tokens)
293293

294294
# observe the output
@@ -362,10 +362,10 @@ mode: 'wide'
362362

363363
let text = '';
364364

365-
for await (const chunk of readStream) {
366-
const chunkText = chunk.toString();
367-
console.log(chunkText);
368-
text += chunkText;
365+
for await (const tokens of readStream) {
366+
text += tokens;
367+
368+
console.log(tokens);
369369
}
370370

371371
// observe the output
@@ -411,9 +411,9 @@ mode: 'wide'
411411

412412
text = ""
413413

414-
for chunk in readStream:
415-
tokens = chunk.decode("utf-8")
414+
for tokens in readStream:
416415
text += tokens
416+
417417
print(tokens)
418418

419419
# observe the output

docs/scripts/generatePages/jsTemplate.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const readStream = await model.run(input, params, stream);
3030

3131
let text = '';
3232

33-
for await (const chunk of readStream) {
34-
const chunkText = chunk.toString();
35-
console.log(chunkText);
36-
text += chunkText;
33+
for await (const tokens of readStream) {
34+
text += tokens;
35+
36+
console.log(tokens);
3737
}
3838

3939
// observe the output

docs/scripts/generatePages/pyTemplate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
text = ""
3232

33-
for chunk in readStream:
34-
tokens = chunk.decode("utf-8")
33+
for tokens in readStream:
3534
text += tokens
35+
3636
print(tokens)
3737

3838
# observe the output

sdk/javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "bytez.js",
33
"author": "Bytez",
44
"license": "ISC",
5-
"version": "1.1.18",
5+
"version": "3.0.0",
66
"type": "module",
77
"packageManager": "yarn@4.9.2",
88
"engines": {

sdk/javascript/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export default class Client {
7474
// in browsers, return readable text stream
7575
// in node, return node readable stream
7676
return this.#isBrowser
77-
? res.body
78-
: this.#Readable.fromWeb(res.body as any);
77+
? res.body.pipeThrough(new TextDecoderStream())
78+
: this.#Readable.fromWeb(res.body as any, { encoding: "utf8" });
7979
} else {
8080
return await res.json();
8181
}

sdk/javascript/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import { Response } from "./interface/Client";
1010
*/
1111
export default class Bytez {
1212
constructor(apiKey: string, dev = false, browser?: boolean) {
13-
this.key = apiKey;
1413
this.#client = new Client(apiKey, dev, browser);
1514
}
1615
#client: Client;
17-
key = "";
1816
list = {
1917
/** Lists available models, and provides basic information about each one, such as RAM required */
2018
models: (options?: ListModels): Promise<Response> =>

sdk/javascript/tests/tasks.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ describe.skip("text generation", async () => {
8383
let testPass = false;
8484
// console.log(textStream);
8585

86-
for await (const chunk of readStream.setEncoding("utf8")) {
87-
// console.log({ chunk });
86+
for await (const tokens of readStream) {
8887
if (testPass === false) {
89-
testPass = typeof chunk === "string";
88+
testPass = typeof tokens === "string";
9089
assert(testPass, "streams output");
9190
}
9291
}
@@ -95,9 +94,9 @@ describe.skip("text generation", async () => {
9594

9695
testPass = false;
9796

98-
for await (const chunk of readStream.setEncoding("utf8")) {
97+
for await (const tokens of readStream) {
9998
if (testPass === false) {
100-
testPass = typeof chunk === "string";
99+
testPass = typeof tokens === "string";
101100
assert(testPass, "streams output");
102101
}
103102
}
@@ -107,13 +106,13 @@ describe.skip("text generation", async () => {
107106
const browserClient = new Bytez(process.env.BYTEZ_KEY ?? "", true, true);
108107
const model = browserClient.model("openai-community/gpt2");
109108
const readStream = await model.run("Jack and jill", true);
110-
const textStream = readStream.pipeThrough(new TextDecoderStream());
109+
111110
let testPass = false;
112111

113-
for await (const chunk of textStream) {
114-
// console.log({ chunk });
112+
for await (const tokens of readStream) {
113+
// console.log({ tokens });
115114
if (testPass === false) {
116-
testPass = typeof chunk === "string";
115+
testPass = typeof tokens === "string";
117116

118117
assert(testPass, "streams output");
119118
}

0 commit comments

Comments
 (0)