Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
888 B
10
Indexable
Never
    public static async Task<string> RequestChatCompletionWithRetry(ChatCompletionMessage[] messages)
    {

        int maxRetries = 6;
        float initialDelay = 3.0f;

        float delay = initialDelay;
        for (int i = 0; i < maxRetries; i++)
        {
            try
            {
                return await OpenAIAccessManager.RequestChatCompletion(messages);
            }
            catch (Exception ex)
            {
                Debug.LogWarning($"Error in RequestChatCompletion call, attempt {i + 1}: {ex.Message}");
                if (i < maxRetries - 1) // Don't delay if we've reached the max retries
                {
                    await Task.Delay(TimeSpan.FromSeconds(delay));
                    delay += initialDelay;
                }
            }
        }
        throw new Exception("Max retries reached for RequestChatCompletion call");
    }