All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.aallam.openai.client.internal.api.FineTunesApi.kt Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta01
Show newest version
package com.aallam.openai.client.internal.api

import com.aallam.openai.api.ExperimentalOpenAI
import com.aallam.openai.api.core.DeleteResponse
import com.aallam.openai.api.core.ListResponse
import com.aallam.openai.api.finetune.FineTune
import com.aallam.openai.api.finetune.FineTuneEvent
import com.aallam.openai.api.finetune.FineTuneId
import com.aallam.openai.api.finetune.FineTuneRequest
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.FineTunes
import com.aallam.openai.client.internal.api.ModelsApi.Companion.ModelsPathV1
import com.aallam.openai.client.internal.extension.streamEventsOf
import com.aallam.openai.client.internal.http.HttpRequester
import com.aallam.openai.client.internal.http.perform
import io.ktor.client.call.body
import io.ktor.client.request.*
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import kotlinx.coroutines.flow.Flow

/**
 * Implementation of [FineTunes].
 */
internal class FineTunesApi(private val requester: HttpRequester) : FineTunes {

    @ExperimentalOpenAI
    override suspend fun fineTune(request: FineTuneRequest): FineTune {
        return requester.perform {
            it.post {
                url(path = FineTunesPathV1)
                setBody(request)
                contentType(ContentType.Application.Json)
            }
        }
    }

    @ExperimentalOpenAI
    override suspend fun fineTune(fineTuneId: FineTuneId): FineTune? {
        val response = requester.perform {
            it.get { url(path = "$FineTunesPathV1/${fineTuneId.id}") }
        }
        return if (response.status == HttpStatusCode.NotFound) null else response.body()
    }

    @ExperimentalOpenAI
    override suspend fun fineTunes(): List {
        return requester.perform> {
            it.get { url(path = FineTunesPathV1) }
        }.data
    }

    @ExperimentalOpenAI
    override suspend fun cancel(fineTuneId: FineTuneId): FineTune? {
        val response = requester.perform {
            it.post { url(path = "$FineTunesPathV1/${fineTuneId.id}/cancel") }
        }
        return if (response.status == HttpStatusCode.NotFound) null else response.body()
    }

    @ExperimentalOpenAI
    override suspend fun fineTuneEvents(fineTuneId: FineTuneId): List {
        return requester.perform> {
            it.get { url(path = "$FineTunesPathV1/${fineTuneId.id}/events") }
        }.data
    }

    @ExperimentalOpenAI
    override fun fineTuneEventsFlow(fineTuneId: FineTuneId): Flow {
        return streamEventsOf {
            requester.perform {
                it.get {
                    url(path = "$FineTunesPathV1/${fineTuneId.id}/events") {
                        parameters.append("stream", "true")
                    }
                }
            }
        }
    }

    @ExperimentalOpenAI
    override suspend fun delete(fineTuneModel: ModelId): Boolean {
        val response = requester.perform {
            it.delete {
                url(path = "$ModelsPathV1/${fineTuneModel.id}")
            }
        }
        return when (response.status) {
            HttpStatusCode.NotFound -> false
            else -> response.body().deleted
        }
    }

    companion object {
        private const val FineTunesPathV1 = "v1/fine-tunes"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy