CountTokensRequest.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 location: String
  19. let contents: [ModelContent]
  20. let systemInstruction: ModelContent?
  21. let tools: [Tool]?
  22. let generationConfig: GenerationConfig?
  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(
  30. string: "https://\(location)-\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens"
  31. )!
  32. }
  33. }
  34. /// The model's response to a count tokens request.
  35. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  36. public struct CountTokensResponse {
  37. /// The total number of tokens in the input given to the model as a prompt.
  38. public let totalTokens: Int
  39. /// The total number of billable characters in the text input given to the model as a prompt.
  40. ///
  41. /// > Important: This does not include billable image, video or other non-text input. See
  42. /// [Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing) for details.
  43. public let totalBillableCharacters: Int?
  44. }
  45. // MARK: - Codable Conformances
  46. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  47. extension CountTokensRequest: Encodable {
  48. enum CodingKeys: CodingKey {
  49. case contents
  50. case systemInstruction
  51. case tools
  52. case generationConfig
  53. }
  54. }
  55. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  56. extension CountTokensResponse: Decodable {}