Skip to content

Swift/RangeReplaceableCollection.swift:622: Fatal error: Can't remove first element from an empty collection #30

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
itamargilvybe opened this issue Nov 19, 2023 · 2 comments

Comments

@itamargilvybe
Copy link

AnalyticsView - I call the function try await vm.analyseWeek()

AnswersAnalyticsVM:

private let api = ChatGPTAPI(apiKey: Constants.Keys.chatgpt)
private let gptModel = "gpt-3.5-turbo"

func analyseWeek() async throws {
        do {
            let weekAnswers = try await collectAllWeekAnswers() // Fetching the data to input ChatGPT

            let analysisString = try await api.sendMessage(text: weekAnswers, model: gptModel)
            
            if let userid = await Session.shared.user_id {
                // Saving output to DB
            } else {
                throw CustomError.sessionError
            }
            
        } catch let err{
            throw err
        }
    }

App crashes when I call api.sendMessage() with the message "Swift/RangeReplaceableCollection.swift:622: Fatal error: Can't remove first element from an empty collection".

XCode crash trickle-down:

public func sendMessage(text: String,
                            model: String = ChatGPTAPI.Constants.defaultModel,
                            systemText: String = ChatGPTAPI.Constants.defaultSystemText,
                            temperature: Double = ChatGPTAPI.Constants.defaultTemperature) async throws -> String {
        var urlRequest = self.urlRequest
        urlRequest.httpBody = try jsonBody(text: text, model: model, systemText: systemText, temperature: temperature, stream: false) // Here
        ...

private func json```
Body(text: String, model: String, systemText: String, temperature: Double, stream: Bool = true) throws -> Data {
let request = Request(model: model,
temperature: temperature,
messages: generateMessages(from: text, systemText: systemText),
stream: stream) // Here
return try JSONEncoder().encode(request)
}


private func generateMessages(fro```
m text: String, systemText: String) -> [Message] {
        var messages = [systemMessage(content: systemText)] + historyList + [Message(role: "user", content: text)]
        if gptEncoder.encode(text: messages.content).count > 4096  {
            _ = historyList.removeFirst()
            messages = generateMessages(from: text, systemText: systemText) // Here
        }
        return messages
    }

Why does it happen? I think it's a bug.

@IhorLeshko
Copy link

Same here, any solutions?

@conradobh
Copy link

same here

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

No branches or pull requests

3 participants