Skip to content

Instantly share code, notes, and snippets.

@rayanfer32
Created May 2, 2024 11:44
Show Gist options
  • Save rayanfer32/966d91b0be8faef000a2eeae50de70b4 to your computer and use it in GitHub Desktop.
Save rayanfer32/966d91b0be8faef000a2eeae50de70b4 to your computer and use it in GitHub Desktop.
Read the ollama response as a stream
const axios = require("axios");
const BASE_URL = "http://localhost:11434/api/generate";
axios({
method: "post",
url: BASE_URL,
data: {
"model": "llama3",
"prompt": "Tell me a joke"
},
responseType: "stream",
}).then((response) => {
const reader = new TextDecoder();
response.data.on("data", (chunk) => {
const text = reader.decode(chunk);
let json = JSON.parse(text);
process.stdout.write(json.response);
});
}).catch((error) => {
console.log(error);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment