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

Make OpenAiAiAssertionModel httpClient customizable and add timeout setting #541

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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 @@ -4,6 +4,8 @@ import com.github.takahirom.roborazzi.AiAssertionOptions.AiAssertionModel.Compan
import com.github.takahirom.roborazzi.AiAssertionOptions.AiAssertionModel.Companion.DefaultTemperature
import com.github.takahirom.roborazzi.CaptureResults.Companion.json
import io.ktor.client.HttpClient
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.HttpTimeout.Plugin.INFINITE_TIMEOUT_MS
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
Expand Down Expand Up @@ -42,22 +44,25 @@ class OpenAiAiAssertionModel(
private val seed: Int = 1566,
private val requestBuilderModifier: (HttpRequestBuilder.() -> Unit) = {
header("Authorization", "Bearer $apiKey")
}
) : AiAssertionOptions.AiAssertionModel {
},
private val httpClient: HttpClient = HttpClient {
install(ContentNegotiation) {
json(
json = json
)
}
// log
install(HttpTimeout) {
requestTimeoutMillis = INFINITE_TIMEOUT_MS
socketTimeoutMillis = 80_000
}
if (loggingEnabled) {
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL
}
}
}
},
) : AiAssertionOptions.AiAssertionModel {

override fun assert(
referenceImageFilePath: String,
Expand Down Expand Up @@ -180,7 +185,7 @@ private fun parseOpenAiResponse(
val openAiResult = try {
val element = json.parseToJsonElement(responseText)
val resultsElement = element.jsonObject["results"]
val results = if (resultsElement!=null) {
val results = if (resultsElement != null) {
json.decodeFromJsonElement<List<OpenAiConditionResult>>(resultsElement)
} else {
emptyList()
Expand Down
Loading