Skip to content

Instantly share code, notes, and snippets.

@devxpy
Last active August 5, 2024 09:56
Show Gist options
  • Save devxpy/7588bae4026e116e65e3c4b5f0568557 to your computer and use it in GitHub Desktop.
Save devxpy/7588bae4026e116e65e3c4b5f0568557 to your computer and use it in GitHub Desktop.
let response = await fetch("https://api.gooey.ai/v3/integrations/stream/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
// your integration's ID as shown in the Gooey.AI Integrations tab
"integration_id": "DEy",
// the input text for the bot
"input_prompt": "Hello, world!",
// pass the custom user id here
"user_id": "<my-custom-id>",
"variables": { "ais_prompt": "<>" }
}),
});
// get the server-sent events URL
let sseUrl = response.headers.get("Location");
console.log(sseUrl);
// clear screen
document.body.innerHTML = "";
// start listening to the stream
const evtSource = new EventSource(sseUrl);
// handle the stream events
evtSource.onmessage = (event) => {
// display the message in the browser
document.body.innerHTML += event.data + "<br><br>";
// parse the message as JSON
let data = JSON.parse(event.data);
// log the message to the console
console.log(data.type, data);
// check if the message is the final response
if (data.type === "final_response") {
// close the stream
evtSource.close();
}
};
evtSource.onerror = (event) => {
// log the error to the console
console.error(event.data);
// close the stream
evtSource.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment