-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from gitautoai/wes
Fix: write_pr_body encountered an BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 142586 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}
- Loading branch information
Showing
5 changed files
with
52 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import tiktoken | ||
from config import OPENAI_MAX_CONTEXT_TOKENS, OPENAI_MODEL_ID | ||
from utils.handle_exceptions import handle_exceptions | ||
|
||
|
||
@handle_exceptions(default_return_value="", raise_on_error=False) | ||
def truncate_message(input_message: str) -> str: | ||
encoding: tiktoken.Encoding = tiktoken.encoding_for_model( | ||
model_name=OPENAI_MODEL_ID | ||
) | ||
tokens: list[int] = encoding.encode(text=input_message) | ||
if len(tokens) > OPENAI_MAX_CONTEXT_TOKENS: | ||
tokens = tokens[:OPENAI_MAX_CONTEXT_TOKENS] | ||
truncated_message: str = encoding.decode(tokens=tokens) | ||
return truncated_message |