mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-29 16:54:59 +00:00
roughed in cache; elide full dirnames from test results to make them deterministic
This commit is contained in:
parent
2eeee67ee5
commit
0aa2ff2cf9
2 changed files with 21 additions and 3 deletions
|
@ -4,6 +4,7 @@ import json
|
||||||
import backoff
|
import backoff
|
||||||
import openai
|
import openai
|
||||||
import requests
|
import requests
|
||||||
|
from diskcache import Cache
|
||||||
from openai.error import (
|
from openai.error import (
|
||||||
APIConnectionError,
|
APIConnectionError,
|
||||||
APIError,
|
APIError,
|
||||||
|
@ -12,6 +13,9 @@ from openai.error import (
|
||||||
Timeout,
|
Timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CACHE_PATH = ".aider.send.cache.v1"
|
||||||
|
CACHE = Cache(CACHE_PATH)
|
||||||
|
|
||||||
|
|
||||||
@backoff.on_exception(
|
@backoff.on_exception(
|
||||||
backoff.expo,
|
backoff.expo,
|
||||||
|
@ -44,10 +48,22 @@ def send_with_retries(model, messages, functions, stream):
|
||||||
if hasattr(openai, "api_engine"):
|
if hasattr(openai, "api_engine"):
|
||||||
kwargs["engine"] = openai.api_engine
|
kwargs["engine"] = openai.api_engine
|
||||||
|
|
||||||
|
key = json.dumps(kwargs, sort_keys=True).encode()
|
||||||
|
|
||||||
# Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes
|
# Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes
|
||||||
hash_object = hashlib.sha1(json.dumps(kwargs, sort_keys=True).encode())
|
hash_object = hashlib.sha1(key)
|
||||||
|
|
||||||
|
if not stream and key in CACHE:
|
||||||
|
print("hit", key)
|
||||||
|
return hash_object, CACHE[key]
|
||||||
|
|
||||||
|
print("miss", key)
|
||||||
|
|
||||||
res = openai.ChatCompletion.create(**kwargs)
|
res = openai.ChatCompletion.create(**kwargs)
|
||||||
|
|
||||||
|
if not stream:
|
||||||
|
CACHE[key] = res
|
||||||
|
|
||||||
return hash_object, res
|
return hash_object, res
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -657,7 +657,7 @@ def run_unit_tests(testdir, history_fname):
|
||||||
|
|
||||||
success = result.returncode == 0
|
success = result.returncode == 0
|
||||||
res = result.stdout
|
res = result.stdout
|
||||||
res = cleanup_test_output(res)
|
res = cleanup_test_output(res, testdir)
|
||||||
|
|
||||||
with history_fname.open("a") as fh:
|
with history_fname.open("a") as fh:
|
||||||
fh.write(f"```\n{res}\n```")
|
fh.write(f"```\n{res}\n```")
|
||||||
|
@ -667,7 +667,7 @@ def run_unit_tests(testdir, history_fname):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def cleanup_test_output(output):
|
def cleanup_test_output(output, testdir):
|
||||||
# remove timing info, to avoid randomizing the response to GPT
|
# remove timing info, to avoid randomizing the response to GPT
|
||||||
res = re.sub(
|
res = re.sub(
|
||||||
r"^Ran \d+ tests in \d+\.\d+s$",
|
r"^Ran \d+ tests in \d+\.\d+s$",
|
||||||
|
@ -687,6 +687,8 @@ def cleanup_test_output(output):
|
||||||
res,
|
res,
|
||||||
flags=re.MULTILINE,
|
flags=re.MULTILINE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
res = res.replace(str(testdir), str(testdir.name))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue