File size: 1,283 Bytes
a417977 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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.")
}
} |