-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Readable } from "readable-stream"; | ||
import { generateStream, generateWithTokenUsage } from "../lib/generate"; | ||
|
||
const config = { | ||
endpoint: "http://localhost:8080", | ||
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiOGJlOWQwZWYtZGMyZi00ODQzLWE0MjctYmYwNDRlZmJkNTVlIiwiaWF0IjoxNTE2MjM5MDIyfQ.1lULQuZIkRBL9eWPYjsU-x0Am4F0VauW82ZRTiRVyaE", | ||
}; | ||
|
||
function handleStreamData(stream: Readable): Promise<void> { | ||
return new Promise((resolve) => { | ||
let buffer = ""; | ||
|
||
stream.on("data", (data) => { | ||
buffer += data.toString(); | ||
|
||
let lastSpaceIndex = buffer.lastIndexOf(" "); | ||
if (lastSpaceIndex !== -1) { | ||
let partToPrint = buffer.substring(0, lastSpaceIndex); | ||
buffer = buffer.substring(lastSpaceIndex + 1); | ||
process.stdout.write(partToPrint + " "); | ||
} | ||
}); | ||
|
||
stream.on("end", () => { | ||
if (buffer.length) { | ||
process.stdout.write(buffer + "\n"); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
(async () => { | ||
const response = await generateWithTokenUsage( | ||
"When is the next Olympics Games ?", | ||
{ web: true, model: "gpt-4", provider: "openai" }, | ||
config, | ||
); | ||
console.log(response); | ||
|
||
console.log("\n", "-".repeat(80), "\n"); | ||
|
||
const weatherStream = generateStream( | ||
"what is the weather like today in Paris?", | ||
{ web: true }, | ||
config, | ||
); | ||
await handleStreamData(weatherStream); | ||
|
||
console.log("\n", "-".repeat(80), "\n"); | ||
|
||
const websiteSummaryStream = generateStream( | ||
"summarize this website : read:https://www.polyfact.com/", | ||
{ web: true }, | ||
config, | ||
); | ||
await handleStreamData(websiteSummaryStream); | ||
})(); |
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