9dce1cacb9
Note that it's not possible to replace any call where we encode and pretty print, as JSON doesn't have such functionality.
19 lines
462 B
Elixir
19 lines
462 B
Elixir
defmodule OpenAI do
|
|
alias OpenAI.API
|
|
|
|
def gpt(completion) do
|
|
{:ok, collector} = Agent.start_link(fn -> "" end)
|
|
|
|
API.gpt_stream(completion, fn data ->
|
|
case get_in(data, ["choices", Access.at(0), "delta", "content"]) do
|
|
nil -> :ok
|
|
data -> Agent.update(collector, fn current -> current <> data end)
|
|
end
|
|
end)
|
|
|
|
result = Agent.get(collector, & &1) |> JSON.decode!()
|
|
Agent.stop(collector)
|
|
{:ok, result}
|
|
end
|
|
end
|