Repository: softwaremill/sttp-openai
Pull request #174 (2024-05-09 10:50:13)

Added model field to ImageCreationBody


Patch:
diff --git a/core/src/main/scala/sttp/openai/requests/images/creation/ImageCreationRequestBody.scala b/core/src/main/scala/sttp/openai/requests/images/creation/ImageCreationRequestBody.scala
index 1954532..027296e 100644
--- a/core/src/main/scala/sttp/openai/requests/images/creation/ImageCreationRequestBody.scala
+++ b/core/src/main/scala/sttp/openai/requests/images/creation/ImageCreationRequestBody.scala
@@ -8,6 +8,8 @@ object ImageCreationRequestBody {
 
   /** @param prompt
     *   A text description of the desired image(s). The maximum length is 1000 characters.
+    * @param model
+    *   A name of the model to use for image generation
     * @param n
     *   The number of images to generate. Must be between 1 and 10.
     * @param size
@@ -19,6 +21,7 @@ object ImageCreationRequestBody {
     */
   case class ImageCreationBody(
       prompt: String,
+      model: String,
       n: Option[Int] = None,
       size: Option[Size] = None,
       responseFormat: Option[ResponseFormat] = None,


Test patch:
diff --git a/core/src/test/scala/sttp/openai/fixtures/ImageCreationFixture.scala b/core/src/test/scala/sttp/openai/fixtures/ImageCreationFixture.scala
index 54197ad..54a50da 100644
--- a/core/src/test/scala/sttp/openai/fixtures/ImageCreationFixture.scala
+++ b/core/src/test/scala/sttp/openai/fixtures/ImageCreationFixture.scala
@@ -3,6 +3,16 @@ package sttp.openai.fixtures
 object ImageCreationFixture {
   val jsonRequest: String = """{
       |"prompt": "cute fish",
+      |"model": "dall-e-3",
+      |"n": 1,
+      |"size": "1024x1024",
+      |"response_format": "url",
+      |"user": "user1"
+      |}""".stripMargin
+
+  val jsonRequestDalle2: String = """{
+      |"prompt": "cute fish",
+      |"model": "dall-e-2",
       |"n": 1,
       |"size": "1024x1024",
       |"response_format": "url",
diff --git a/core/src/test/scala/sttp/openai/requests/images/creation/ImageCreationDataSpec.scala b/core/src/test/scala/sttp/openai/requests/images/creation/ImageCreationDataSpec.scala
index 3c2a581..36287a6 100644
--- a/core/src/test/scala/sttp/openai/requests/images/creation/ImageCreationDataSpec.scala
+++ b/core/src/test/scala/sttp/openai/requests/images/creation/ImageCreationDataSpec.scala
@@ -37,6 +37,7 @@ class ImageCreationDataSpec extends AnyFlatSpec with Matchers with EitherValues
     // given
     val givenRequest: ImageCreationBody = ImageCreationBody(
       prompt = "cute fish",
+      "dall-e-3",
       Some(1),
       size = Some(Size.Custom("1024x1024")),
       Some(ResponseFormat.Custom("url")),
@@ -59,6 +60,7 @@ class ImageCreationDataSpec extends AnyFlatSpec with Matchers with EitherValues
     // given
     val givenRequest: ImageCreationBody = ImageCreationBody(
       "cute fish",
+      "dall-e-3",
       Some(1),
       Some(Size.Large),
       Some(ResponseFormat.URL),
@@ -74,4 +76,26 @@ class ImageCreationDataSpec extends AnyFlatSpec with Matchers with EitherValues
     serializedJson shouldBe jsonRequest
   }
 
+  "Given create image request as case class with different model" should "be properly serialized to Json" in {
+    import sttp.openai.requests.images.creation.ImageCreationRequestBody.ImageCreationBody._
+    import sttp.openai.requests.images.creation.ImageCreationRequestBody._
+
+    // given
+    val givenRequest: ImageCreationBody = ImageCreationBody(
+      "cute fish",
+      "dall-e-2",
+      Some(1),
+      Some(Size.Large),
+      Some(ResponseFormat.URL),
+      Some("user1")
+    )
+
+    val jsonRequest = ujson.read(fixtures.ImageCreationFixture.jsonRequestDalle2)
+
+    // when
+    val serializedJson: ujson.Value = SnakePickle.writeJs(givenRequest)
+
+    // then
+    serializedJson shouldBe jsonRequest
+  }
 }