GenerateContentRequest.swift 1.8 KB

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