Skip to content

Commit 30c5f62

Browse files
committed
docs: Update OpenAI examples
1 parent b3c8f70 commit 30c5f62

File tree

5 files changed

+240
-71
lines changed

5 files changed

+240
-71
lines changed

GUIDELINES.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ development purposes.
7575
with infix form assertions `shouldBe` instead of `assertEquals`.
7676
- Use Kotest's `withClue("<failure reason>")` to describe failure reasons, but only when the assertion is NOT obvious.
7777
Remove obvious cases for simplicity.
78-
- If multiple assertions exist against nullable field, first check for null, e.g.: `params shoulNotBeNull { params.id shouldBe 1 }`
78+
- If multiple assertions exist against nullable field, first check for null, e.g.: `params shouldNotBeNull { params.id shouldBe 1 }`
7979
- For testing json serialization use [Kotest-assertions-json](https://kotest.io/docs/assertions/json/json-overview.html)
8080
assertions, e.g. `shouldEqualJson` and never compare substrings.
8181
- Use `assertSoftly(subject) { ... }` to perform multiple assertions. Never use `assertSoftly { }` to verify properties
82-
of
83-
different subjects, or when there is only one assertion per subject. Avoid using `assertSoftly(this) { ... }`
82+
of different subjects, or when there is only one assertion per subject. Avoid using `assertSoftly(this) { ... }`
8483
- Prioritize test readability
8584
- When asked to write tests in Java: use JUnit5, Mockito, AssertJ core
8685

ai-mocks-openai/src/commonMain/kotlin/me/kpavlov/aimocks/openai/embeddings/OpenaiEmbeddingsMatchers.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ internal object OpenaiEmbeddingsMatchers {
1515
it.contains(string)
1616
} != null,
1717
{ "Input should contain \"$string\"" },
18-
{ "Input should contain not contain \"$string\"" },
18+
{ "Input should not contain \"$string\"" },
1919
)
2020

21-
override fun toString(): String = "Input should contain contain \"$string\""
21+
override fun toString(): String = "Input should contain \"$string\""
2222
}
2323
}

ai-mocks-openai/src/commonMain/kotlin/me/kpavlov/aimocks/openai/model/chat/ChatCompletionModels.kt

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,17 @@ public sealed class MessageContent {
221221
* Simple string content.
222222
*/
223223
@Serializable
224-
public data class Text(val text: String) : MessageContent()
224+
public data class Text(
225+
val text: String,
226+
) : MessageContent()
225227

226228
/**
227229
* Array of content parts for multimodal messages.
228230
*/
229231
@Serializable
230-
public data class Parts(val parts: List<ContentPart>) : MessageContent()
232+
public data class Parts(
233+
val parts: List<ContentPart>,
234+
) : MessageContent()
231235

232236
/**
233237
* Extracts all text content from the message, regardless of whether it's a simple string
@@ -552,13 +556,16 @@ public class MessageContentSerializer : KSerializer<MessageContent> {
552556
// Simple string content
553557
MessageContent.Text(element.contentOrNull ?: "")
554558
}
559+
555560
is JsonArray -> {
556561
// Array of content parts
557-
val parts = element.map { partElement ->
558-
jsonDecoder.json.decodeFromJsonElement<ContentPart>(partElement)
559-
}
562+
val parts =
563+
element.map { partElement ->
564+
jsonDecoder.json.decodeFromJsonElement<ContentPart>(partElement)
565+
}
560566
MessageContent.Parts(parts)
561567
}
568+
562569
else -> throw SerializationException("Expected string or array for message content")
563570
}
564571
}
@@ -575,19 +582,35 @@ public class MessageContentSerializer : KSerializer<MessageContent> {
575582
is MessageContent.Text -> {
576583
jsonEncoder.encodeJsonElement(JsonPrimitive(value.text))
577584
}
585+
578586
is MessageContent.Parts -> {
579587
val array =
580588
JsonArray(
581589
value.parts.map { part ->
582590
when (part) {
583591
is ContentPart.Text ->
584-
jsonEncoder.json.encodeToJsonElement(ContentPart.Text.serializer(), part)
592+
jsonEncoder.json.encodeToJsonElement(
593+
ContentPart.Text.serializer(),
594+
part,
595+
)
596+
585597
is ContentPart.OutputText ->
586-
jsonEncoder.json.encodeToJsonElement(ContentPart.OutputText.serializer(), part)
598+
jsonEncoder.json.encodeToJsonElement(
599+
ContentPart.OutputText.serializer(),
600+
part,
601+
)
602+
587603
is ContentPart.ImageUrl ->
588-
jsonEncoder.json.encodeToJsonElement(ContentPart.ImageUrl.serializer(), part)
604+
jsonEncoder.json.encodeToJsonElement(
605+
ContentPart.ImageUrl.serializer(),
606+
part,
607+
)
608+
589609
is ContentPart.InputAudio ->
590-
jsonEncoder.json.encodeToJsonElement(ContentPart.InputAudio.serializer(), part)
610+
jsonEncoder.json.encodeToJsonElement(
611+
ContentPart.InputAudio.serializer(),
612+
part,
613+
)
591614
}
592615
},
593616
)

0 commit comments

Comments
 (0)