aider/aider
Paul Gauthier 32e40a3e1c aider: Prevent gpt-3.5-turbo from generating commit messages for diffs larger than 4k tokens and catch InvalidRequestError.
# Context:
USER: `Coder.get_commit_message()` uses gpt-3.5-turbo to summarize the diffs into a sensible commit message. If the diff is large, it exceeds the 4K context window.

A simple fix would be:

1. Check if the diff is close to or larger than 4 kbytes (4k tokens * 4 bytes/token) and don't even bother sending the messages to gpt. Just return.
2. Catch the `openai.error.InvalidRequestError` and just return.

Maybe print a nice tool_error() message for each of these.

ASSISTANT: To implement the suggested changes, we will modify the `get_commit_message()` method in the `aider/coder.py` file. Here are the step-by-step changes:

1. Check if the length of `diffs` is close to or larger than 4 kbytes (4k tokens * 4 bytes/token). If it is, print an error message and return.
2. Catch the `openai.error.InvalidRequestError` exception when calling `self.send()` and print an error message, then return.

Here are the code changes:

```python
aider/coder.py
<<<<<<< ORIGINAL
    def get_commit_message(self, diffs, context):
        diffs = "# Diffs:\n" + diffs

        messages = [
            dict(role="system", content=prompts.commit_system),
            dict(role="user", content=context + diffs),
        ]

        commit_message, interrupted = self.send(
            messages,
            model="gpt-3.5-turbo",
            silent=True,
        )
=======
    def get_commit_message(self, diffs, context):
        if len(diffs) >= 4 * 1024 * 4:
            self.io.tool_error("Diff is too large for gpt-3.5-turbo to generate a commit message.")
            return

        diffs = "# Diffs:\n" + diffs

        messages = [
            dict(role="system", content=prompts.commit_system),
            dict(role="user", content=context + diffs),
        ]

        try:
            commit_message, interrupted = self.send(
                messages,
                model="gpt-3.5-turbo",
                silent=True,
            )
        except openai.error.InvalidRequestError:
            self.io.tool_error("Failed to generate commit message using gpt-3.5-turbo due to an invalid request.")
            return
>>>>>>> UPDATED
```
2023-05-19 10:41:43 -07:00
..
__init__.py initial 2023-05-09 11:00:32 -07:00
coder.py aider: Prevent gpt-3.5-turbo from generating commit messages for diffs larger than 4k tokens and catch InvalidRequestError. 2023-05-19 10:41:43 -07:00
commands.py aider: Updated cmd_run method in aider/commands.py to use the io version of Confirm.ask(). 2023-05-19 10:18:49 -07:00
ctags.py fixed missing case of just the filename 2023-05-19 10:32:55 -07:00
dump.py black formatting 2023-05-17 18:24:35 -07:00
io.py fixed indent 2023-05-18 17:33:11 -07:00
main.py aider: Added --ctags argument and environment variable to pass to Coder class. 2023-05-19 10:02:21 -07:00
prompts.py implement --ctags 2023-05-19 10:07:52 -07:00
utils.py make line numbers optional 2023-05-18 10:04:13 -07:00