Skip to content

Instantly share code, notes, and snippets.

@nicolay-r
Last active February 17, 2025 14:44
Show Gist options
  • Save nicolay-r/86fc212086c0955d541244253ec0564b to your computer and use it in GitHub Desktop.
Save nicolay-r/86fc212086c0955d541244253ec0564b to your computer and use it in GitHub Desktop.
No-string streaming mode for Replicate LLM models in Javascript
// This is a customized version of the original script:
// https://til.simonwillison.net/llms/streaming-llm-apis
// Adapted for Replicate Streaming API documentation:
// https://replicate.com/docs/topics/predictions/streaming
async function* sseStreamIterator(apiUrl, requestBody, extraHeaders) {
// POST.
let response = await fetch(apiUrl, {
method: 'POST',
headers: { ...{'Content-Type': 'application/json'}, ...(extraHeaders || {}) },
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Await for the body once received the premise.
const body = await response.json();
response = await fetch(body.urls.stream, {
headers: {...{"Accept": "text/event-stream"}, ...(extraHeaders || {}) },
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// NOTE: This a TextDecoder based implemenataion.
// In the case of working with UI can use parser API for handling it via events:
// https://www.npmjs.com/package/eventsource-parser
const reader = response.body.getReader();
const decoder = new TextDecoder();
while(true) {
const {value, done} = await reader.read();
if (done) break;
yield decoder.decode(value, { stream: true });
}
}
async function handleSSE() {
const apiUrl = 'https://api.replicate.com/v1/models/meta/meta-llama-3-70b-instruct/predictions';
const requestBody = {
input: {
prompt: "What's the color of the sky?",
stream: true // Important to mention for sending instruction on fetching data after POST.
},
};
for await (const event of sseStreamIterator(apiUrl, requestBody, {
"Authorization": "Bearer <API-GOES-HERE>"
})) {
console.log(event);
}
}
handleSSE()
@nicolay-r
Copy link
Author

nicolay-r commented Feb 14, 2025

To parse the raw requests, the simpliest way is to apply parser here by using eventsource-parser package:
https://www.npmjs.com/package/eventsource-parser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment