TemplateGenerateContentRequest.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2025 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 TemplateGenerateContentRequest: Sendable {
  17. let template: String
  18. let inputs: [String: TemplateInput]
  19. let history: [ModelContent]
  20. let projectID: String
  21. let stream: Bool
  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 TemplateGenerateContentRequest: Encodable {
  27. enum CodingKeys: String, CodingKey {
  28. case inputs
  29. case history
  30. }
  31. func encode(to encoder: any Encoder) throws {
  32. var container = encoder.container(keyedBy: CodingKeys.self)
  33. try container.encode(inputs, forKey: .inputs)
  34. try container.encode(history, forKey: .history)
  35. }
  36. }
  37. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  38. extension TemplateGenerateContentRequest: GenerativeAIRequest {
  39. typealias Response = GenerateContentResponse
  40. func getURL() throws -> URL {
  41. var urlString =
  42. "\(apiConfig.service.endpoint.rawValue)/\(apiConfig.version.rawValue)/projects/\(projectID)"
  43. if case let .vertexAI(_, location) = apiConfig.service {
  44. urlString += "/locations/\(location)"
  45. }
  46. if stream {
  47. urlString += "/templates/\(template):templateStreamGenerateContent?alt=sse"
  48. } else {
  49. urlString += "/templates/\(template):templateGenerateContent"
  50. }
  51. guard let url = URL(string: urlString) else {
  52. throw AILog.makeInternalError(message: "Malformed URL: \(urlString)", code: .malformedURL)
  53. }
  54. return url
  55. }
  56. }