Skip to content

Commit

Permalink
1.0.47 (#52)
Browse files Browse the repository at this point in the history
* 1.0.47

* wip

* pwd

* Update ApiKeyServlet.kt

* morning checkin

* wip

* ShellTool

* Update TestOpenAPITool.kt
  • Loading branch information
acharneski authored Feb 25, 2024
1 parent 7adef36 commit 0c05842
Show file tree
Hide file tree
Showing 29 changed files with 1,633 additions and 514 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.0.46</version>
<version>1.0.47</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.46'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.47'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.46")
implementation("com.simiacryptus:skyenet:1.0.47")
```

### 🌟 To Use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,38 @@ import com.simiacryptus.skyenet.core.util.FunctionWrapper
import com.simiacryptus.skyenet.core.util.JsonFunctionRecorder
import java.io.File

open class ActorSystem<T:Enum<*>>(
val actors: Map<T, BaseActor<*,*>>,
val dataStorage: StorageInterface,
val user: User?,
val session: Session
open class ActorSystem<T : Enum<*>>(
val actors: Map<T, BaseActor<*, *>>,
val dataStorage: StorageInterface,
val user: User?,
val session: Session
) {
private val sessionDir = dataStorage.getSessionDir(user, session)
protected val pool by lazy { ApplicationServices.clientManager.getPool(session, user, dataStorage) }
fun getActor(actor: T): BaseActor<*,*> {
private val sessionDir = dataStorage.getSessionDir(user, session)
protected val pool by lazy { ApplicationServices.clientManager.getPool(session, user, dataStorage) }

private val actorMap = mutableMapOf<T, BaseActor<*, *>>()

fun getActor(actor: T): BaseActor<*, *> {
return synchronized(actorMap) {
actorMap.computeIfAbsent(actor) {
val wrapper = getWrapper(actor.name)
return when (val baseActor = actors[actor]) {
null -> throw RuntimeException("No actor for $actor")
is SimpleActor -> SimpleActorInterceptor(baseActor, wrapper)
is ParsedActor<*> -> ParsedActorInterceptor(baseActor, wrapper)
is CodingActor -> CodingActorInterceptor(baseActor, wrapper)
is ImageActor -> ImageActorInterceptor(baseActor, wrapper)
is TextToSpeechActor -> TextToSpeechActorInterceptor(baseActor, wrapper)
else -> throw RuntimeException("Unknown actor type: ${baseActor.javaClass}")
when (val baseActor = actors[actor]) {
null -> throw RuntimeException("No actor for $actor")
is SimpleActor -> SimpleActorInterceptor(baseActor, wrapper)
is ParsedActor<*> -> ParsedActorInterceptor(baseActor, wrapper)
is CodingActor -> CodingActorInterceptor(baseActor, wrapper)
is ImageActor -> ImageActorInterceptor(baseActor, wrapper)
is TextToSpeechActor -> TextToSpeechActorInterceptor(baseActor, wrapper)
else -> throw RuntimeException("Unknown actor type: ${baseActor.javaClass}")
}
}
}
}

private val wrapperMap = mutableMapOf<String, FunctionWrapper>()
private fun getWrapper(name: String) = synchronized(wrapperMap) {
wrapperMap.getOrPut(name) {
FunctionWrapper(JsonFunctionRecorder(File(sessionDir, ".sys/actors/$name")))
}
private val wrapperMap = mutableMapOf<String, FunctionWrapper>()
private fun getWrapper(name: String) = synchronized(wrapperMap) {
wrapperMap.getOrPut(name) {
FunctionWrapper(JsonFunctionRecorder(File(sessionDir, ".sys/actors/$name")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,37 @@ open class CodingActor(
val resultOutput: String
)

var evalFormat = true
override val prompt: String
get() = 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.
|
|Defined symbols include {${symbols.keys.joinToString(", ")}} described below:
|
|```${this.describer.markupLanguage}
|${this.apiDescription}
|```
|
|${details ?: ""}
|""".trimMargin().trim()
else """
|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.
|
|${details ?: ""}
|""".trimMargin().trim()
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 ""
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}
|```
|
|${details ?: ""}
|""".trimMargin().trim()
} else """
|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
|
|${details ?: ""}
|""".trimMargin().trim()
}

open val apiDescription: String
get() = this.symbols.map { (name, utilityObj) ->
Expand Down Expand Up @@ -160,7 +168,7 @@ open class CodingActor(
log.debug("Running $code")
OutputInterceptor.clearGlobalOutput()
val result = try {
interpreter.run((prefix + code).sortCode())
interpreter.run((prefix + "\n" + code).sortCode())
} catch (e: Exception) {
when {
e is FailedToImplementException -> throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class ImageActor(
text.toChatMessage(),
"Please shorten the description".toChatMessage(),
),
).flatten().map { it as ChatMessage }.toTypedArray(),
).flatten().toTypedArray(),
model = imageModel,
api = api
).choices.first().message?.content ?: throw RuntimeException("No response")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.core.platform

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.google.common.util.concurrent.AtomicDouble
import com.simiacryptus.jopenai.ApiModel
Expand Down Expand Up @@ -267,6 +268,7 @@ data class User(
@get:JsonProperty("name") val name: String? = null,
@get:JsonProperty("id") val id: String? = null,
@get:JsonProperty("picture") val picture: String? = null,
@get:JsonIgnore val credential: Any? = null,
) {
override fun toString() = email

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ open class ClientManager {
if (!canUseGlobalKey) throw RuntimeException("No API key")
val logfile = dataStorage?.getSessionDir(user, session)?.resolve(".sys/openai.log")
logfile?.parentFile?.mkdirs()
return (if (ClientUtil.keyTxt?.isBlank() == false) {
return (if (ClientUtil.keyTxt.isNotBlank()) {
MonitoredClient(
key = ClientUtil.keyTxt,
logfile = logfile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ open class DataStorage(
val settings = getJson(sessionDir, "internal.json", Map::class.java) ?: mapOf<String,String>()
if(settings.containsKey("ids")) return settings["ids"].toString().split(",").toList()
val ids = messageFiles(sessionDir).entries.sortedBy { it.key.lastModified() }
?.map { it.key.nameWithoutExtension }?.toList() ?: listOf()
.map { it.key.nameWithoutExtension }.toList()
setJson(sessionDir, "internal.json", settings.plus("ids" to ids.joinToString(",")))
return ids
}
Expand Down Expand Up @@ -227,7 +227,7 @@ override fun getSessionTime(
override fun userRoot(user: User?) = File(
File(dataDir, "users"),
if (user?.email != null) {
user?.email
user.email
} else {
throw IllegalArgumentException("User required for private session")
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.46
libraryVersion = 1.0.47
gradleVersion = 7.6.1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ open class CodingAgent<T : Interpreter>(
displayCode(message, codeRequest)
} catch (e: Throwable) {
log.warn("Error", e)
message.error(e)
message.error(ui, e)
}
}

Expand All @@ -85,7 +85,7 @@ open class CodingAgent<T : Interpreter>(
displayCodeAndFeedback(task, codeRequest, codeResponse)
} catch (e: Throwable) {
log.warn("Error", e)
val error = task.error(e)
val error = task.error(ui, e)
var regenButton: StringBuilder? = null
regenButton = task.complete(ui.hrefLink("", "href-link regen-button") {
regenButton?.clear()
Expand All @@ -106,7 +106,7 @@ open class CodingAgent<T : Interpreter>(
displayCode(task, response)
displayFeedback(task, append(codeRequest, response), response)
} catch (e: Throwable) {
task.error(e)
task.error(ui, e)
log.warn("Error", e)
}
}
Expand All @@ -125,7 +125,7 @@ open class CodingAgent<T : Interpreter>(
task: SessionTask,
response: CodeResult
) {
task.add(
task.hideable(ui,
renderMarkdown(
response.renderedResponse ?:
//language=Markdown
Expand Down Expand Up @@ -243,76 +243,93 @@ open class CodingAgent<T : Interpreter>(
))
} catch (e: Throwable) {
log.warn("Error", e)
task.error(e)
task.error(ui, e)
}
}

protected open fun execute(
task: SessionTask,
response: CodeResult,
request: CodingActor.CodeRequest
request: CodingActor.CodeRequest,
) {
try {
val resultValue = response.result.resultValue
val resultOutput = response.result.resultOutput
val result = when {
resultValue.isBlank() || resultValue.trim().lowercase() == "null" -> """
|# Output
|```text
|${resultOutput}
|```
""".trimMargin()

else -> """
|# Result
|```
|$resultValue
|```
|
|# Output
|```text
|${resultOutput}
|```
""".trimMargin()
}
task.add(renderMarkdown(result))
val result = execute(task, response)
displayFeedback(task, CodingActor.CodeRequest(
messages = request.messages +
listOf(
"Running...\n\n$result" to ApiModel.Role.assistant,
).filter { it.first.isNotBlank() }
), response)
} catch (e: Throwable) {
val message = when {
e is ValidatedObject.ValidationError -> renderMarkdown(e.message ?: "")
e is CodingActor.FailedToImplementException -> renderMarkdown(
"""
|**Failed to Implement**
|
|${e.message}
|
|""".trimMargin()
)
handleExecutionError(e, task, request, response)
}
}

else -> renderMarkdown(
"""
|**Error `${e.javaClass.name}`**
protected open fun handleExecutionError(
e: Throwable,
task: SessionTask,
request: CodingActor.CodeRequest,
response: CodeResult
) {
val message = when {
e is ValidatedObject.ValidationError -> renderMarkdown(e.message ?: "")
e is CodingActor.FailedToImplementException -> renderMarkdown(
"""
|**Failed to Implement**
|
|${e.message}
|
|""".trimMargin()
)

else -> renderMarkdown(
"""
|**Error `${e.javaClass.name}`**
|
|```text
|${e.message}
|```
|""".trimMargin()
)
}
task.add(message, true, "div", "error")
displayCode(task, CodingActor.CodeRequest(
messages = request.messages +
listOf(
response.code to ApiModel.Role.assistant,
message to ApiModel.Role.system,
).filter { it.first.isNotBlank() }
))
}

fun execute(
task: SessionTask,
response: CodeResult
): String {
val resultValue = response.result.resultValue
val resultOutput = response.result.resultOutput
val result = when {
resultValue.isBlank() || resultValue.trim().lowercase() == "null" -> """
|# Output
|```text
|${resultOutput}
|```
""".trimMargin()

else -> """
|# Result
|```
|$resultValue
|```
|
|# Output
|```text
|${e.message}
|${resultOutput}
|```
|""".trimMargin()
)
}
task.add(message, true, "div", "error")
displayCode(task, CodingActor.CodeRequest(
messages = request.messages +
listOf(
response.code to ApiModel.Role.assistant,
message to ApiModel.Role.system,
).filter { it.first.isNotBlank() }
))
""".trimMargin()
}
task.add(renderMarkdown(result))
return result
}

companion object {
Expand Down
Loading

0 comments on commit 0c05842

Please sign in to comment.