I was making an AI bot maker that can learn code f...
# getting-started
l
I was making an AI bot maker that can learn code from docs and answer questions. When running my code, asking a question to the bot, instead of answering it like ChatGPT, for example, it only gave the entire memory as a response. Is there any way I can fix it? Here is the code:
Copy code
package kt.kaii

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll

object Kaii {
    val client = HttpClient(CIO)

    val ml = ML()
    val dl = DL()

    class ML {
        private val models = mutableMapOf<String, AIModel>()

        fun create(name: String): AIModel {
            val model = AIModel(name, "ML", client)
            models[name] = model
            return model
        }

        operator fun get(name: String): AIModel? = models[name]
    }

    class DL {
        private val models = mutableMapOf<String, AIModel>()

        fun create(name: String): AIModel {
            val model = AIModel(name, "DL", client)
            models[name] = model
            return model
        }

        operator fun get(name: String): AIModel? = models[name]
    }

    class AIModel(val name: String, val type: String, private val client: HttpClient) {
        var code: Boolean = false
        private val memory = mutableListOf<String>()

        fun dependencies(code: Boolean = false) {
            this.code = code
        }

        suspend fun train(timeInSeconds: Int) {
            println("Training $type model '$name' for $timeInSeconds seconds by reading documentation...")
            val startTime = System.currentTimeMillis()
            learnFromDocumentation()
            val elapsedTime = (System.currentTimeMillis() - startTime) / 1000
            val remainingTime = timeInSeconds - elapsedTime

            if (remainingTime > 0) {
                delay(remainingTime * 1000)
            }

            println("$type model '$name' training completed.")
        }

        private suspend fun learnFromDocumentation() {
            if (code) {
                println("$type model '$name' is learning Kotlin from documentation...")
                val kotlinDocUrl = listOf(
                    "<https://kotlinlang.org/docs/basic-syntax.html>", "<https://kotlinlang.org/docs/idioms.html>", "<https://kotlinlang.org/docs/numbers.html>", "<https://kotlinlang.org/docs/unsigned-integer-types.html>", "<https://kotlinlang.org/docs/booleans.html>", "<https://kotlinlang.org/docs/characters.html>", "<https://kotlinlang.org/docs/strings.html>", "<https://kotlinlang.org/docs/arrays.html>", "<https://kotlinlang.org/docs/typecasts.html>", "<https://kotlinlang.org/docs/control-flow.html>", "<https://kotlinlang.org/docs/returns.html>", "<https://kotlinlang.org/docs/exceptions.html>", "<https://kotlinlang.org/docs/packages.html>", "<https://kotlinlang.org/docs/classes.html>", "<https://kotlinlang.org/docs/inheritance.html>", "<https://kotlinlang.org/docs/properties.html>", "<https://kotlinlang.org/docs/interfaces.html>", "<https://kotlinlang.org/docs/fun-interfaces.html>", "<https://kotlinlang.org/docs/visibility-modifiers.html>", "<https://kotlinlang.org/docs/extensions.html#extensions-are-resolved-statically>", "<https://kotlinlang.org/docs/data-classes.html>", "<https://kotlinlang.org/docs/sealed-classes.html>", "<https://kotlinlang.org/docs/generics.html>", "<https://kotlinlang.org/docs/nested-classes.html>", "<https://kotlinlang.org/docs/enum-classes.html>", "<https://kotlinlang.org/docs/inline-classes.html>", "<https://kotlinlang.org/docs/object-declarations.html>", "<https://kotlinlang.org/docs/delegation.html>", "<https://kotlinlang.org/docs/delegated-properties.html>", "<https://kotlinlang.org/docs/type-aliases.html>", "<https://kotlinlang.org/docs/functions.html>", "<https://kotlinlang.org/docs/lambdas.html>", "<https://kotlinlang.org/docs/inline-functions.html>", "<https://kotlinlang.org/docs/operator-overloading.html>", "<https://kotlinlang.org/docs/type-safe-builders.html#full-definition-of-the-com-example-html-package>", "<https://kotlinlang.org/docs/using-builders-with-builder-inference.html>", "<https://kotlinlang.org/docs/null-safety.html>", "<https://kotlinlang.org/docs/equality.html>", "<https://kotlinlang.org/docs/this-expressions.html>", "<https://kotlinlang.org/docs/async-programming.html>")
                val channel = Channel<String>(Channel.UNLIMITED) // Increase channel capacity

                val jobs = kotlinDocUrl.map { url ->
                    runBlocking {
                        async {
                        val content = readFromUrl(url)
                        channel.send(content)
                    } }

                }

                jobs.awaitAll() // Wait for all jobs to complete

                // Process each received content
                repeat(kotlinDocUrl.size) {
                    val content = channel.receive()
                    processDocumentation(content)
                }

                println("$type model '$name' has finished learning Kotlin.")
            } else {
                println("$type model '$name' is not configured to learn code.")
            }
        }

        private fun processDocumentation(content: String) {
            val lines = content.split("\n")
            lines.forEach { line ->
                memory.add(line)
            }
            println("Stored ${memory.size} lines of important information in memory.")
        }

        fun recallMemory(): List<String> {
            return memory
        }

        fun ask(question: String): String {
            println("$type model '$name' received question: $question")
            return if (memory.isNotEmpty()) {
                "Answering based on memory: ${memory.joinToString(separator = " ")}"
            } else {
                "I don't have enough information to answer that question."
            }
        }

        private suspend fun readFromUrl(url: String): String {
            val response: HttpResponse = client.get(url)
            return response.bodyAsText()
        }
    }
}

fun main() = runBlocking {
    val mlModel = Kaii.ml.create("MLModel1")
    mlModel.dependencies(code = true)
    mlModel.train(timeInSeconds = 5)
    delay(6000) // Wait for the training to complete
    println("MLModel1 answer: ${mlModel.ask("What is a class in Kotlin?")}")

    val dlModel = Kaii.dl.create("DLModel1")
    dlModel.dependencies(code = true)
    dlModel.train(timeInSeconds = 10)
    delay(11000) // Wait for the training to complete
    println("DLModel1 answer: ${dlModel.ask("How do you define a function in Kotlin?")}")


    Kaii.client.close()
}
🧵 2
a
Are you asking people to fix ChatGPT generated code? Looking at your comments, it seems your intention was to create a simulation 😅
l
I only made some parts with Chatgpt lol. But yea, comments don't matter really. 😀
s
Do you actually want your code to be able to generate conversational answers like ChatGPT? Although you've used some words like 'model' and 'train', there's nothing in your code that would actually do anything like that. I'm sure there'll be plenty of stuff online about how to make a real custom chatbot, but it's not really a Kotlin-specific question so this isn't the right place to go into that.
l
ok
d
This code explicitly does what you are asking about. It's because you are dumping
memory
. Also, Sam is correct on both accounts, but yeah there appears to be no actual ML implementation here.
"Answering based on memory: ${memory.joinToString(separator = " ")}"