Skip to content

Gemini support

Choose a tag to compare

@jamesrochabrun jamesrochabrun released this 05 May 06:18
· 20 commits to main since this release

Adding Gemini support.

Screenshot 2024-05-04 at 11 17 10 PM
public enum LLMConfiguration {

   case openAI(OpenAI)
   
   public enum OpenAI {
      /// Configuration for accessing OpenAI's API.
      /// - Parameters:
      ///   - apiKey: The API key for authenticating requests to OpenAI.
      ///   - organizationID: Optional organization ID for OpenAI usage.
      ///   - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
      ///   - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
      case api(key: String, organizationID: String? = nil, configuration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
      /// Configuration for accessing OpenAI's API.
      /// - Parameters:
      ///   - configuration: The AzureOpenAIConfiguration.
      ///   - urlSessionConfiguration: The URLSession configuration to use for network requests. Defaults to `.default`.
      ///   - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
      case azure(configuration: AzureOpenAIConfiguration, urlSessionConfiguration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
      /// Configuration for accessing OpenAI's API.
      /// - Parameters:
      ///   - aiproxyPartialKey: The partial key provided in the 'API Keys' section of the AIProxy dashboard.
      ///   - aiproxyDeviceCheckBypass: The bypass token that is provided in the 'API Keys' section of the AIProxy dashboard.
      ///   - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
      ///   - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
      case aiProxy(aiproxyPartialKey: String, aiproxyDeviceCheckBypass: String? = nil, configuration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
   }
   
   /// Configuration for accessing Anthropic's API.
   /// - Parameters:
   ///   - apiKey: The API key for authenticating requests to Anthropic.
   ///   - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
   case anthropic(apiKey: String, configuration: URLSessionConfiguration = .default)
   
   /// Configuration for accessing Gemini's API.
   /// - Parameters:
   ///   - apiKey: The API key for authenticating requests to Gemini.
   case gemini(apiKey: String) /// ✨ New
}

Gemini Configuration:

let geminiConfiguration: LLMConfiguration = .gemini(apiKey: "your_gemini_api_key_here")
let service = PolyAIServiceFactory.serviceWith([geminiConfiguration])

Message Stream With Gemini

let prompt = "How are you today?"
let parameters: LLMParameter = .gemini(model: "gemini-pro", messages: [.init(role: .user, content: prompt)], maxTokens: 2000)
let stream = try await service.streamMessage(parameters)