Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Apr 17, 2024
1 parent 1c01e20 commit 02072c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
6 changes: 1 addition & 5 deletions packages/ai/examples/promise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { createClient } from "edgedb";
import { createAI } from "../src/index.js";

const client = createClient({
instanceName: "_localdev",
database: "main",
tlsSecurity: "insecure",
});
const client = createClient();

const gpt4Ai = createAI(client, {
model: "gpt-4-turbo-preview",
Expand Down
25 changes: 12 additions & 13 deletions packages/ai/examples/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import http from "node:http";
import { createClient } from "edgedb";
import { createAI } from "../src/index.js";

const client = createClient({
instanceName: "_localdev",
database: "main",
tlsSecurity: "insecure",
});
const client = createClient();

const gpt4Ai = createAI(client, {
model: "gpt-4-turbo-preview",
Expand Down Expand Up @@ -43,10 +39,12 @@ const server = http.createServer(async (req, res) => {
res.writeHead(200, { "Content-Type": "text/event-stream" });

const reader = streamResponse.body.getReader();
streamReaderToResponse(reader, res);
await streamReaderToResponse(reader, res);
} catch (error) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(`Error: ${error.message}`);
if (!res.headersSent) {
res.writeHead(500, { "Content-Type": "text/plain" });
}
res.end(`Error: ${error}`);
}
});

Expand All @@ -55,16 +53,17 @@ server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

function streamReaderToResponse(
async function streamReaderToResponse(
reader: ReadableStreamDefaultReader,
res: http.ServerResponse
) {
reader.read().then(({ done, value }) => {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) {
res.end();
return;
break;
}
res.write(value);
streamReaderToResponse(reader, res);
});
}
}

0 comments on commit 02072c8

Please sign in to comment.