Simplify OpenAI api implementation

This commit is contained in:
Claudio Ortolina
2026-03-02 13:56:46 +00:00
parent e0530bb039
commit 5bd85013c6
4 changed files with 37 additions and 105 deletions
+1 -12
View File
@@ -2,18 +2,7 @@ defmodule OpenAI do
alias OpenAI.API
def gpt(completion) do
{:ok, collector} = Agent.start_link(fn -> "" end)
API.gpt_stream(completion, api_key(), 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}
API.gpt(completion, api_key())
end
def chat_stream(messages, opts \\ []) when is_list(messages) do