mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 22:34:59 +00:00
80 lines
1.9 KiB
Python
80 lines
1.9 KiB
Python
import hashlib
|
|
import json
|
|
|
|
import backoff
|
|
import httpx
|
|
import openai
|
|
|
|
# from diskcache import Cache
|
|
from openai import APIConnectionError, InternalServerError, RateLimitError
|
|
|
|
from aider.dump import dump # noqa: F401
|
|
from aider.litellm import litellm
|
|
|
|
CACHE_PATH = "~/.aider.send.cache.v1"
|
|
CACHE = None
|
|
# CACHE = Cache(CACHE_PATH)
|
|
|
|
|
|
def should_giveup(e):
|
|
if not hasattr(e, "status_code"):
|
|
return False
|
|
|
|
return not litellm._should_retry(e.status_code)
|
|
|
|
|
|
@backoff.on_exception(
|
|
backoff.expo,
|
|
(
|
|
InternalServerError,
|
|
RateLimitError,
|
|
APIConnectionError,
|
|
httpx.ConnectError,
|
|
httpx.RemoteProtocolError,
|
|
litellm.exceptions.ServiceUnavailableError,
|
|
),
|
|
giveup=should_giveup,
|
|
max_time=60,
|
|
on_backoff=lambda details: print(
|
|
f"{details.get('exception','Exception')}\nRetry in {details['wait']:.1f} seconds."
|
|
),
|
|
)
|
|
def send_with_retries(model_name, messages, functions, stream):
|
|
kwargs = dict(
|
|
model=model_name,
|
|
messages=messages,
|
|
temperature=0,
|
|
stream=stream,
|
|
)
|
|
if functions is not None:
|
|
kwargs["functions"] = functions
|
|
|
|
key = json.dumps(kwargs, sort_keys=True).encode()
|
|
|
|
# Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes
|
|
hash_object = hashlib.sha1(key)
|
|
|
|
if not stream and CACHE is not None and key in CACHE:
|
|
return hash_object, CACHE[key]
|
|
|
|
# del kwargs['stream']
|
|
|
|
res = litellm.completion(**kwargs)
|
|
|
|
if not stream and CACHE is not None:
|
|
CACHE[key] = res
|
|
|
|
return hash_object, res
|
|
|
|
|
|
def simple_send_with_retries(model_name, messages):
|
|
try:
|
|
_hash, response = send_with_retries(
|
|
model_name=model_name,
|
|
messages=messages,
|
|
functions=None,
|
|
stream=False,
|
|
)
|
|
return response.choices[0].message.content
|
|
except (AttributeError, openai.BadRequestError):
|
|
return
|