From 64e16c8ad17b78555309e070675e1d7b50f30096 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 27 Jun 2023 14:59:58 -0700 Subject: [PATCH] aider: Added SHA1 hash of kwargs to `chat_completion_call_hashes` in `send_with_retries` method. # Context: USER: when calling `ChatCompletion`, take a sha1 hash of dict with all the args and append it to a list self.chat_completion_call_hashes ASSISTANT: To implement this change, we need to modify the `send_with_retries` method in the `Coder` class in the `aider/coders/base_coder.py` file. We will import the `hashlib` module to generate the SHA1 hash. We will also add a new attribute `chat_completion_call_hashes` to the `Coder` class to store the hashes. Here are the changes: ```python aider/coders/base_coder.py <<<<<<< ORIGINAL import json import os import sys import traceback from json.decoder import JSONDecodeError from pathlib import Path ======= import hashlib import json import os import sys import traceback from json.decoder import JSONDecodeError from pathlib import Path >>>>>>> UPDATED ``` ```python aider/coders/base_coder.py <<<<<<< ORIGINAL class Coder: abs_fnames = None repo = None last_aider_commit_hash = None last_asked_for_commit_time = 0 repo_map = None functions = None total_cost = 0.0 ======= class Coder: abs_fnames = None repo = None last_aider_commit_hash = None last_asked_for_commit_time = 0 repo_map = None functions = None total_cost = 0.0 chat_completion_call_hashes = [] >>>>>>> UPDATED ``` ```python aider/coders/base_coder.py <<<<<<< ORIGINAL def send_with_retries(self, model, messages, functions): kwargs = dict( model=model, messages=messages, temperature=0, stream=self.stream, ) if functions is not None: kwargs["functions"] = self.functions res = openai.ChatCompletion.create(**kwargs) return res ======= def send_with_retries(self, model, messages, functions): kwargs = dict( model=model, messages=messages, temperature=0, stream=self.stream, ) if functions is not None: kwargs["functions"] = self.functions # Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes hash_object = hashlib.sha1(json.dumps(kwargs, sort_keys=True).encode()) self.chat_completion_call_hashes.append(hash_object.hexdigest()) res = openai.ChatCompletion.create(**kwargs) return res >>>>>>> UPDATED ``` --- aider/coders/base_coder.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index da998bfcb..6a899ab8e 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import hashlib import json import os import sys @@ -43,6 +44,7 @@ class Coder: repo_map = None functions = None total_cost = 0.0 + chat_completion_call_hashes = [] @classmethod def create( @@ -555,6 +557,10 @@ class Coder: if functions is not None: kwargs["functions"] = self.functions + # Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes + hash_object = hashlib.sha1(json.dumps(kwargs, sort_keys=True).encode()) + self.chat_completion_call_hashes.append(hash_object.hexdigest()) + res = openai.ChatCompletion.create(**kwargs) return res