Skip to content

Commit

Permalink
fix(core): polyfill Web Stream APIs (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Apr 28, 2024
1 parent 1ab3ba4 commit dccb816
Show file tree
Hide file tree
Showing 26 changed files with 488 additions and 996 deletions.
18 changes: 16 additions & 2 deletions examples/agent/step_wise_openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FunctionTool, OpenAIAgent } from "llamaindex";
import { ReadableStream } from "node:stream/web";

// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }) {
Expand Down Expand Up @@ -69,11 +70,24 @@ async function main() {
for await (const stepOutput of task) {
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
console.log(stepOutput.output.message.content);
const output = stepOutput.output;
if (output instanceof ReadableStream) {
for await (const chunk of output) {
process.stdout.write(chunk.delta);
}
} else {
console.log(output);
}
console.log(`==========================`);

if (stepOutput.isLast) {
console.log(stepOutput.output.message.content);
if (stepOutput.output instanceof ReadableStream) {
for await (const chunk of stepOutput.output) {
process.stdout.write(chunk.delta);
}
} else {
console.log(stepOutput.output);
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/agent/stream_openai_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ async function main() {
stream: true,
});

for await (const chunk of stream.response) {
process.stdout.write(chunk.response);
console.log("Response:");

for await (const {
response: { delta },
} of stream) {
process.stdout.write(delta);
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/agent/wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ async function main() {
stream: true,
});

for await (const chunk of response.response) {
process.stdout.write(chunk.response);
for await (const {
response: { delta },
} of response) {
process.stdout.write(delta);
}
}

Expand Down
1 change: 1 addition & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"strict": true,
"skipLibCheck": true,
"lib": ["ES2022"],
"types": ["node"],
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"incremental": true,
Expand Down
Loading

0 comments on commit dccb816

Please sign in to comment.