package com.correx.infrastructure.inference.llama.cpp

import com.correx.core.inference.Token
import com.correx.core.inference.Tokenizer
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.accept
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType

class LlamaCppTokenizer(
    private val baseUrl: String = "http://127.0.0.1:10000",
    private val httpClient: HttpClient,
) : Tokenizer {

    override suspend fun tokenize(text: String): List<Token> =
        httpClient.post("$baseUrl/tokenize") {
            contentType(ContentType.Application.Json)
            accept(ContentType.Application.Json)
            setBody(TokenizeRequest(content = text))
        }.body<TokenizeResponse>().tokens.map { Token(it) }

    override suspend fun countTokens(text: String): Int = tokenize(text).size
}
