页面 · Laravilt
Streaming
Stream AI responses in PHP and consume the server-sent events endpoint.
Realtime callback
streamChatRealtime() calls your callback for each chunk as the provider sends it:
Generator
streamChat() returns a generator of chunks:
Some providers (such as OpenAI) collect the full response before yielding from
streamChat(). UsestreamChatRealtime()when you need output as it arrives.
The stream endpoint
POST /laravilt-ai/stream accepts:
| Field | Type | Description |
|---|---|---|
messages | array | role (system, user, assistant, tool) and content |
provider | string | Optional provider name |
model | string | Optional model |
session_id | string | Optional session to save the exchange to |
mentioned_resources | array | Optional resource slugs to focus on |
It responds with text/event-stream. Each event is a JSON object with content (or error), and the stream ends with data: [DONE]:
Before streaming, the controller lets the model call the resource tools (list_resources, query_resource) and then streams the final answer.
Consuming the stream
The endpoint is a POST, so read it with fetch rather than EventSource:
The useAI() composable (Vue and React) wraps this for you; see Frontend components.