MK-LLM-Mistral / examples /client_js.mjs
ainow-mk's picture
Upload 65 files
f29d474 verified
const BASE_URL = process.env.OPENAI_BASE_URL || 'http://localhost:8000/v1';
const API_KEY = process.env.OPENAI_API_KEY || 'dummy';
async function chatStream() {
const resp = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: JSON.stringify({
model: 'mk-llm',
messages: [
{ role: 'system', content: 'Ти си помошник кој зборува на македонски.' },
{ role: 'user', content: 'Која е историјата на Охрид?' },
],
stream: true,
}),
});
const reader = resp.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
for (const line of chunk.split('\n')) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
console.log(data);
}
}
}
}
chatStream();