package com.lanyuanxiaoyao.digtal.market.ai

import com.baidubce.qianfan.Qianfan
import com.baidubce.qianfan.model.chat.Message
import com.sun.org.apache.xml.internal.serializer.utils.Utils.messages

class QianfanChat(accessKey: String = "ALTAKgPKxinDKjx8DjPTJIR53h", secretKey: String = "593a239482ec43cb81693e0c0883baf8") : Chat {
    private val qianfan = Qianfan(accessKey, secretKey)

    override fun ask(vararg messages: Chat.Companion.Message): String? {
        val response = qianfan.chatCompletion()
            .model("ERNIE-Speed-128K")
            .messages(messages.map {
                Message().apply {
                    role = "user"
                    content = it.content
                }
            })
            .execute()
        return if (response.result.isNullOrBlank()) null
        else response.result
    }
}