Stream handling for LLM API responses.
Provides functions to:
- Start a streaming request
- Receive individual chunks
- Collect all chunks into a final response
- Auto-execute tool calls and loop
Usage
{:ok, stream} = LLM.stream("Hello", provider: :openai, model: "gpt-4")
{:ok, response} = LLM.Stream.collect(stream, on_chunk: &IO.write/1)
Summary
Functions
Collect all chunks into a final response, executing tool calls automatically.
Receive the next chunk from the stream.
Start a streaming request. Returns {:ok, stream} or {:error, reason}.
Types
@type chunk_type() :: LLM.Stream.Chunk.t() | LLM.Stream.ToolCall.t() | LLM.Stream.Thinking.t() | LLM.Stream.Stop.t() | LLM.Stream.Error.t()
@type t() :: %LLM.Stream{ buffer: String.t(), context: LLM.Context.t(), dialect: module(), done: boolean(), headers: [{String.t(), String.t()}], opts: keyword(), provider: map(), ref: Req.Response.t(), rounds: non_neg_integer(), url: String.t() }
Functions
@spec collect( t(), keyword() ) :: {:ok, LLM.Response.t()} | {:error, term()}
Collect all chunks into a final response, executing tool calls automatically.
Options
:auto_tools— auto-execute tool calls (default:true):max_rounds— max tool call rounds (default:10):on_chunk— callback for each chunk,fn chunk -> ... end
@spec next(t()) :: {:ok, [chunk_type()], t()} | {:halt, t()} | {:error, term()}
Receive the next chunk from the stream.
Returns:
{:ok, [chunk], stream}— one or more chunks received{:halt, stream}— stream ended{:error, reason}— error occurred
@spec start( LLM.Context.t(), keyword() ) :: {:ok, t()} | {:error, term()}
Start a streaming request. Returns {:ok, stream} or {:error, reason}.