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
```
This commit is contained in:
Paul Gauthier 2023-05-19 10:41:43 -07:00
parent af85c81c13
commit 32e40a3e1c

View file

@ -477,6 +477,10 @@ class Coder:
return context
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 = [
@ -484,11 +488,15 @@ class Coder:
dict(role="user", content=context + diffs),
]
commit_message, interrupted = self.send(
messages,
model="gpt-3.5-turbo",
silent=True,
)
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
commit_message = commit_message.strip().strip('"').strip()