google-gemini / generative-ai-android

The official Android library for the Google Gemini API
https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=android
Apache License 2.0
712 stars 153 forks source link

Expose the HttpClient to allow proxies etc #39

Open yschimke opened 8 months ago

yschimke commented 8 months ago

https://github.com/google/generative-ai-android/blob/7043908ce793a6d424f8c10985d4654bc1b010e5/generativeai/src/main/java/com/google/ai/client/generativeai/internal/api/APIController.kt#L71

Doesn't allow changing the HttpClient, for example adding logging, or configuring a proxy.

Ideally a Ktor engine could be passed in, or if not possible a OkHttp Call.Factory.

yschimke commented 8 months ago

My current workaround is

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "EXPOSED_FUNCTION_RETURN_TYPE", "EXPOSED_PARAMETER_TYPE")
GenerativeModel(
        modelName = "gemini-pro",
        apiKey = BuildConfig.GEMINI_API_KEY,
        generationConfig = config,
        controller = com.google.ai.client.generativeai.internal.api.APIController(
            key = BuildConfig.GEMINI_API_KEY,
            model = "gemini-pro",
            httpEngine = OkHttp.create {
                proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("10.0.2.2", 3125))
            }
        )
    )