Skip to content

Commit

Permalink
1.0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 9, 2023
1 parent d87f490 commit 37d5f6f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 62 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.21</version>
<version>1.0.22</version>
</dependency>
```

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
Expand Down
111 changes: 61 additions & 50 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,14 +14,14 @@ import java.util.concurrent.atomic.AtomicInteger
@Suppress("MemberVisibilityCanBePrivate")
open class Brain(
val api: OpenAIClient,
val hands: java.util.Map<String, Object> = java.util.HashMap<String, Object>() as java.util.Map<String, Object>,
val symbols: java.util.Map<String, Object> = java.util.HashMap<String, Object>() as java.util.Map<String, Object>,
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<String, Any>
get() = hashMapOf(
Expand All @@ -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<ChatMessage>()
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<ChatMessage> = listOf(
open fun getChatSystemMessages(apiDescription: String): List<ChatMessage> = 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
Expand All @@ -67,65 +70,73 @@ open class Brain(
)

open fun fixCommand(
prompt: String,
previousCode: String,
error: Throwable,
output: String
output: String,
vararg prompt: String
): Pair<String, List<Pair<String, String>>> {
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<String, List<Pair<String, String>>> {
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<ChatMessage>()
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)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Expand Down Expand Up @@ -176,7 +176,7 @@ open class SkyenetCodingSession(
buffer.append("""<pre><code class="language-$language">${codedInstruction}</code></pre><pre>${ex.message}</pre>""")
send("""$messageTrail$buffer${SkyenetSessionServerBase.spinner}</div>""")
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("""<div>${renderedResponse}</div>""")
Expand All @@ -201,7 +201,7 @@ open class SkyenetCodingSession(
//language=HTML
send("""$messageTrail<div><h3>New Code:</h3>${SkyenetSessionServerBase.spinner}</div>""")
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")
Expand Down

0 comments on commit 37d5f6f

Please sign in to comment.