Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. Add onClientCreated And onConfigClient parameter #224

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wilinz
Copy link

@wilinz wilinz commented Jul 29, 2023

  1. Add onClientCreated parameter to set request interceptors for httpClient.
  2. Add onConfigClient parameter to configure HttpClient, such as adding a cookie manager.
Q A
New feature? yes

Describe your change

  1. Add onClientCreated parameter to set request interceptors for httpClient.
  2. Add onConfigClient parameter to configure HttpClient, such as adding a cookie manager.

What problem is this fixing?

  1. onClientCreated: Many existing Android apps typically use okhttp as their http client. Now I have implemented a forwarding server for the OpenAI API, which has the same interface as the official one. This avoids exposing the apikey in the frontend and prevents apikey leakage. Therefore, I changed the authentication method to cookie authentication. This means I need to set the cookie header for the http client in the SDK. However, the cookie may change frequently, so it cannot be simply passed as a parameter through OpenAIConfig's headers. Therefore, I need to set a request interceptor for the SDK's http client. Here is an example:
    var cookieJar =
        PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(MyApplication.instance))

    val openAI = OpenAI(
        token = "",
        logging = LoggingConfig(LogLevel.All),
        host = OpenAIHost(baseUrl = "http://127.0.0.1/v1/"),
        onClientCreated = {
            it.plugin(HttpSend).intercept { request ->
                fun cookieHeader(cookies: List<Cookie>): String = buildString {
                    cookies.forEachIndexed { index, cookie ->
                        if (index > 0) append("; ")
                        append(cookie.name).append('=').append(cookie.value)
                    }
                }
                val cookie = cookieJar.loadForRequest(request.url.buildString().toHttpUrl())
                request.headers.append("Cookie", cookieHeader(cookie))
                execute(request)
            }
        },
    )
  1. onConfigClient: This parameter allows you to configure the http client with additional options, such as:
val openAI = OpenAI(
    token = "",
    logging = LoggingConfig(LogLevel.All),
    host = OpenAIHost(baseUrl = "http://127.0.0.1/v1/"),
    onConfigClient = {
        install(HttpCookies)
    }
)

wilinz added 2 commits July 30, 2023 03:17
…lient.

2. Add onConfigClient parameter to configure HttpClient, such as adding a cookie manager.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants