Repository navigation
Expand file tree
/
Copy pathsendchat.py
More file actions
68 lines (54 loc) · 1.62 KB
/
Copy pathsendchat.py
File metadata and controls
68 lines (54 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import hashlib
import json
import backoff
import httpx
import litellm
import openai
# from diskcache import Cache
from openai import APIConnectionError, InternalServerError, RateLimitError
from aider.dump import dump # noqa: F401
CACHE_PATH = "~/.aider.send.cache.v1"
CACHE = None
# CACHE = Cache(CACHE_PATH)
@backoff.on_exception(
backoff.expo,
(
InternalServerError,
RateLimitError,
APIConnectionError,
httpx.ConnectError,
),
max_tries=10,
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]
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