-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
345 additions
and
1 deletion.
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
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
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
12 changes: 12 additions & 0 deletions
12
ychat/src/commonMain/kotlin/co/yml/ychat/data/dto/EditsChoiceDto.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 co.yml.ychat.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class EditsChoiceDto( | ||
@SerialName("text") | ||
val text: String, | ||
@SerialName("index") | ||
val index: Int | ||
) |
16 changes: 16 additions & 0 deletions
16
ychat/src/commonMain/kotlin/co/yml/ychat/data/dto/EditsDto.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 co.yml.ychat.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class EditsDto( | ||
@SerialName("object") | ||
val objectType: String, | ||
@SerialName("created") | ||
val created: Long, | ||
@SerialName("choices") | ||
val choices: List<EditsChoiceDto>, | ||
@SerialName("usage") | ||
val usage: UsageDto, | ||
) |
20 changes: 20 additions & 0 deletions
20
ychat/src/commonMain/kotlin/co/yml/ychat/data/dto/EditsParamsDto.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,20 @@ | ||
package co.yml.ychat.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class EditsParamsDto( | ||
@SerialName("model") | ||
val model: String, | ||
@SerialName("input") | ||
val input: String, | ||
@SerialName("instruction") | ||
val instruction: String, | ||
@SerialName("n") | ||
val results: Int = 1, | ||
@SerialName("temperature") | ||
val temperature: Double, | ||
@SerialName("top_p") | ||
val topP: Double, | ||
) |
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
20 changes: 20 additions & 0 deletions
20
ychat/src/commonMain/kotlin/co/yml/ychat/domain/mapper/EditsMapper.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,20 @@ | ||
package co.yml.ychat.domain.mapper | ||
|
||
import co.yml.ychat.data.dto.EditsDto | ||
import co.yml.ychat.data.dto.EditsParamsDto | ||
import co.yml.ychat.domain.model.EditsParams | ||
|
||
internal fun EditsDto.toEditsModel(): List<String> { | ||
return this.choices.map { it.text } | ||
} | ||
|
||
internal fun EditsParams.toEditsParamsDto(): EditsParamsDto { | ||
return EditsParamsDto( | ||
model = this.model, | ||
input = this.input, | ||
instruction = this.instruction, | ||
results = this.results, | ||
temperature = this.temperature, | ||
topP = this.topP | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
ychat/src/commonMain/kotlin/co/yml/ychat/domain/model/EditsParams.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,10 @@ | ||
package co.yml.ychat.domain.model | ||
|
||
internal data class EditsParams( | ||
var model: String = "text-davinci-edit-001", | ||
var input: String = "", | ||
var instruction: String = "", | ||
var results: Int = 1, | ||
var temperature: Double = 1.0, | ||
var topP: Double = 1.0, | ||
) |
15 changes: 15 additions & 0 deletions
15
ychat/src/commonMain/kotlin/co/yml/ychat/domain/usecases/EditsUseCase.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,15 @@ | ||
package co.yml.ychat.domain.usecases | ||
|
||
import co.yml.ychat.data.api.ChatGptApi | ||
import co.yml.ychat.domain.mapper.toEditsModel | ||
import co.yml.ychat.domain.mapper.toEditsParamsDto | ||
import co.yml.ychat.domain.model.EditsParams | ||
|
||
internal data class EditsUseCase(private val chatGptApi: ChatGptApi) { | ||
|
||
suspend fun requestEdits(params: EditsParams): List<String> { | ||
val requestDto = params.toEditsParamsDto() | ||
val response = chatGptApi.edits(requestDto) | ||
return response.getBodyOrThrow().toEditsModel() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
ychat/src/commonMain/kotlin/co/yml/ychat/entrypoint/features/Edits.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,23 @@ | ||
package co.yml.ychat.entrypoint.features | ||
|
||
import co.yml.ychat.YChat | ||
import co.yml.ychat.data.exception.ChatGptException | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
interface Edits { | ||
|
||
fun setInput(input: String): Edits | ||
|
||
fun setResults(results: Int): Edits | ||
|
||
fun setModel(model: String): Edits | ||
|
||
fun setTemperature(temperature: Double): Edits | ||
|
||
fun setTopP(topP: Double): Edits | ||
|
||
@Throws(CancellationException::class, ChatGptException::class) | ||
suspend fun execute(instruction: String): List<String> | ||
|
||
fun execute(instruction: String, callback: YChat.Callback<List<String>>) | ||
} |
58 changes: 58 additions & 0 deletions
58
ychat/src/commonMain/kotlin/co/yml/ychat/entrypoint/impl/EditsImpl.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,58 @@ | ||
package co.yml.ychat.entrypoint.impl | ||
|
||
import co.yml.ychat.YChat | ||
import co.yml.ychat.domain.model.EditsParams | ||
import co.yml.ychat.domain.usecases.EditsUseCase | ||
import co.yml.ychat.entrypoint.features.Edits | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
|
||
internal class EditsImpl( | ||
private val dispatcher: CoroutineDispatcher, | ||
private val editsUseCase: EditsUseCase | ||
) : Edits { | ||
|
||
private val scope by lazy { CoroutineScope(SupervisorJob() + dispatcher) } | ||
|
||
private var params: EditsParams = EditsParams() | ||
|
||
override fun setInput(input: String): Edits { | ||
params.input = input | ||
return this | ||
} | ||
|
||
override fun setResults(results: Int): Edits { | ||
params.results = results | ||
return this | ||
} | ||
|
||
override fun setModel(model: String): Edits { | ||
params.model = model | ||
return this | ||
} | ||
|
||
override fun setTemperature(temperature: Double): Edits { | ||
params.temperature = temperature | ||
return this | ||
} | ||
|
||
override fun setTopP(topP: Double): Edits { | ||
params.topP = topP | ||
return this | ||
} | ||
|
||
override suspend fun execute(instruction: String): List<String> { | ||
params.instruction = instruction | ||
return editsUseCase.requestEdits(params) | ||
} | ||
|
||
override fun execute(instruction: String, callback: YChat.Callback<List<String>>) { | ||
scope.launch { | ||
runCatching { execute(instruction) } | ||
.onSuccess { callback.onSuccess(it) } | ||
.onFailure { callback.onError(it) } | ||
} | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
ychat/src/commonTest/kotlin/co/yml/ychat/domain/mapper/EditsMapperTest.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 co.yml.ychat.domain.mapper | ||
|
||
import co.yml.ychat.data.dto.EditsChoiceDto | ||
import co.yml.ychat.data.dto.EditsDto | ||
import co.yml.ychat.data.dto.ImageGenerationsDto | ||
import co.yml.ychat.data.dto.UsageDto | ||
import co.yml.ychat.domain.model.EditsParams | ||
import co.yml.ychat.domain.model.ImageGeneratedDto | ||
import co.yml.ychat.domain.model.ImageGenerationsParams | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class EditsMapperTest { | ||
|
||
@Test | ||
fun `on convert EditsDto to EditsModel`() { | ||
val listOfChoicesDto = listOf(EditsChoiceDto("text 1", 1), EditsChoiceDto("text 2", 2)) | ||
val editsDto = EditsDto( | ||
created = 12345, | ||
objectType = "edit", | ||
choices = listOfChoicesDto, | ||
usage = UsageDto(1, 1, 1) | ||
) | ||
assertEquals(listOfChoicesDto.map { it.text }, editsDto.toEditsModel()) | ||
} | ||
|
||
@Test | ||
fun `on convert EditsParams to EditsDto`() { | ||
val editsParams = EditsParams(input = "this is a test") | ||
assertEquals("text-davinci-edit-001", editsParams.toEditsParamsDto().model) | ||
assertEquals("this is a test", editsParams.toEditsParamsDto().input) | ||
assertEquals("", editsParams.toEditsParamsDto().instruction) | ||
assertEquals(1, editsParams.toEditsParamsDto().results) | ||
assertEquals(1.0, editsParams.toEditsParamsDto().temperature) | ||
assertEquals(1.0, editsParams.toEditsParamsDto().topP) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ychat/src/commonTest/kotlin/co/yml/ychat/domain/model/EditsParamsTest.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,21 @@ | ||
package co.yml.ychat.domain.model | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class EditsParamsTest { | ||
|
||
@Test | ||
fun `on EditsParams verify default values`() { | ||
// arrange | ||
val params = EditsParams() | ||
|
||
// assert | ||
assertEquals("text-davinci-edit-001", params.model) | ||
assertEquals("", params.input) | ||
assertEquals("", params.instruction) | ||
assertEquals(1, params.results) | ||
assertEquals(1.0, params.temperature) | ||
assertEquals(1.0, params.topP) | ||
} | ||
} |
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
Oops, something went wrong.