CountTokensRequest.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 systemInstruction: ModelContent?
  20. let tools: [Tool]?
  21. let generationConfig: GenerationConfig?
  22. let options: RequestOptions
  23. }
  24. @available(iOS 15.0, macOS 11.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 11.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. }
  42. // MARK: - Codable Conformances
  43. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  44. extension CountTokensRequest: Encodable {
  45. enum CodingKeys: CodingKey {
  46. case contents
  47. case systemInstruction
  48. case tools
  49. case generationConfig
  50. }
  51. }
  52. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  53. extension CountTokensResponse: Decodable {}