Skip to content

Commit 259a6a6

Browse files
committed
feature: Implements list models entrypoint
1 parent 221a1bf commit 259a6a6

File tree

16 files changed

+305
-0
lines changed

16 files changed

+305
-0
lines changed

ychat/src/commonMain/kotlin/co/yml/ychat/YChat.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import co.yml.ychat.entrypoint.features.ChatCompletions
44
import co.yml.ychat.entrypoint.features.Completion
55
import co.yml.ychat.entrypoint.features.Edits
66
import co.yml.ychat.entrypoint.features.ImageGenerations
7+
import co.yml.ychat.entrypoint.features.ListModels
78
import co.yml.ychat.entrypoint.impl.YChatImpl
89
import kotlin.jvm.JvmStatic
910
import kotlin.jvm.Volatile
@@ -18,6 +19,19 @@ import kotlin.native.concurrent.ThreadLocal
1819
*/
1920
interface YChat {
2021

22+
/**
23+
* The listModels api lists the currently available models, and provides basic information
24+
* about each one such as the owner and availability.
25+
*
26+
* Example usage:
27+
* ```
28+
* val result = YChat.create(apiKey)
29+
* .listModels()
30+
* .execute()
31+
* ```
32+
*/
33+
fun listModels(): ListModels
34+
2135
/**
2236
* The completions api can be used for a wide variety of tasks. You input some text as a
2337
* prompt, and the model will generate a text completion that attempts to match whatever

ychat/src/commonMain/kotlin/co/yml/ychat/data/api/ChatGptApi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import co.yml.ychat.data.dto.EditsDto
88
import co.yml.ychat.data.dto.EditsParamsDto
99
import co.yml.ychat.data.dto.ImageGenerationsDto
1010
import co.yml.ychat.data.dto.ImageGenerationsParamsDto
11+
import co.yml.ychat.data.dto.ModelListDto
1112
import co.yml.ychat.data.infrastructure.ApiResult
1213

1314
internal interface ChatGptApi {
@@ -19,4 +20,6 @@ internal interface ChatGptApi {
1920
suspend fun imageGenerations(paramsDto: ImageGenerationsParamsDto): ApiResult<ImageGenerationsDto>
2021

2122
suspend fun edits(paramsDto: EditsParamsDto): ApiResult<EditsDto>
23+
24+
suspend fun models(): ApiResult<ModelListDto>
2225
}

ychat/src/commonMain/kotlin/co/yml/ychat/data/api/impl/ChatGptApiImpl.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import co.yml.ychat.data.dto.EditsDto
99
import co.yml.ychat.data.dto.EditsParamsDto
1010
import co.yml.ychat.data.dto.ImageGenerationsDto
1111
import co.yml.ychat.data.dto.ImageGenerationsParamsDto
12+
import co.yml.ychat.data.dto.ModelListDto
1213
import co.yml.ychat.data.infrastructure.ApiExecutor
1314
import co.yml.ychat.data.infrastructure.ApiResult
1415
import io.ktor.http.HttpMethod
@@ -46,4 +47,11 @@ internal class ChatGptApiImpl(private val apiExecutor: ApiExecutor) : ChatGptApi
4647
.setBody(paramsDto)
4748
.execute()
4849
}
50+
51+
override suspend fun models(): ApiResult<ModelListDto> {
52+
return apiExecutor
53+
.setEndpoint("v1/models")
54+
.setHttpMethod(HttpMethod.Get)
55+
.execute()
56+
}
4957
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package co.yml.ychat.data.dto
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
internal data class ModelDto(
8+
@SerialName("id")
9+
val id: String,
10+
@SerialName("owned_by")
11+
val ownedBy: String,
12+
@SerialName("permission")
13+
val permission: List<ModelPermissionDto>,
14+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package co.yml.ychat.data.dto
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
internal data class ModelListDto(
8+
@SerialName("data")
9+
val models: List<ModelDto>,
10+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package co.yml.ychat.data.dto
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
internal data class ModelPermissionDto(
8+
@SerialName("id")
9+
val id: String,
10+
@SerialName("allow_create_engine")
11+
val allowCreateEngine: Boolean,
12+
@SerialName("allow_sampling")
13+
val allowSampling: Boolean,
14+
@SerialName("allow_logprobs")
15+
val allowLogProbs: Boolean,
16+
@SerialName("allow_search_indices")
17+
val allowSearchIndices: Boolean,
18+
@SerialName("allow_view")
19+
val allowView: Boolean,
20+
@SerialName("allow_fine_tuning")
21+
val allowFineTuning: Boolean,
22+
@SerialName("organization")
23+
val organization: String,
24+
@SerialName("is_blocking")
25+
val isBlocking: Boolean,
26+
)

ychat/src/commonMain/kotlin/co/yml/ychat/di/module/LibraryModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import co.yml.ychat.domain.usecases.ChatCompletionsUseCase
99
import co.yml.ychat.domain.usecases.CompletionUseCase
1010
import co.yml.ychat.domain.usecases.EditsUseCase
1111
import co.yml.ychat.domain.usecases.ImageGenerationsUseCase
12+
import co.yml.ychat.domain.usecases.ListModelsUseCase
1213
import co.yml.ychat.entrypoint.features.ChatCompletions
1314
import co.yml.ychat.entrypoint.features.Completion
1415
import co.yml.ychat.entrypoint.features.Edits
1516
import co.yml.ychat.entrypoint.features.ImageGenerations
17+
import co.yml.ychat.entrypoint.features.ListModels
1618
import co.yml.ychat.entrypoint.impl.ChatCompletionsImpl
1719
import co.yml.ychat.entrypoint.impl.CompletionImpl
1820
import co.yml.ychat.entrypoint.impl.EditsImpl
1921
import co.yml.ychat.entrypoint.impl.ImageGenerationsImpl
22+
import co.yml.ychat.entrypoint.impl.ListModelsImpl
2023
import kotlinx.coroutines.Dispatchers
2124
import org.koin.core.module.Module
2225
import org.koin.dsl.module
@@ -27,13 +30,15 @@ internal class LibraryModule(private val apiKey: String) {
2730
entrypointModule + domainModule + dataModule + platformModule()
2831

2932
private val entrypointModule = module {
33+
factory<ListModels> { ListModelsImpl(Dispatchers.Default, get()) }
3034
factory<Completion> { CompletionImpl(Dispatchers.Default, get()) }
3135
factory<ChatCompletions> { ChatCompletionsImpl(Dispatchers.Default, get()) }
3236
factory<ImageGenerations> { ImageGenerationsImpl(Dispatchers.Default, get()) }
3337
factory<Edits> { EditsImpl(Dispatchers.Default, get()) }
3438
}
3539

3640
private val domainModule = module {
41+
factory { ListModelsUseCase(get()) }
3742
factory { CompletionUseCase(get(), get()) }
3843
factory { ChatCompletionsUseCase(get()) }
3944
factory { ImageGenerationsUseCase(get()) }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package co.yml.ychat.domain.mapper
2+
3+
import co.yml.ychat.data.dto.ModelDto
4+
import co.yml.ychat.data.dto.ModelPermissionDto
5+
import co.yml.ychat.domain.model.AIModel
6+
import co.yml.ychat.domain.model.AIModelPermission
7+
8+
internal fun List<ModelDto>.toModel(): List<AIModel> {
9+
return this.map { it.toModel() }
10+
}
11+
12+
internal fun ModelDto.toModel(): AIModel {
13+
return AIModel(
14+
id = this.id,
15+
ownedBy = this.ownedBy,
16+
permission = this.permission.map { it.toPermission() }
17+
)
18+
}
19+
20+
internal fun ModelPermissionDto.toPermission(): AIModelPermission {
21+
return AIModelPermission(
22+
id = id,
23+
allowCreateEngine = allowCreateEngine,
24+
allowSampling = allowSampling,
25+
allowLogProbs = allowLogProbs,
26+
allowSearchIndices = allowSearchIndices,
27+
allowView = allowView,
28+
allowFineTuning = allowFineTuning,
29+
organization = organization,
30+
isBlocking = isBlocking,
31+
)
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package co.yml.ychat.domain.model
2+
3+
/**
4+
* The artificial intelligence model, providing basic information about the model such as the
5+
* owner and permission.
6+
* @property id The ID of the model.
7+
* @property ownedBy The user or organization that owns the model.
8+
* @property permission A list of permissions associated with the model.
9+
*/
10+
data class AIModel(
11+
val id: String,
12+
val ownedBy: String,
13+
val permission: List<AIModelPermission>,
14+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package co.yml.ychat.domain.model
2+
3+
/**
4+
* A representation of the permissions associated with a [AIModel].
5+
* @property id The ID of the permission.
6+
* @property allowCreateEngine Whether the permission allows creating engines.
7+
* @property allowSampling Whether the permission allows sampling from the model.
8+
* @property allowLogProbs Whether the permission allows accessing log probabilities.
9+
* @property allowSearchIndices Whether the permission allows accessing search indices.
10+
* @property allowView Whether the permission allows viewing the model.
11+
* @property allowFineTuning Whether the permission allows fine-tuning the model.
12+
* @property organization The organization associated with the permission.
13+
* @property isBlocking Whether the permission is currently blocking access to the model.
14+
*/
15+
data class AIModelPermission(
16+
val id: String,
17+
val allowCreateEngine: Boolean,
18+
val allowSampling: Boolean,
19+
val allowLogProbs: Boolean,
20+
val allowSearchIndices: Boolean,
21+
val allowView: Boolean,
22+
val allowFineTuning: Boolean,
23+
val organization: String,
24+
val isBlocking: Boolean,
25+
)

0 commit comments

Comments
 (0)