-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat :: Image generation on final chat request
- Loading branch information
Showing
7 changed files
with
165 additions
and
44 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/teamapi/palette/config/WebClientConfig.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,16 @@ | ||
package com.teamapi.palette.config | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.reactive.function.client.ExchangeStrategies | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
@Configuration | ||
class WebClientConfig { | ||
@Bean | ||
fun client() = WebClient.builder().exchangeStrategies( | ||
ExchangeStrategies.builder() | ||
.codecs { it.defaultCodecs().maxInMemorySize(30 * 1024 * 1024) } | ||
.build() | ||
).build() | ||
} |
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
21 changes: 15 additions & 6 deletions
21
src/main/kotlin/com/teamapi/palette/service/infra/GenerativeImageService.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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package com.teamapi.palette.service.infra | ||
|
||
import com.azure.ai.openai.OpenAIAsyncClient | ||
import com.azure.ai.openai.models.ImageGenerationOptions | ||
import com.teamapi.palette.service.infra.comfy.GenerateRequest | ||
import com.teamapi.palette.service.infra.comfy.GenerateResponse | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.awaitBody | ||
import org.springframework.web.reactive.function.client.awaitExchange | ||
|
||
@Service | ||
class GenerativeImageService( | ||
private val azure: OpenAIAsyncClient, | ||
private val client: WebClient, | ||
private val mapper: Json, | ||
) { | ||
fun draw(originalText: String) = | ||
azure.getImageGenerations("Dalle3", ImageGenerationOptions(originalText)) | ||
.handleAzureError(mapper) | ||
suspend fun draw(prompt: GenerateRequest): GenerateResponse { | ||
return client.post() | ||
.uri("https://comfy.paletteapp.xyz/gen") | ||
.bodyValue(mapper.encodeToString(prompt)) | ||
.header("content-type", "application/json") | ||
.awaitExchange { it.awaitBody<GenerateResponse>() } | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/teamapi/palette/service/infra/comfy/GenerateRequest.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,12 @@ | ||
package com.teamapi.palette.service.infra.comfy | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class GenerateRequest( | ||
val title: String, | ||
val pos: Int, | ||
val width: Int, | ||
val height: Int, | ||
val prompt: String | ||
) |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/teamapi/palette/service/infra/comfy/GenerateResponse.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,6 @@ | ||
package com.teamapi.palette.service.infra.comfy | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class GenerateResponse(val result: Boolean, val image: String? = null, val error: String? = null) |
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