CountTokensRequest.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  16. struct CountTokensRequest {
  17. let model: String
  18. let contents: [ModelContent]
  19. let systemInstruction: ModelContent?
  20. let tools: [Tool]?
  21. let generationConfig: GenerationConfig?
  22. let options: RequestOptions
  23. }
  24. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  25. extension CountTokensRequest: GenerativeAIRequest {
  26. typealias Response = CountTokensResponse
  27. var url: URL {
  28. URL(string: "\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens")!
  29. }
  30. }
  31. /// The model's response to a count tokens request.
  32. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  33. public struct CountTokensResponse {
  34. /// The total number of tokens in the input given to the model as a prompt.
  35. public let totalTokens: Int
  36. /// The total number of billable characters in the text input given to the model as a prompt.
  37. ///
  38. /// > Important: This does not include billable image, video or other non-text input. See
  39. /// [Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing) for details.
  40. public let totalBillableCharacters: Int?
  41. /// The breakdown, by modality, of how many tokens are consumed by the prompt.
  42. public let promptTokensDetails: [ModalityTokenCount]
  43. }
  44. // MARK: - Codable Conformances
  45. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  46. extension CountTokensRequest: Encodable {
  47. enum CodingKeys: CodingKey {
  48. case contents
  49. case systemInstruction
  50. case tools
  51. case generationConfig
  52. }
  53. }
  54. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  55. extension CountTokensResponse: Decodable {
  56. enum CodingKeys: CodingKey {
  57. case totalTokens
  58. case totalBillableCharacters
  59. case promptTokensDetails
  60. }
  61. public init(from decoder: any Decoder) throws {
  62. let container = try decoder.container(keyedBy: CodingKeys.self)
  63. totalTokens = try container.decodeIfPresent(Int.self, forKey: .totalTokens) ?? 0
  64. totalBillableCharacters =
  65. try container.decodeIfPresent(Int.self, forKey: .totalBillableCharacters)
  66. promptTokensDetails =
  67. try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
  68. }
  69. }