GenerateContentRequest.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 GenerateContentRequest {
  17. /// Model name.
  18. let model: String
  19. let contents: [ModelContent]
  20. let generationConfig: GenerationConfig?
  21. let safetySettings: [SafetySetting]?
  22. let tools: [Tool]?
  23. let toolConfig: ToolConfig?
  24. let systemInstruction: ModelContent?
  25. let isStreaming: Bool
  26. let options: RequestOptions
  27. }
  28. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  29. extension GenerateContentRequest: Encodable {
  30. enum CodingKeys: String, CodingKey {
  31. case contents
  32. case generationConfig
  33. case safetySettings
  34. case tools
  35. case toolConfig
  36. case systemInstruction
  37. }
  38. }
  39. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  40. extension GenerateContentRequest: GenerativeAIRequest {
  41. typealias Response = GenerateContentResponse
  42. var url: URL {
  43. let modelURL = "\(Constants.baseURL)/\(options.apiVersion)/\(model)"
  44. if isStreaming {
  45. return URL(string: "\(modelURL):streamGenerateContent?alt=sse")!
  46. } else {
  47. return URL(string: "\(modelURL):generateContent")!
  48. }
  49. }
  50. }