adamrushy / OpenAISwift

This is a wrapper library around the ChatGPT and OpenAI HTTP API
MIT License
1.6k stars 242 forks source link

Total Usage #55

Closed thebarbican19 closed 1 year ago

thebarbican19 commented 1 year ago

It would be great to be able to manage the user's usage to limit any potential overage costs to the developer. Below is the struct needed to decode the usage object sent back from the API and a quick, maybe messy approach to storing the usage overtime.

Regardless of the second part, the first part alone would be super useful. Thanks.

struct Usage:Codable {
        var prompt:Int
        var completion:Int
        var total:Int

        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)

            self.prompt = try values.decode(Int.self, forKey: .prompt)
            self.total = try values.decode(Int.self, forKey: .total)
            self.completion = try values.decode(Int.self, forKey: .completion)

            self.storeLocalUsage(usage:self)

        }

        enum CodingKeys: String, CodingKey {
            case prompt = "prompt_tokens"
            case completion = "completion_tokens"
            case total = "total_tokens"

        }

        func storeLocalUsage(usage:Usage) {
            var list = UserDefaults().string(forKey: "gpt_usage").components(separatedBy: ",") {
                list.append(String(usage.completion))

                UserDefaults().set(list.lazy.joined(separator: ","), forKey: "gpt_usage")
                UserDefaults().synchronize()

            }

        }

}
julianschiavo commented 1 year ago

46 (merged) added support for receiving info on token usage, and I think storing usage to UserDefaults should be an app-level decision, IMO.

thebarbican19 commented 1 year ago

Yeah, that makes sense. Thanks for adding it.