CountTokensRequest.swift 2.9 KB

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