
let MODEL = 'llama3:8b';
//let MODEL = 'gpt-oss:20b';
//let MODEL = 'qwen2.5:7b-instruct';
//let MODEL = 'mistral:instruct';
// let MODEL = 'yi:34b';
//let MODEL = 'gemma2:9b';

async function askStream(prompt) {
  const res = await fetch('http://127.0.0.1:11434/api/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      model: MODEL,
      messages: [
        { role: 'system', content: 'Всегда веди рассуждения и отвечай на том же языке, на котором задан вопрос.' },
        { role: 'user', content: prompt },
      ],
      stream: true
    })
  });

  console.log(prompt);
  console.log('----------------');

  if (!res.ok) throw new Error(await res.text());

  const reader = res.body.getReader();
  const decoder = new TextDecoder();

  let content = '';
  let thinking = '';

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    const chunk = decoder.decode(value, { stream: true }).trim();
    if (!chunk) continue;

    try {
      const obj = JSON.parse(chunk);
      console.error(JSON.stringify(obj, null, 2));

      if (obj.message?.thinking) {
        thinking += obj.message?.thinking;
        if (thinking.length > 100) {
          console.log(thinking.trim());
          thinking = '';
        }
      }

      if (obj.message?.content) {
        if (thinking) {
          console.log(thinking.trim());
          console.log('--------------');
          thinking = '';
        }

        content += obj.message?.content;
        if (content.endsWith('\n')) {
          console.log(content.trim());
          content = '';
        }
        if (content.length > 100) {
          let b = content.substring(0, 100);
          let e = content.substring(101);
          console.log(content.trim());
          content = '';
        }
      }

    } catch (err) {
      console.error('Failed to parse chunk:', chunk, err);
    }
  }
  
  // console.log(thinking);
  console.log(content.trim());

}

//askStream('Hello');
askStream('Сколько в мире котов и кошек?');
askStream('Найди вэб ссылки на материалы');
