Skip to content

Fix crash when first message exceeds max token length. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tylerstillwater
Copy link

@tylerstillwater tylerstillwater commented Apr 10, 2023

During encoding, we check to see if the latest text to be encoded and sent to ChatGPT exceeds our maximum token length of 4096. If it does, we throw an error, as this message can never function correctly.

@@ -57,7 +57,9 @@ public class ChatGPTAPI: @unchecked Sendable {
private func generateMessages(from text: String, systemText: String) -> [Message] {
var messages = [systemMessage(content: systemText)] + historyList + [Message(role: "user", content: text)]
if gptEncoder.encode(text: messages.content).count > 4096 {
if !historyList.isEmpty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isEmpty is a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Working on this now. I'm very new to Swift and made this edit in a github codespace. This solution doesn't work anyway. Something more complex is required.

@tylerstillwater
Copy link
Author

I've pushed a commit that correctly fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants