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:
OpenAi.Assistants.submit_tool_outputs_to_run/4
OpenAi.Assistants.create_thread_and_run/2
OpenAi.Assistants.create_run/2
OpenAi.Chat.create_chat_completion/2
OpenAi.Completions.create_completion/2
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{}
}