-
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.
Merge pull request #36 from polyfact/feat/web-request
feat: add web-request
- 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: "<YOUR_TOKEN>", | ||
}; | ||
|
||
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