Skip to content

Commit

Permalink
1.2.4 (#102)
Browse files Browse the repository at this point in the history
* 1.2.4

* refactor

* wip

* wip

* wip

* wip

* task type data types

* wip

* Update settings.gradle.kts

* Update PlanCoordinator.kt

* Update build.gradle.kts
  • Loading branch information
acharneski authored Sep 23, 2024
1 parent 68fc375 commit 56b8e63
Show file tree
Hide file tree
Showing 111 changed files with 2,284 additions and 1,103 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ Maven:
<dependency>
<groupId>com.simiacryptus</groupId>
<artifactId>skyenet-webui</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.1.4'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.1.5'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.1.4")
implementation("com.simiacryptus:skyenet:1.1.5")
```

### 🌟 To Use
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val hsqldb_version = "2.7.2"

dependencies {

implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.1.4")
implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.1.5")
implementation(group = "org.hsqldb", name = "hsqldb", version = hsqldb_version)

implementation("org.apache.commons:commons-text:1.11.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import com.simiacryptus.skyenet.core.platform.User

open class ActorSystem<T : Enum<*>>(
val actors: Map<String, BaseActor<*, *>>,
dataStorage: StorageInterface,
user: User?,
session: Session
) : PoolSystem(dataStorage, user, session) {

fun getActor(actor: T) = actors.get(actor.name)!!
}

open class PoolSystem(
val dataStorage: StorageInterface,
val user: User?,
val session: Session
) {
protected val pool by lazy { ApplicationServices.clientManager.getPool(session, user) }

fun getActor(actor: T) = actors.get(actor.name)!!
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.simiacryptus.skyenet.core.actors

import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.ApiModel
import com.simiacryptus.jopenai.models.ApiModel
import com.simiacryptus.jopenai.ChatClient
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.ChatModels
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.simiacryptus.skyenet.core.actors

import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.ApiModel.*
import com.simiacryptus.jopenai.models.ApiModel.*
import com.simiacryptus.jopenai.ChatClient
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.describe.AbbrevWhitelistTSDescriber
import com.simiacryptus.jopenai.describe.AbbrevWhitelistYamlDescriber
import com.simiacryptus.jopenai.describe.TypeDescriber
import com.simiacryptus.jopenai.models.ChatModels
Expand All @@ -16,19 +16,24 @@ import java.util.*
import javax.script.ScriptException
import kotlin.reflect.KClass

private const val TT = "`"+"`"+"`"
typealias CodeInterceptor = (String) -> String


open class CodingActor(
val interpreterClass: KClass<out Interpreter>,
val symbols: Map<String, Any> = mapOf(),
val describer: TypeDescriber = AbbrevWhitelistYamlDescriber(
val describer: TypeDescriber = AbbrevWhitelistTSDescriber(
"com.simiacryptus",
"com.github.simiacryptus"
),
name: String? = interpreterClass.simpleName,
val details: String? = null,
model: OpenAITextModel,
model: OpenAITextModel = OpenAIModels.GPT4o,
val fallbackModel: ChatModels = OpenAIModels.GPT4o,
temperature: Double = 0.1,
val runtimeSymbols: Map<String, Any> = mapOf()
val runtimeSymbols: Map<String, Any> = mapOf(),
var codeInterceptor: CodeInterceptor = { it }
) : BaseActor<CodingActor.CodeRequest, CodingActor.CodeResult>(
prompt = "",
name = name,
Expand Down Expand Up @@ -67,43 +72,45 @@ open class CodingActor(
get() {
val formatInstructions =
if (evalFormat) """Code should be structured as appropriately parameterized function(s)
|with the final line invoking the function with the appropriate request parameters.""" else ""
with the final line invoking the function with the appropriate request parameters.""" else ""
return if (symbols.isNotEmpty()) {
"""
|You are a coding assistant allows users actions to be enacted using $language and the script context.
|Your role is to translate natural language instructions into code as well as interpret the results and converse with the user.
|Use ``` code blocks labeled with $language where appropriate. (i.e. ```$language)
|Each response should have EXACTLY ONE code block. Do not use inline blocks.
|$formatInstructions
|
|Defined symbols include ${symbols.keys.joinToString(", ")} described below:
|
|```${this.describer.markupLanguage}
|${this.apiDescription}
|```
|
|THESE VARIABLES ARE READ-ONLY: ${symbols.keys.joinToString(", ")}
|They are already defined for you.
|
|${details ?: ""}
|""".trimMargin().trim()
} else """
|You are a coding assistant allowing users actions to be enacted using $language and the script context.
|Your role is to translate natural language instructions into code as well as interpret the results and converse with the user.
|Use ``` code blocks labeled with $language where appropriate. (i.e. ```$language)
|Each response should have EXACTLY ONE code block. Do not use inline blocks.
|$formatInstructions
|
|${details ?: ""}
|""".trimMargin().trim()
You are a coding assistant allows users actions to be enacted using $language and the script context.
Your role is to translate natural language instructions into code as well as interpret the results and converse with the user.
Use $TT code blocks labeled with $language where appropriate. (i.e. ${TT}$language)
Each response should have EXACTLY ONE code block. Do not use inline blocks.
$formatInstructions
Defined symbols include ${symbols.keys.joinToString(", ")} described below:
$TT${this.describer.markupLanguage}
${this.apiDescription}
${TT}
THESE VARIABLES ARE READ-ONLY: ${symbols.keys.joinToString(", ")}
They are already defined for you.
${details ?: ""}
""".trim()
} else """
You are a coding assistant allowing users actions to be enacted using $language and the script context.
Your role is to translate natural language instructions into code as well as interpret the results and converse with the user.
Use $TT code blocks labeled with $language where appropriate. (i.e. ${TT}$language)
Each response should have EXACTLY ONE code block. Do not use inline blocks.
$formatInstructions
${details ?: ""}
""".trim()
}

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


Expand All @@ -123,7 +130,7 @@ open class CodingActor(
}
if (questions.codePrefix.isNotBlank()) {
chatMessages = (chatMessages.dropLast(1) + listOf(
ChatMessage(Role.assistant, "Code Prefix:\n```\n${questions.codePrefix}\n```".toContentList())
ChatMessage(Role.assistant, "Code Prefix:\n$TT\n${questions.codePrefix}\n${TT}".toContentList())
) + chatMessages.last()).toTypedArray<ChatMessage>()
}
return chatMessages
Expand Down Expand Up @@ -156,7 +163,7 @@ open class CodingActor(
val respondWithCode = fixCommand(api, result.code, ex, *messages, model = model)
val blocks = extractTextBlocks(respondWithCode)
val renderedResponse = getRenderedResponse(blocks)
val codedInstruction = getCode(language, blocks)
val codedInstruction = codeInterceptor(getCode(language, blocks))
log.debug("Response: \n\t${renderedResponse.replace("\n", "\n\t", false)}".trimMargin())
log.debug("New Code: \n\t${codedInstruction.replace("\n", "\n\t", false)}".trimMargin())
result = CodeResultImpl(
Expand All @@ -175,7 +182,7 @@ open class CodingActor(
log.debug("Running $code")
OutputInterceptor.clearGlobalOutput()
val result = try {
interpreter.run((prefix + "\n" + code).sortCode())
interpreter.run((prefix + "\n" + codeInterceptor(code)).sortCode())
} catch (e: Exception) {
when {
e is FailedToImplementException -> throw e
Expand Down Expand Up @@ -247,14 +254,14 @@ open class CodingActor(
try {
val codeBlocks = extractTextBlocks(chat(api, request, model))
val renderedResponse = getRenderedResponse(codeBlocks)
val codedInstruction = getCode(language, codeBlocks)
val codedInstruction = codeInterceptor(getCode(language, codeBlocks))
log.debug("Response: \n\t${renderedResponse.replace("\n", "\n\t", false)}".trimMargin())
log.debug("New Code: \n\t${codedInstruction.replace("\n", "\n\t", false)}".trimMargin())
var workingCode = codedInstruction
var workingRenderedResponse = renderedResponse
for (fixAttempt in 0..input.fixIterations) {
try {
val validate = interpreter.validate((input.codePrefix + "\n" + workingCode).sortCode())
val validate = interpreter.validate((input.codePrefix + "\n" + codeInterceptor(workingCode)).sortCode())
if (validate != null) throw validate
log.debug("Validation succeeded")
_status = CodeResult.Status.Success
Expand All @@ -264,12 +271,12 @@ open class CodingActor(
throw if (ex is FailedToImplementException) ex else FailedToImplementException(
cause = ex,
message = """
|**ERROR**
|
|```text
|${ex.stackTraceToString()}
|```
|""".trimMargin().trim(),
**ERROR**
|
${TT}text
${ex.stackTraceToString()}
${TT}
""".trim(),
language = language,
code = workingCode,
prefix = input.codePrefix
Expand All @@ -279,7 +286,7 @@ open class CodingActor(
val respondWithCode = fixCommand(api, workingCode, ex, *messages, model = model)
val codeBlocks = extractTextBlocks(respondWithCode)
workingRenderedResponse = getRenderedResponse(codeBlocks)
workingCode = getCode(language, codeBlocks)
workingCode = codeInterceptor(getCode(language, codeBlocks))
log.debug(
"Response: \n\t${
workingRenderedResponse.replace(
Expand Down Expand Up @@ -324,22 +331,22 @@ open class CodingActor(
ChatMessage(
Role.assistant,
"""
|```${language.lowercase()}
|${previousCode.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
|""".trimMargin().trim().toContentList()
$TT${language.lowercase()}
${previousCode.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
${TT}
""".trim().toContentList()
),
ChatMessage(
Role.system,
"""
|The previous code failed with the following error:
|
|```
|${error.message?.trim() ?: "".let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
|
|Correct the code and try again.
|""".trimMargin().trim().toContentList()
The previous code failed with the following error:
$TT
${error.message?.trim() ?: "".let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
${TT}
Correct the code and try again.
""".trim().toContentList()
)
)
)
Expand All @@ -361,7 +368,8 @@ open class CodingActor(
model = model,
fallbackModel = fallbackModel,
temperature = temperature,
runtimeSymbols = runtimeSymbols
runtimeSymbols = runtimeSymbols,
codeInterceptor = codeInterceptor
)

companion object {
Expand All @@ -370,7 +378,7 @@ open class CodingActor(
fun String.indent(indent: String = " ") = this.replace("\n", "\n$indent")

fun extractTextBlocks(response: String): List<Pair<String, String>> {
val codeBlockRegex = Regex("(?s)```(.*?)\\n(.*?)```")
val codeBlockRegex = Regex("(?s)$TT(.*?)\\n(.*?)${TT}")
val languageRegex = Regex("([a-zA-Z0-9-_]+)")

val result = mutableListOf<Pair<String, String>>()
Expand Down Expand Up @@ -407,9 +415,9 @@ open class CodingActor(
fun getRenderedResponse(respondWithCode: List<Pair<String, String>>, defaultLanguage: String = "") =
respondWithCode.joinToString("\n") {
when (it.first) {
"code" -> "```$defaultLanguage\n${it.second.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n```"
"code" -> "$TT$defaultLanguage\n${it.second.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n${TT}"
"text" -> it.second.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }.toString()
else -> "```${it.first}\n${it.second.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n```"
else -> "$TT${it.first}\n${it.second.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n${TT}"
}
}

Expand All @@ -426,7 +434,7 @@ open class CodingActor(

fun String.sortCode(bodyWrapper: (String) -> String = { it }): String {
val (imports, otherCode) = this.split("\n").partition { it.trim().startsWith("import ") }
return imports.distinct().sorted().joinToString("\n") + "\n\n" + bodyWrapper(otherCode.joinToString("\n"))
return imports.map { it.trim() }.distinct().sorted().joinToString("\n") + "\n\n" + bodyWrapper(otherCode.joinToString("\n"))
}

fun String.camelCase(locale: Locale = Locale.getDefault()): String {
Expand Down Expand Up @@ -481,11 +489,11 @@ open class CodingActor(

fun errorMessage(ex: ScriptException, code: String) = try {
"""
|```text
|${TT}text
|${ex.message ?: ""} at line ${ex.lineNumber} column ${ex.columnNumber}
| ${if (ex.lineNumber > 0) code.split("\n")[ex.lineNumber - 1] else ""}
| ${if (ex.columnNumber > 0) " ".repeat(ex.columnNumber - 1) + "^" else ""}
|```
|${TT}
""".trimMargin().trim()
} catch (_: Exception) {
ex.message ?: ""
Expand All @@ -499,4 +507,4 @@ open class CodingActor(
val code: String? = null,
val prefix: String? = null,
) : RuntimeException(message, cause)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.simiacryptus.skyenet.core.actors

import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.ApiModel
import com.simiacryptus.jopenai.ApiModel.ChatMessage
import com.simiacryptus.jopenai.ApiModel.ImageGenerationRequest
import com.simiacryptus.jopenai.GPT4Tokenizer
import com.simiacryptus.jopenai.models.ApiModel
import com.simiacryptus.jopenai.models.ApiModel.ChatMessage
import com.simiacryptus.jopenai.models.ApiModel.ImageGenerationRequest
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.ImageModels
Expand Down Expand Up @@ -66,7 +65,7 @@ open class ImageActor(
override fun respond(input: List<String>, api: API, vararg messages: ChatMessage): ImageResponse {
var text = response(*messages, api = api).choices.first().message?.content
?: throw RuntimeException("No response")
while (imageModel.maxPrompt <= text.length) {
while (imageModel.maxPrompt <= text.length && null != openAI) {
text = response(
*listOf(
messages.toList(),
Expand All @@ -79,7 +78,7 @@ open class ImageActor(
api = api
).choices.first().message?.content ?: throw RuntimeException("No response")
}
return ImageResponseImpl(text, api = api)
return ImageResponseImpl(text, api = this.openAI!!)
}

override fun withModel(model: ChatModels): ImageActor = ImageActor(
Expand All @@ -92,9 +91,11 @@ open class ImageActor(
height = height,
)

var openAI: OpenAIClient? = null
fun setImageAPI(openAI: OpenAIClient): ImageActor {
this.openAI = openAI
return this
}

}

interface ImageResponse {
val text: String
val image: BufferedImage
}
Loading

0 comments on commit 56b8e63

Please sign in to comment.