Last active
August 5, 2024 09:56
-
-
Save devxpy/7588bae4026e116e65e3c4b5f0568557 to your computer and use it in GitHub Desktop.
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
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