View Source OpenAi.Client.Stream (OpenAI REST API Client v0.4.4)

Module for transforming streaming responses.

When passing a stream parameter to any operation that supports streaming, like:

Additional parameter stream_to should be passed to specify which process should handle the stream. Generally, it may be the calling process, like self().

This module provides functions to transform the stream of events into structured data. The operation that supports streaming will return a stream of transformed events. They can be processed by a consumer, like a Phoenix channel.

Example:

OpenAi.Assistants.create_run(%OpenAi.Run.CreateRequest{
  stream: true,
  ...
}, stream_to: self())
|> Stream.each(fn
  {:ok, event} ->
    # Handle event
  {:error, error} ->
    # Handle error
end)

Event will have a structure like:

%{
  event: "thread.message.delta",
  data: %OpenAi.Message.Delta{}
}