Skip to content

Streamowanie umożliwia renderowanie tekstu w miarę jego generowania przez model.

Streamowanie jest domyślnie włączone za pośrednictwem interfejsu REST API, ale domyślnie wyłączone w SDK.

Aby włączyć streamowanie w SDK, ustaw parametr stream na wartość True.

Podstawowe koncepcje streamowania ​

  1. Czat: Streamuj częściowe wiadomości asystenta. Każdy fragment zawiera pole content, dzięki czemu możesz renderować wiadomości w miarę ich przybywania.
  2. Myślenie: Modele obsługujące funkcję myślenia emitują pole thinking obok zwykłej zawartości w każdym fragmencie. Wykrywaj to pole w streamowanych fragmentach, aby wyświetlać lub ukrywać ślady rozumowania przed otrzymaniem ostatecznej odpowiedzi.
  3. Wywoływanie narzędzi: Obserwuj streamowane pola tool_calls w każdym fragmencie, wykonuj żądane narzędzie i dołączaj wyniki działania narzędzia z powrotem do konwersji.

Obsługa streamowanych fragmentów ​

Uwaga

Należy akumulować częściowe pola, aby zachować historię konwersji. Jest to szczególnie ważne przy wywoływaniu narzędzi, gdzie myślenie, wywołanie narzędzia przez model oraz wynik wykonania narzędzia muszą zostać przekazane z powrotem do modelu w następnym żądaniu.

Python ​

```python
from ollama import chat

stream = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
  stream=True,
)

in_thinking = False
content = ''
thinking = ''
for chunk in stream:
  if chunk.message.thinking:
    if not in_thinking:
      in_thinking = True
      print('Thinking:\n', end='', flush=True)
    print(chunk.message.thinking, end='', flush=True)
    # akumuluj częściowe myślenie
    thinking += chunk.message.thinking
  elif chunk.message.content:
    if in_thinking:
      in_thinking = False
      print('\n\nAnswer:\n', end='', flush=True)
    print(chunk.message.content, end='', flush=True)
    # akumuluj częściową zawartość
    content += chunk.message.content

  # dołącz akumulowane pola do wiadomości na potrzeby następnego żądania
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
```

JavaScript ​

```javascript
import ollama from 'ollama'

async function main() {
  const stream = await ollama.chat({
    model: 'qwen3',
    messages: [{ role: 'user', content: 'What is 17 × 23?' }],
    stream: true,
  })

  let inThinking = false
  let content = ''
  let thinking = ''

  for await (const chunk of stream) {
    if (chunk.message.thinking) {
      if (!inThinking) {
        inThinking = true
        process.stdout.write('Thinking:\n')
      }
      process.stdout.write(chunk.message.thinking)
      // akumuluj częściowe myślenie
      thinking += chunk.message.thinking
    } else if (chunk.message.content) {
      if (inThinking) {
        inThinking = false
        process.stdout.write('\n\nAnswer:\n')
      }
      process.stdout.write(chunk.message.content)
      // akumuluj częściową zawartość
      content += chunk.message.content
    }
  }

  // dołącz akumulowane pola do wiadomości na potrzeby następnego żądania
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
}

main().catch(console.error)
```