File size: 1,133 Bytes
f29d474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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();