diff --git a/README.md b/README.md index 4878e072..74e69f58 100644 --- a/README.md +++ b/README.md @@ -76,18 +76,18 @@ Maven: com.simiacryptus skyenet-webui - 1.0.21 + 1.0.22 ``` Gradle: ```groovy -implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.21' +implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.22' ``` ```kotlin -implementation("com.simiacryptus:skyenet:1.0.21") +implementation("com.simiacryptus:skyenet:1.0.22") ``` ### 🌟 To Use diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt index 380487ec..96441cc4 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt @@ -1,8 +1,8 @@ package com.simiacryptus.skyenet +import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.openai.OpenAIClient.ChatMessage import com.simiacryptus.openai.OpenAIClient.ChatRequest -import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.util.JsonUtil.toJson import com.simiacryptus.util.describe.TypeDescriber import com.simiacryptus.util.describe.YamlDescriber @@ -14,14 +14,14 @@ import java.util.concurrent.atomic.AtomicInteger @Suppress("MemberVisibilityCanBePrivate") open class Brain( val api: OpenAIClient, - val hands: java.util.Map = java.util.HashMap() as java.util.Map, + val symbols: java.util.Map = java.util.HashMap() as java.util.Map, var model: OpenAIClient.Model = OpenAIClient.Models.GPT35Turbo, var verbose: Boolean = false, var temperature: Double = 0.3, - var yamlDescriber: TypeDescriber = YamlDescriber(), + var describer: TypeDescriber = YamlDescriber(), val language: String = "Kotlin", private val moderated: Boolean = true, - val apiDescription: String = apiDescription(hands, yamlDescriber), + val apiDescription: String = apiDescription(symbols, describer), ) { val metrics: Map get() = hashMapOf( @@ -36,29 +36,32 @@ open class Brain( protected val totalApiDescriptionLength: AtomicInteger = AtomicInteger(0) - open fun implement(prompt: String): String { - if (verbose) log.info(prompt) + open fun implement(vararg prompt: String): String { + if (verbose) log.info("Prompt: \n\t" + prompt.joinToString("\n\t")) + return implement(*getChatMessages(*prompt).toTypedArray()) + } + + fun getChatMessages(vararg prompt: String) = getChatSystemMessages(apiDescription) + + prompt.map { ChatMessage(ChatMessage.Role.user, it) } + + fun implement( + vararg messages: ChatMessage + ): String { val request = ChatRequest() - request.messages = ( - getChatMessages(apiDescription) + listOf( - ChatMessage( - ChatMessage.Role.user, - prompt - ) - )).toTypedArray() + request.messages = messages.toList().toTypedArray() totalApiDescriptionLength.addAndGet(apiDescription.length) - val response = run(request) + val response = chat(request) return response } @Language("TEXT") - open fun getChatMessages(apiDescription: String): List = listOf( + open fun getChatSystemMessages(apiDescription: String): List = listOf( ChatMessage( ChatMessage.Role.system, """ |You will translate natural language instructions into |an implementation using $language and the script context. |Use ``` code blocks labeled with $language where appropriate. - |Defined symbols include ${hands.keySet().joinToString(", ")}. + |Defined symbols include ${symbols.keySet().joinToString(", ")}. |The runtime context is described below: | |$apiDescription @@ -67,65 +70,73 @@ open class Brain( ) open fun fixCommand( - prompt: String, previousCode: String, error: Throwable, - output: String + output: String, + vararg prompt: String ): Pair>> { - if (verbose) log.info(prompt) - val request = ChatRequest() - request.messages = ( - listOf( - ChatMessage( - ChatMessage.Role.system, """ + val promptMessages = listOf( + ChatMessage( + ChatMessage.Role.system, """ |You will translate natural language instructions into |an implementation using $language and the script context. |Use ``` code blocks labeled with $language where appropriate. - |Defined symbols include ${hands.keySet().joinToString(", ")}. + |Defined symbols include ${symbols.keySet().joinToString(", ")}. |Do not include wrapping code blocks, assume a REPL context. |The runtime context is described below: | |$apiDescription |""".trimMargin().trim() - ) - ) + listOf( - ChatMessage( - ChatMessage.Role.user, - prompt - ), + ) + ) + prompt.map { + ChatMessage(ChatMessage.Role.user, it) + } + if (verbose) log.info("Prompt: \n\t" + prompt.joinToString("\n\t")) + return fixCommand(previousCode, error, output, *promptMessages.toTypedArray()) + } + + fun fixCommand( + previousCode: String, + error: Throwable, + output: String, + vararg promptMessages: ChatMessage + ): Pair>> { + val request = ChatRequest() + request.messages = ( + promptMessages.toList() + listOf( ChatMessage( ChatMessage.Role.assistant, """ - |```${language.lowercase()} - |${previousCode} - |``` - |""".trimMargin().trim() + |```${language.lowercase()} + |${previousCode} + |``` + |""".trimMargin().trim() ), ChatMessage( ChatMessage.Role.system, """ - |The previous code failed with the following error: - | - |``` - |${error.message?.trim() ?: ""} - |``` - | - |Output: - |``` - |${output.trim()} - |``` - | - |Correct the code and try again. - |""".trimMargin().trim() + |The previous code failed with the following error: + | + |``` + |${error.message?.trim() ?: ""} + |``` + | + |Output: + |``` + |${output.trim()} + |``` + | + |Correct the code and try again. + |""".trimMargin().trim() ) )).toTypedArray() totalApiDescriptionLength.addAndGet(apiDescription.length) - val response = run(request) + val response = chat(request) val codeBlocks = extractCodeBlocks(response) return Pair(response, codeBlocks) } - private fun run(request: ChatRequest): String { + private fun chat(request: ChatRequest): String { request.model = model.modelName request.temperature = temperature val json = toJson(request) diff --git a/gradle.properties b/gradle.properties index 3287e7db..1be5e1bc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Gradle Releases -> https://github.com/gradle/gradle/releases libraryGroup = com.simiacryptus.skyenet -libraryVersion = 1.0.21 +libraryVersion = 1.0.22 gradleVersion = 7.6.1 # Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library diff --git a/util/src/main/kotlin/com/simiacryptus/skyenet/util/AgentDemoBase.kt b/util/src/main/kotlin/com/simiacryptus/skyenet/util/AgentDemoBase.kt index 8a555905..42eadb67 100644 --- a/util/src/main/kotlin/com/simiacryptus/skyenet/util/AgentDemoBase.kt +++ b/util/src/main/kotlin/com/simiacryptus/skyenet/util/AgentDemoBase.kt @@ -19,7 +19,7 @@ abstract class AgentDemoBase { val heart = heart(hands) val brain = Brain( api = OpenAIClient(OpenAIClient.keyTxt), - hands = hands, + symbols = hands, language = heart.getLanguage(), ) brain.model = OpenAIClient.Models.GPT35Turbo diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/body/SkyenetCodingSession.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/body/SkyenetCodingSession.kt index 8ae6c926..5928a820 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/body/SkyenetCodingSession.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/body/SkyenetCodingSession.kt @@ -23,20 +23,20 @@ open class SkyenetCodingSession( open val brain by lazy { object : Brain( api = parent.api, - hands = hands, + symbols = hands, language = heart.getLanguage(), - yamlDescriber = parent.typeDescriber, + describer = parent.typeDescriber, model = parent.model ) { - override fun getChatMessages(apiDescription: String) = + override fun getChatSystemMessages(apiDescription: String) = if (parent.useHistory) { - super.getChatMessages(apiDescription) + SessionServerUtil.getHistory( + super.getChatSystemMessages(apiDescription) + SessionServerUtil.getHistory( history.values, 10, parent.maxHistoryCharacters ) } else { - super.getChatMessages(apiDescription) + super.getChatSystemMessages(apiDescription) } } @@ -176,7 +176,7 @@ open class SkyenetCodingSession( buffer.append("""
${codedInstruction}
${ex.message}
""") send("""$messageTrail$buffer${SkyenetSessionServerBase.spinner}""") val respondWithCode = - brain.fixCommand(describedInstruction, codedInstruction, ex, status.resultOutput) + brain.fixCommand(codedInstruction, ex, status.resultOutput, describedInstruction) renderedResponse = SessionServerUtil.getRenderedResponse(respondWithCode.second) codedInstruction = SessionServerUtil.getCode(language, respondWithCode.second) buffer.append("""
${renderedResponse}
""") @@ -201,7 +201,7 @@ open class SkyenetCodingSession( //language=HTML send("""$messageTrail

New Code:

${SkyenetSessionServerBase.spinner}
""") val respondWithCode = - brain.fixCommand(describedInstruction, codedInstruction, e, status.resultOutput) + brain.fixCommand(codedInstruction, e, status.resultOutput, describedInstruction) val renderedResponse = SessionServerUtil.getRenderedResponse(respondWithCode.second) val newCode = SessionServerUtil.getCode(language, respondWithCode.second) logger.debug("$sessionId - Response: $renderedResponse")