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()
}
}
}
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.