Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,32 @@ class DefaultOkHttpOpenAiClient : OpenAiApiAdapter {
override fun onResponse(response: com.squareup.okhttp.Response) {
try {
if (!response.isSuccessful) {
throw IOException("Error: ${response.code()} ${response.message()}")
sink.error(IOException("Error: ${response.code()} ${response.message()}"))
} else {
sink.success(mapper.readValue(response.body().string()))
}
} catch (e: Exception) {
sink.error(e)
} finally {
try {
if (response.body() != null) {
response.body().close()
}
} catch (e: java.lang.Exception) {
// logger.warn("Failed to quietly close Response", e)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line needed?

}
}
}
})
}
}

override fun close() {
client.dispatcher?.executorService?.shutdown()
client.connectionPool?.evictAll()
client.cache?.close()
}

companion object {
val mapper = jacksonObjectMapper()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ interface OpenAiApiAdapter {
fun getProperties(): OpenAiProperties
fun setProperties(properties: OpenAiProperties)
fun chat(request: ChatRequest): Publisher<ChatResponse>

fun close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ package io.github.mscheong01.interfaice.openai

import io.github.mscheong01.interfaice.AiProxyFactory
import io.github.mscheong01.interfaice.TranscodingRules
import java.io.Closeable
import java.lang.reflect.Proxy
import java.util.ServiceLoader

class OpenAiProxyFactory(
private val openAiApiAdapter: OpenAiApiAdapter
) : AiProxyFactory {
) : AiProxyFactory, Closeable {
val customTranscodingRules = mutableListOf<TranscodingRules.CustomRule<*>>()

constructor(
Expand All @@ -39,6 +40,10 @@ class OpenAiProxyFactory(
this.customTranscodingRules.addAll(customTranscodingRules)
}

override fun close() {
openAiApiAdapter.close()
}

companion object {
private fun getOpenAiApiAdapter(properties: OpenAiProperties): OpenAiApiAdapter {
val adapters = ServiceLoader.load(OpenAiApiAdapter::class.java)
Expand Down