CountTokensRequest.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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: [ContentModality: ModalityTokenDetails]
  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 = try container.decodeIfPresent(
  65. Int.self,
  66. forKey: .totalBillableCharacters
  67. )
  68. let promptModalityTokenCounts = try container
  69. .decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
  70. promptTokensDetails = promptModalityTokenCounts.asModalityTokenDetails()
  71. }
  72. }