import io.ktor.client.call.*
import io.ktor.client.content.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
data class GeminiResponse(
val candidates: List<Candidate>,
val usageMetadata: UsageMetadata,
val finishReason: String,
val safetyRatings: List<SafetyRating>,
data class UsageMetadata(
val promptTokenCount: Long,
val candidatesTokenCount: Long,
val totalTokenCount: Long,
suspend fun sendMessage(input: String): String {
val client = HttpClient {
install(ContentNegotiation) {
val baseUrl = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent"
val systemInstruction = "You are funny json ai bot and you return json or something :)"
data class SystemInstruct(
data class GenerationConfig(
val maxOutputTokens: Int,
val responseMimeType: String
val system_instruction: SystemInstruct,
val contents: List<Contents>,
val generation_config: GenerationConfig
val generationConfig = GenerationConfig(
responseMimeType = "application/json"
val response = client.post(baseUrl) {
append("Content-Type", "application/json")
append("x-goog-api-key", "")
generation_config = generationConfig,
system_instruction = SystemInstruct(parts = Part(text = systemInstruction)),
parts = listOf(Part(text = input))
println(Json.encodeToString(body))
val res = response.body<GeminiResponse>()
return res.candidates[0].content.parts[0].text