Merge branch 'main' into watch

This commit is contained in:
Paul Gauthier 2024-10-28 15:13:20 -07:00
commit 34f34879c9
3 changed files with 53 additions and 32 deletions

View file

@ -1,5 +1,6 @@
import hashlib import hashlib
import json import json
import time
import backoff import backoff
@ -18,26 +19,32 @@ RETRY_TIMEOUT = 60
def retry_exceptions(): def retry_exceptions():
import httpx import httpx
import openai
return ( return (
# httpx # httpx
httpx.ConnectError, httpx.ConnectError,
httpx.RemoteProtocolError, httpx.RemoteProtocolError,
httpx.ReadTimeout, httpx.ReadTimeout,
# litellm #
litellm.exceptions.BadRequestError, # litellm exceptions inherit from openai exceptions
litellm.exceptions.AuthenticationError, # https://docs.litellm.ai/docs/exception_mapping
litellm.exceptions.PermissionDeniedError, #
litellm.exceptions.NotFoundError, # openai.BadRequestError,
litellm.exceptions.UnprocessableEntityError, # litellm.ContextWindowExceededError,
litellm.exceptions.RateLimitError, # litellm.ContentPolicyViolationError,
litellm.exceptions.InternalServerError, #
litellm.exceptions.ContextWindowExceededError, # openai.AuthenticationError,
litellm.exceptions.ContentPolicyViolationError, # openai.PermissionDeniedError,
litellm.exceptions.APIConnectionError, # openai.NotFoundError,
litellm.exceptions.APIError, #
litellm.exceptions.ServiceUnavailableError, openai.APITimeoutError,
litellm.exceptions.Timeout, openai.UnprocessableEntityError,
openai.RateLimitError,
openai.APIConnectionError,
openai.APIError,
openai.APIStatusError,
openai.InternalServerError,
) )
@ -64,8 +71,6 @@ def send_completion(
temperature=0, temperature=0,
extra_params=None, extra_params=None,
): ):
from aider.llm import litellm
kwargs = dict( kwargs = dict(
model=model_name, model=model_name,
messages=messages, messages=messages,
@ -98,18 +103,27 @@ def send_completion(
return hash_object, res return hash_object, res
@lazy_litellm_retry_decorator
def simple_send_with_retries(model_name, messages, extra_params=None): def simple_send_with_retries(model_name, messages, extra_params=None):
try: retry_delay = 0.125
kwargs = { while True:
"model_name": model_name, try:
"messages": messages, kwargs = {
"functions": None, "model_name": model_name,
"stream": False, "messages": messages,
"extra_params": extra_params, "functions": None,
} "stream": False,
"extra_params": extra_params,
}
_hash, response = send_completion(**kwargs) _hash, response = send_completion(**kwargs)
return response.choices[0].message.content return response.choices[0].message.content
except (AttributeError, litellm.exceptions.BadRequestError): except retry_exceptions() as err:
return print(str(err))
retry_delay *= 2
if retry_delay > RETRY_TIMEOUT:
break
print(f"Retrying in {retry_delay:.1f} seconds...")
time.sleep(retry_delay)
continue
except AttributeError:
return

View file

@ -55,6 +55,7 @@ model_list = "\n".join(f"- {model}" for model in sorted(prefill_models))
cog.out(model_list) cog.out(model_list)
]]]--> ]]]-->
- anthropic.claude-3-5-sonnet-20241022-v2:0
- anthropic/claude-3-5-sonnet-20241022 - anthropic/claude-3-5-sonnet-20241022
- claude-3-5-sonnet-20240620 - claude-3-5-sonnet-20240620
- claude-3-5-sonnet-20241022 - claude-3-5-sonnet-20241022
@ -65,6 +66,7 @@ cog.out(model_list)
- codestral/codestral-latest - codestral/codestral-latest
- deepseek-chat - deepseek-chat
- deepseek-coder - deepseek-coder
- eu.anthropic.claude-3-5-sonnet-20241022-v2:0
- mistral/codestral-2405 - mistral/codestral-2405
- mistral/codestral-latest - mistral/codestral-latest
- mistral/codestral-mamba-latest - mistral/codestral-mamba-latest
@ -85,6 +87,7 @@ cog.out(model_list)
- mistral/open-mixtral-8x7b - mistral/open-mixtral-8x7b
- mistral/pixtral-12b-2409 - mistral/pixtral-12b-2409
- openrouter/anthropic/claude-3.5-sonnet - openrouter/anthropic/claude-3.5-sonnet
- us.anthropic.claude-3-5-sonnet-20241022-v2:0
- vertex_ai/claude-3-5-sonnet-v2@20241022 - vertex_ai/claude-3-5-sonnet-v2@20241022
- vertex_ai/claude-3-5-sonnet@20240620 - vertex_ai/claude-3-5-sonnet@20240620
- vertex_ai/claude-3-haiku@20240307 - vertex_ai/claude-3-haiku@20240307

View file

@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch
import httpx import httpx
from aider.llm import litellm from aider.llm import litellm
from aider.sendchat import simple_send_with_retries from aider.sendchat import retry_exceptions, simple_send_with_retries
class PrintCalled(Exception): class PrintCalled(Exception):
@ -12,6 +12,10 @@ class PrintCalled(Exception):
class TestSendChat(unittest.TestCase): class TestSendChat(unittest.TestCase):
def test_retry_exceptions(self):
"""Test that retry_exceptions() can be called without raising errors"""
retry_exceptions() # Should not raise any exceptions
@patch("litellm.completion") @patch("litellm.completion")
@patch("builtins.print") @patch("builtins.print")
def test_simple_send_with_retries_rate_limit_error(self, mock_print, mock_completion): def test_simple_send_with_retries_rate_limit_error(self, mock_print, mock_completion):
@ -31,7 +35,7 @@ class TestSendChat(unittest.TestCase):
# Call the simple_send_with_retries method # Call the simple_send_with_retries method
simple_send_with_retries("model", ["message"]) simple_send_with_retries("model", ["message"])
mock_print.assert_called_once() assert mock_print.call_count == 2
@patch("litellm.completion") @patch("litellm.completion")
@patch("builtins.print") @patch("builtins.print")
@ -44,4 +48,4 @@ class TestSendChat(unittest.TestCase):
# Call the simple_send_with_retries method # Call the simple_send_with_retries method
simple_send_with_retries("model", ["message"]) simple_send_with_retries("model", ["message"])
mock_print.assert_called_once() assert mock_print.call_count == 2