TemplateGenerateContentRequest.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 variables: [String: TemplateVariable]
  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 variables = "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(variables, forKey: .variables)
  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. var url: 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. let templateName = template.hasSuffix(".prompt") ? template : "\(template).prompt"
  47. urlString += "/templates/\(templateName):templateGenerateContent"
  48. if stream {
  49. // TODO: Fix this.
  50. urlString += "?alt=sse"
  51. }
  52. return URL(string: urlString)!
  53. }
  54. }