CountTokensRequest.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  16. struct CountTokensRequest {
  17. let model: String
  18. let contents: [ModelContent]
  19. let options: RequestOptions
  20. }
  21. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  22. extension CountTokensRequest: GenerativeAIRequest {
  23. typealias Response = CountTokensResponse
  24. var url: URL {
  25. URL(string: "\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens")!
  26. }
  27. }
  28. /// The model's response to a count tokens request.
  29. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  30. public struct CountTokensResponse {
  31. /// The total number of tokens in the input given to the model as a prompt.
  32. public let totalTokens: Int
  33. /// The total number of billable characters in the text input given to the model as a prompt.
  34. ///
  35. /// > Important: This does not include billable image, video or other non-text input. See
  36. /// [Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing) for details.
  37. public let totalBillableCharacters: Int
  38. }
  39. // MARK: - Codable Conformances
  40. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  41. extension CountTokensRequest: Encodable {
  42. enum CodingKeys: CodingKey {
  43. case contents
  44. }
  45. }
  46. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  47. extension CountTokensResponse: Decodable {
  48. enum CodingKeys: CodingKey {
  49. case totalTokens
  50. case totalBillableCharacters
  51. }
  52. public init(from decoder: any Decoder) throws {
  53. let container = try decoder.container(keyedBy: CodingKeys.self)
  54. totalTokens = try container.decode(Int.self, forKey: .totalTokens)
  55. totalBillableCharacters = try container.decodeIfPresent(
  56. Int.self,
  57. forKey: .totalBillableCharacters
  58. ) ?? 0
  59. }
  60. }