jvcgpt / src /utils /beam.ts
Greums's picture
major improvements to the app
a417977
raw
history blame
1.28 kB
import {Settings} from "@/contexts/settings";
import {LogAction} from "@/contexts/log";
export async function* streamAPI(
prompt: string,
settings: Settings,
log: LogAction,
): AsyncGenerator<string> {
log(`Fetching ${settings.postCount} posts from Beam API…`);
const response = await fetch(new URL(settings.apiURL), {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "text/event-stream",
"Authorization": `Bearer ${settings.apiKey}`
},
body: JSON.stringify({
"prompt": prompt,
"posts_count": settings.postCount,
"temperature": settings.temperature,
}),
});
if (!response.ok) {
const message = `Failed to fetch API (${response.status} ${response.statusText}): ${await response.text()}`;
log(message);
throw new Error(message);
}
log("Reading HTTP stream…");
try {
const decoder = new TextDecoder();
for await (const chunk of response.body) {
const text = decoder.decode(chunk, { stream: true });
yield text;
}
log("Stream reading completed.");
} catch(e) {
log("Error while reading the HTTP stream.")
}
}