Skip to content

Commit

Permalink
Fixes for CodingActor
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 19, 2023
1 parent 766650f commit 614ddfd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 50 deletions.
3 changes: 1 addition & 2 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.simiacryptus.skyenet

import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.OpenAIClient.*
Expand Down Expand Up @@ -119,7 +118,7 @@ open class Brain(

companion object {
private val log = org.slf4j.LoggerFactory.getLogger(Brain::class.java)
fun String.indent() = this.replace("\n", "\n ")
fun String.indent(indent: String = " ") = this.replace("\n", "\n$indent")
fun joinYamlList(typeDescriptions: List<String>) = typeDescriptions.joinToString("\n") {
"- " + it.indent()
}
Expand Down
64 changes: 16 additions & 48 deletions core/src/main/kotlin/com/simiacryptus/skyenet/actors/CodingActor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.models.OpenAITextModel
import com.simiacryptus.skyenet.Brain
import com.simiacryptus.skyenet.Brain.Companion.indent
import com.simiacryptus.skyenet.Brain.Companion.superMethod
import com.simiacryptus.skyenet.Heart
import com.simiacryptus.skyenet.OutputInterceptor
import com.simiacryptus.util.describe.AbbrevWhitelistYamlDescriber
import com.simiacryptus.util.describe.TypeDescriber
import java.lang.reflect.Modifier
import kotlin.reflect.KClass

@Suppress("unused", "MemberVisibilityCanBePrivate")
Expand Down Expand Up @@ -58,7 +56,12 @@ open class CodingActor(
|""".trimMargin().trim()

open val apiDescription: String
get() = apiDescription(this.symbols, this.describer)
get() = this.symbols.map { (name, utilityObj) ->
"""
|$name:
| ${this.describer.describe(utilityObj.javaClass).indent(" ")}
|""".trimMargin().trim()
}.joinToString("\n")

open val interpreter by lazy { interpreterClass.java.getConstructor(Map::class.java).newInstance(symbols) }

Expand All @@ -70,7 +73,11 @@ open class CodingActor(
if (!autoEvaluate) CodeResultImpl(*messages, api = api)
else answerWithAutoEval(*messages, api = api).first

open fun answerWithPrefix(codePrefix: String, vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): CodeResult =
open fun answerWithPrefix(
codePrefix: String,
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIClient
): CodeResult =
if (!autoEvaluate) CodeResultImpl(*injectCodePrefix(messages, codePrefix), api = api)
else answerWithAutoEval(*injectCodePrefix(messages, codePrefix), api = api).first

Expand All @@ -85,14 +92,15 @@ open class CodingActor(
api: OpenAIClient
): Pair<CodeResult, ExecutionResult> {
var result = CodeResultImpl(*messages, api = api)
var lastError : Throwable? = null
var lastError: Throwable? = null
for (i in 0..fixIterations) try {
return result to result.run()
} catch (ex: Throwable) {
lastError = ex
result = fix(api, messages, result, ex)
}
throw RuntimeException("""
throw RuntimeException(
"""
|Failed to fix code. Last attempt:
|```${interpreter.getLanguage().lowercase()}
|${result.getCode()}
Expand All @@ -102,7 +110,8 @@ open class CodingActor(
|```
|${lastError?.message}
|```
|""".trimMargin().trim())
|""".trimMargin().trim()
)
}

private fun injectCodePrefix(
Expand Down Expand Up @@ -290,47 +299,6 @@ open class CodingActor(
} as java.util.Map<K, V>
}

fun apiDescription(
symbols: Map<String, Any>,
describer: TypeDescriber
): String {
val types: ArrayList<Class<*>> = ArrayList()
val apiobjs = symbols.map { (name, utilityObj) ->
val clazz = Class.forName(utilityObj.javaClass.typeName)
val methods = clazz.methods
.filter { Modifier.isPublic(it.modifiers) }
.filter { it.declaringClass == clazz }
.filter { !it.isSynthetic }
.map { it.superMethod() ?: it }
.filter { it.declaringClass != Object::class.java }
types.addAll(methods.flatMap { (listOf(it.returnType) + it.parameters.map { it.type }).filter { it != clazz } })
types.addAll(clazz.declaredClasses.filter { Modifier.isPublic(it.modifiers) })
"""
|$name:
| operations:
| ${Brain.joinYamlList(methods.map { describer.describe(it) }).indent().indent()}
|""".trimMargin().trim()
}.toTypedArray<String>()
val typeDescriptions = types
.filter { !it.isPrimitive }
.filter { !it.isSynthetic }
.filter { !it.name.startsWith("java.") }
.filter { !setOf("void").contains(it.name) }
.distinct().map {
"""
|${it.simpleName}:
| ${describer.describe(it).indent()}
""".trimMargin().trim()
}.toTypedArray<String>()
return """
|api_objects:
| ${apiobjs.joinToString("\n").indent()}
|components:
| schemas:
| ${typeDescriptions.joinToString("\n").indent().indent()}
|""".trimMargin().trim()
}

}
}

Expand Down

0 comments on commit 614ddfd

Please sign in to comment.