Skip to content

Commit

Permalink
Add configurable timeout to OpenAI requests
Browse files Browse the repository at this point in the history
The default is 2 minutes, but it can be configured with the
`GptAgent` application environment `:receive_timeout_ms`.
  • Loading branch information
jwilger committed Feb 3, 2024
1 parent 963eb7f commit 7459eb5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
32 changes: 24 additions & 8 deletions lib/gpt_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ defmodule GptAgent do

defp retrieve_current_run_status(%__MODULE__{} = state) do
{:ok, %{body: %{"object" => "list", "data" => runs}}} =
OpenAiClient.get("/v1/threads/#{state.thread_id}/runs?limit=1&order=desc")
OpenAiClient.get("/v1/threads/#{state.thread_id}/runs?limit=1&order=desc",
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

case runs do
[%{"id" => run_id, "status" => status} | _rest]
Expand All @@ -119,7 +121,8 @@ defmodule GptAgent do
OpenAiClient.post("/v1/threads/#{state.thread_id}/runs",
json: %{
"assistant_id" => state.assistant_id
}
},
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

Process.send_after(self(), {:check_run_status, id}, heartbeat_interval_ms())
Expand Down Expand Up @@ -160,7 +163,9 @@ defmodule GptAgent do
end

log("Reading messages with request to #{url}")
{:ok, %{body: %{"object" => "list", "data" => messages}}} = OpenAiClient.get(url)

{:ok, %{body: %{"object" => "list", "data" => messages}}} =
OpenAiClient.get(url, receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms))

state
|> process_messages(messages)
Expand Down Expand Up @@ -240,7 +245,10 @@ defmodule GptAgent do
log("Adding user message #{inspect(message)}")

{:ok, %{body: %{"id" => id}}} =
OpenAiClient.post("/v1/threads/#{state.thread_id}/messages", json: message)
OpenAiClient.post("/v1/threads/#{state.thread_id}/messages",
json: message,
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

state
|> publish_event(
Expand Down Expand Up @@ -310,7 +318,8 @@ defmodule GptAgent do

{:ok, %{body: %{"object" => "thread.run", "cancelled_at" => nil, "failed_at" => nil}}} =
OpenAiClient.post("/v1/threads/#{state.thread_id}/runs/#{state.run_id}/submit_tool_outputs",
json: %{tool_outputs: state.tool_outputs}
json: %{tool_outputs: state.tool_outputs},
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

Process.send_after(self(), {:check_run_status, state.run_id}, heartbeat_interval_ms())
Expand Down Expand Up @@ -338,7 +347,9 @@ defmodule GptAgent do
log("Checking run status for run ID #{inspect(id)}")

{:ok, %{body: %{"status" => status} = response}} =
OpenAiClient.get("/v1/threads/#{state.thread_id}/runs/#{id}", [])
OpenAiClient.get("/v1/threads/#{state.thread_id}/runs/#{id}",
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

handle_run_status(status, id, response, state)
end
Expand Down Expand Up @@ -413,7 +424,10 @@ defmodule GptAgent do
log("Creating thread")

{:ok, %{body: %{"id" => thread_id, "object" => "thread"}}} =
OpenAiClient.post("/v1/threads", json: "")
OpenAiClient.post("/v1/threads",
json: "",
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
)

log("Created thread with ID #{inspect(thread_id)}")

Expand Down Expand Up @@ -522,7 +536,9 @@ defmodule GptAgent do
timeout_ms: timeout_ms || default_timeout_ms()
)

case OpenAiClient.get("/v1/threads/#{thread_id}") do
case OpenAiClient.get("/v1/threads/#{thread_id}",
receive_timeout: Application.get_env(:gpt_agent, :receive_timeout_ms)
) do
{:ok, %{status: 404}} ->
log("Thread ID #{inspect(thread_id)} not found")
{:error, :invalid_thread_id}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule GptAgent.MixProject do
def project do
[
app: :gpt_agent,
version: "6.0.0",
version: "6.0.1",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
aliases: aliases(),
Expand Down

0 comments on commit 7459eb5

Please sign in to comment.