dvcrn / ex_openai

Auto-generated Elixir SDK for OpenAI APIs with full typespecs, docs and streaming support
MIT License
71 stars 27 forks source link

Allow specifying of callback functions for streams #12

Closed dvcrn closed 8 months ago

dvcrn commented 8 months ago

Instead of spawning a new process, simplify handling of streams a bit with a callback function:

callback = fn
    :finish -> IO.puts "Done"
    {:data, data} -> IO.puts "Data: #{inspect(data)}"
    {:error, err} -> IO.puts "Error: #{inspect(err)}"
end

ExOpenAI.Completions.create_completion "text-davinci-003", "hello world", stream: true, stream_to: callback

Inspired by finch