From 5ca17908b4f687b5c587bf8fa7e55c933ecc0093 Mon Sep 17 00:00:00 2001 From: chrisgreg Date: Fri, 1 Dec 2023 11:35:07 +0000 Subject: [PATCH 1/2] Add retry strategy to OpenAI Chat API requests --- lib/chat_models/chat_open_ai.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/chat_models/chat_open_ai.ex b/lib/chat_models/chat_open_ai.ex index 5dd610e7..a3553cea 100644 --- a/lib/chat_models/chat_open_ai.ex +++ b/lib/chat_models/chat_open_ai.ex @@ -212,6 +212,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do # # Executes the callback function passing the response only parsed to the data # structures. + # Retries the request up to 3 times on transient errors with a 1 second delay @doc false @spec do_api_request(t(), [Message.t()], [Function.t()], (any() -> any())) :: list() | struct() | {:error, String.t()} @@ -221,7 +222,10 @@ defmodule LangChain.ChatModels.ChatOpenAI do url: openai.endpoint, json: for_api(openai, messages, functions), auth: {:bearer, get_api_key()}, - receive_timeout: openai.receive_timeout + receive_timeout: openai.receive_timeout, + retry: :transient, + max_retries: 3, + retry_delay: fn attempt -> :timer.sleep(1000 * attempt) end ) req From a94ecfb05f9d571bc0f0aeff6a9182ead99ea3b8 Mon Sep 17 00:00:00 2001 From: chrisgreg Date: Fri, 1 Dec 2023 11:40:51 +0000 Subject: [PATCH 2/2] Lessen retry delay to 300ms --- lib/chat_models/chat_open_ai.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chat_models/chat_open_ai.ex b/lib/chat_models/chat_open_ai.ex index a3553cea..4d90cd17 100644 --- a/lib/chat_models/chat_open_ai.ex +++ b/lib/chat_models/chat_open_ai.ex @@ -225,7 +225,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do receive_timeout: openai.receive_timeout, retry: :transient, max_retries: 3, - retry_delay: fn attempt -> :timer.sleep(1000 * attempt) end + retry_delay: fn attempt -> :timer.sleep(300 * attempt) end ) req