Created
May 2, 2024 11:44
-
-
Save rayanfer32/966d91b0be8faef000a2eeae50de70b4 to your computer and use it in GitHub Desktop.
Read the ollama response as a stream
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
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