-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include response tests in serialization errors
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
core/src/commonTest/kotlin/com/xebia/functional/xef/errors/SerializationErrorTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.xebia.functional.xef.errors | ||
|
||
import com.xebia.functional.openai.generated.model.CreateChatCompletionRequestModel | ||
import com.xebia.functional.xef.AI | ||
import com.xebia.functional.xef.Config | ||
import com.xebia.functional.xef.OpenAI | ||
import com.xebia.functional.xef.prompt.Prompt | ||
import com.xebia.functional.xef.prompt.PromptBuilder.Companion.user | ||
import com.xebia.functional.xef.prompt.configuration.PromptConfiguration | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.string.shouldContain | ||
|
||
class SerializationErrorTests : | ||
StringSpec({ | ||
val config = | ||
Config( | ||
token = "<bad-token>", | ||
) | ||
val openAI = OpenAI(config) | ||
val chat = openAI.chat | ||
val model = CreateChatCompletionRequestModel.gpt_3_5_turbo | ||
|
||
"serialization errors should include response" { | ||
try { | ||
val prompt = | ||
Prompt( | ||
model = model, | ||
configuration = PromptConfiguration.invoke { maxDeserializationAttempts = 1 } | ||
) { | ||
+user("Hello, how are you?") | ||
} | ||
AI<String>(prompt = prompt, api = chat) | ||
} catch (e: Exception) { | ||
e.message.shouldContain("Incorrect API key") | ||
} | ||
} | ||
}) |
4 changes: 4 additions & 0 deletions
4
...lient/src/commonMain/kotlin/com/xebia/functional/openai/errors/ResponseSerializerError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.xebia.functional.openai.errors | ||
|
||
class ResponseSerializerError(message: String, cause: Throwable? = null) : | ||
Exception(message, cause) |
11 changes: 11 additions & 0 deletions
11
...ient/src/commonMain/kotlin/com/xebia/functional/openai/errors/ResponseSerializerErrors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.xebia.functional.openai.errors | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.statement.* | ||
|
||
suspend inline fun <reified A> HttpResponse.serializeOrThrowWithResponseInfo(): A = | ||
try { | ||
this.body() ?: throw ResponseSerializerError("Response body is null") | ||
} catch (e: Exception) { | ||
throw ResponseSerializerError("Failed to deserialize response body:\n${bodyAsText()}", e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters