Skip to content

Commit

Permalink
1.0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Oct 15, 2023
1 parent a72345a commit a4b92a0
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 79 deletions.
2 changes: 1 addition & 1 deletion core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ open class Brain(
if (moderated) api.moderate(json)
totalInputLength.addAndGet(json.length)
val chatResponse = api.chat(request, model)
var response = chatResponse.choices?.first()?.message?.content.orEmpty()
var response = chatResponse.choices.first()?.message?.content.orEmpty()
if (verbose) log.info(response)
totalOutputLength.addAndGet(response.length)
response = response.trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class KotlinLocalInterpreter(
init {
System.setProperty("idea.io.use.nio2","true")
val factory = KotlinJsr223JvmLocalScriptEngineFactory()
engine = factory.scriptEngine as org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine
engine = factory.scriptEngine as KotlinJsr223JvmLocalScriptEngine
val bindings: Bindings? = engine.getBindings(ScriptContext.GLOBAL_SCOPE)
defs.entrySet().forEach { (key, value) ->
engine.put(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ open class BasicChatSession(
|Ask questions to keep the conversation going.
|Say goodbye when the conversation is over.
""".trimMargin(),
) : ChatSession(parent, model, sessionId, visiblePrompt, hiddenPrompt, systemPrompt) {

}
) : ChatSession(parent, model, sessionId, visiblePrompt, hiddenPrompt, systemPrompt)
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class ChatSessionFlexmark(

fun defaultOptions(): MutableDataSet {
val options = MutableDataSet()
options.set(Parser.EXTENSIONS, listOf(TablesExtension.create()));
options.set(Parser.EXTENSIONS, listOf(TablesExtension.create()))
return options
}
}
Expand Down
19 changes: 10 additions & 9 deletions webui/src/main/kotlin/com/simiacryptus/skyenet/body/SessionBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ abstract class SessionBase(val sessionId: String) : SessionInterface {
return sentMessages
}

fun newUpdate(
fun newSessionDiv(
operationID: String, spinner: String
): (String, Boolean) -> Unit {
): SessionDiv {
var responseContents = ChatSession.divInitializer(operationID)
val sendUpdate: (String, Boolean) -> Unit = { message, showProgress ->
if (message.isNotBlank()) {
responseContents += """<div>$message</div>"""
send(responseContents)
return object : SessionDiv() {
override fun append(htmlToAppend: String, showSpinner: Boolean) {
if (htmlToAppend.isNotBlank()) {
responseContents += """<div>$htmlToAppend</div>"""
}
val spinner1 = if (showSpinner) """<div>$spinner</div>""" else ""
return this@SessionBase.send("""$responseContents$spinner1""")
}
val spinner = if (showProgress) """<div>$spinner</div>""" else ""
send("""$responseContents$spinner""")
}
send(responseContents)
return sendUpdate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ open class SkyenetCodingSession(
try {
heart.validate(codedInstruction)
buffer.append("""</div>""")
break;
break
} catch (ex: Throwable) {
buffer.append("""<pre><code class="language-$language">${codedInstruction}</code></pre><pre>${ex.message}</pre>""")
send("""$messageTrail$buffer${parent.spinner}</div>""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class SkyenetMacroChat(
val session : PersistentSessionBase = this
override fun run(userMessage: String) {
val operationID = ChatSession.randomID()
val sendUpdate = newUpdate(operationID, spinner)
val sessionDiv = newSessionDiv(operationID, spinner)
val thread = Thread {
playSempaphores[operationID] = Semaphore(0)
try {
Expand All @@ -69,7 +69,7 @@ abstract class SkyenetMacroChat(
</form>""".trimIndent()
}

}, sendUpdate)
}, sessionDiv)
} catch (e: Throwable) {
e.printStackTrace()
} finally {
Expand Down Expand Up @@ -107,32 +107,33 @@ abstract class SkyenetMacroChat(
userMessage: String,
session: PersistentSessionBase,
sessionUI: SessionUI,
sendUpdate: (String, Boolean) -> Unit
sessionDiv: SessionDiv
)

companion object {
fun iterate(

fun <T : Any> iterate(
sessionUI: SessionUI,
parameters: Any,
sendUpdate: (String, Boolean) -> Unit,
feedbackFn: (t: String) -> Unit,
selectLabel: String = "Execute",
selectFn: (t: Unit) -> Unit
) {
sendUpdate(
"""
<pre>${JsonUtil.toJson(parameters)}</pre>
<br/>
${sessionUI.textInput(feedbackFn)}
<br/>
${
sessionUI.hrefLink(
selectFn
)
}$selectLabel</a>""", false
)
}
sessionDiv: SessionDiv,
parameters: T,
feedbackFn: (msg: T, t: String) -> Unit,
fns: Map<String, (t: T) -> Unit>
) = sessionDiv.append(
"""
<pre>${JsonUtil.toJson(parameters)}</pre>
<br/>
${sessionUI.textInput { feedbackFn(parameters, it) }}
<br/>
${fns.entries.joinToString("\n") { (label, fn) ->
sessionUI.hrefLink { fn(parameters) } + label + "</a>"
}}
""", false
)
}

}

abstract class SessionDiv {
abstract fun append(htmlToAppend: String, showSpinner: Boolean) : Unit
}

25 changes: 13 additions & 12 deletions webui/src/test/kotlin/com/simiacryptus/skyenet/CookbookGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.skyenet.body.ChatSessionFlexmark
import com.simiacryptus.skyenet.body.PersistentSessionBase
import com.simiacryptus.skyenet.body.SessionDiv
import com.simiacryptus.skyenet.body.SkyenetMacroChat
import com.simiacryptus.util.JsonUtil
import com.simiacryptus.util.describe.Description
Expand Down Expand Up @@ -119,18 +120,18 @@ class CookbookGenerator(
userMessage: String,
session: PersistentSessionBase,
sessionUI: SessionUI,
sendUpdate: (String, Boolean) -> Unit
sessionDiv: SessionDiv
) {
sendUpdate("""<div>$userMessage</div>""", true)
sessionDiv.append("""<div>$userMessage</div>""", true)
val spec = cookbookAuthorAPI.parseRecipeSpec(userMessage)
sendUpdate("""<div><pre>${JsonUtil.toJson(spec)}</pre></div>""", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(spec)}</pre></div>""", true)
val recipes = cookbookAuthorAPI.createRecipes(spec, 20)
//sendUpdate("""<div><pre>${JsonUtil.toJson(recipes)}</pre></div>""", true)
//sessionDiv.apply("""<div><pre>${JsonUtil.toJson(recipes)}</pre></div>""", true)
for (recipe in recipes.recipeList.toMutableList().shuffled()) {
sendUpdate("""<div><pre>${JsonUtil.toJson(recipe)}</pre>${sessionUI.hrefLink {
sendUpdate("", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(recipe)}</pre>${sessionUI.hrefLink {
sessionDiv.append("", true)
val details = cookbookAuthorAPI.detailRecipe(recipe)
sendUpdate("""<div><pre>${JsonUtil.toJson(details)}</pre></div>""", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(details)}</pre></div>""", true)
val fullCookbook = cookbookAuthorAPI.getFullCookbook(
style = CookbookAuthorAPI.WritingStyle(
targetAudience = spec.targetAudience,
Expand All @@ -142,23 +143,23 @@ class CookbookGenerator(
),
recipe = details,
)
postRecipe(sendUpdate, fullCookbook, sessionUI, cookbookAuthorAPI)
postRecipe(sessionDiv, fullCookbook, sessionUI, cookbookAuthorAPI)
} }Expand</a></div>""", true)
}
}

private fun postRecipe(
sendUpdate: (String, Boolean) -> Unit,
sessionDiv: SessionDiv,
fullCookbook: CookbookAuthorAPI.Cookbook,
sessionUI: SessionUI,
cookbookAuthorAPI: CookbookAuthorAPI
) {
sendUpdate(
sessionDiv.append(
"""<div>${ChatSessionFlexmark.renderMarkdown(fullCookbook.markdown)}</div>${
sessionUI.textInput { userInput ->
sendUpdate("", true)
sessionDiv.append("", true)
val cookbook = cookbookAuthorAPI.modifyCookbook(fullCookbook.markdown, userInput)
postRecipe(sendUpdate, cookbook, sessionUI, cookbookAuthorAPI)
postRecipe(sessionDiv, cookbook, sessionUI, cookbookAuthorAPI)
}
}""", false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.skyenet.body.ChatSessionFlexmark
import com.simiacryptus.skyenet.body.PersistentSessionBase
import com.simiacryptus.skyenet.body.SessionDiv
import com.simiacryptus.skyenet.body.SkyenetMacroChat
import com.simiacryptus.util.JsonUtil
import com.simiacryptus.util.describe.Description
Expand Down Expand Up @@ -119,17 +120,17 @@ class SkyenetScienceBook(
userMessage: String,
session: PersistentSessionBase,
sessionUI: SessionUI,
sendUpdate: (String, Boolean) -> Unit
sessionDiv: SessionDiv
) {
sendUpdate("""<div>$userMessage</div>""", true)
sessionDiv.append("""<div>$userMessage</div>""", true)
val spec = scienceAuthorAPI.parseProjectSpec(userMessage)
sendUpdate("""<div><pre>${JsonUtil.toJson(spec)}</pre></div>""", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(spec)}</pre></div>""", true)
val experiments = scienceAuthorAPI.createExperiments(spec, 20)
for (experiment in experiments.experimentList.toMutableList().shuffled()) {
sendUpdate("""<div><pre>${JsonUtil.toJson(experiment)}</pre>${sessionUI.hrefLink {
sendUpdate("", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(experiment)}</pre>${sessionUI.hrefLink {
sessionDiv.append("", true)
val details = scienceAuthorAPI.detailExperiment(experiment)
sendUpdate("""<div><pre>${JsonUtil.toJson(details)}</pre></div>""", true)
sessionDiv.append("""<div><pre>${JsonUtil.toJson(details)}</pre></div>""", true)
val fullLabNotebook = scienceAuthorAPI.getFullLabNotebook(
style = ScienceAuthorAPI.WritingStyle(
targetAudience = spec.targetAudience,
Expand All @@ -141,23 +142,23 @@ class SkyenetScienceBook(
),
experiment = details,
)
postExperiment(sendUpdate, fullLabNotebook, sessionUI, scienceAuthorAPI)
postExperiment(sessionDiv, fullLabNotebook, sessionUI, scienceAuthorAPI)
} }Expand</a></div>""", true)
}
}

private fun postExperiment(
sendUpdate: (String, Boolean) -> Unit,
sessionDiv: SessionDiv,
fullLabNotebook: ScienceAuthorAPI.LabNotebook,
sessionUI: SessionUI,
scienceAuthorAPI: ScienceAuthorAPI
) {
sendUpdate(
sessionDiv.append(
"""<div>${ChatSessionFlexmark.renderMarkdown(fullLabNotebook.markdown)}</div>${
sessionUI.textInput { userInput ->
sendUpdate("", true)
sessionDiv.append("", true)
val labNotebook = scienceAuthorAPI.modifyNotebook(fullLabNotebook.markdown, userInput)
postExperiment(sendUpdate, labNotebook, sessionUI, scienceAuthorAPI)
postExperiment(sessionDiv, labNotebook, sessionUI, scienceAuthorAPI)
}
}""", false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.skyenet.body.ChatSessionFlexmark
import com.simiacryptus.skyenet.body.PersistentSessionBase
import com.simiacryptus.skyenet.body.SessionDiv
import com.simiacryptus.skyenet.body.SkyenetMacroChat
import com.simiacryptus.util.JsonUtil
import java.awt.Desktop
Expand Down Expand Up @@ -63,22 +64,22 @@ class SoftwareProjectGenerator(
userMessage: String,
session: PersistentSessionBase,
sessionUI: SessionUI,
sendUpdate: (String, Boolean) -> Unit
sessionDiv: SessionDiv
) {
try {
sendUpdate("""<div>${ChatSessionFlexmark.renderMarkdown(userMessage)}</div>""", true)
sessionDiv.append("""<div>${ChatSessionFlexmark.renderMarkdown(userMessage)}</div>""", true)
val projectParameters = projectAPI.parseProject(userMessage)
sendUpdate("""<pre>${JsonUtil.toJson(projectParameters)}</pre>""", true)
//sendUpdate("<hr/><div><em>${projectParameters.title}</em></div>", true)
sessionDiv.append("""<pre>${JsonUtil.toJson(projectParameters)}</pre>""", true)
//sessionDiv.apply("<hr/><div><em>${projectParameters.title}</em></div>", true)
val fileSpecList = projectAPI.expandProject(projectParameters)

fileSpecList.items.forEach { fileSpec ->
sendUpdate(
sessionDiv.append(
"""<div>${
sessionUI.hrefLink {
sendUpdate("<hr/><div><em>${fileSpec.filepath}</em></div>", true)
sessionDiv.append("<hr/><div><em>${fileSpec.filepath}</em></div>", true)
val fileImpl = projectAPI.implementFile(fileSpec)
sendUpdate("<pre>${fileImpl.text}</pre>", false)
sessionDiv.append("<pre>${fileImpl.text}</pre>", false)
}
}${fileSpec.filepath}</a></div>""", false)
}
Expand Down
21 changes: 11 additions & 10 deletions webui/src/test/kotlin/com/simiacryptus/skyenet/StoryGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.skyenet.body.ChatSessionFlexmark
import com.simiacryptus.skyenet.body.PersistentSessionBase
import com.simiacryptus.skyenet.body.SessionDiv
import com.simiacryptus.skyenet.body.SkyenetMacroChat
import com.simiacryptus.util.describe.Description
import java.awt.Desktop
Expand Down Expand Up @@ -82,18 +83,18 @@ class StoryGenerator(
userMessage: String,
session: PersistentSessionBase,
sessionUI: SessionUI,
sendUpdate: (String, Boolean) -> Unit
sessionDiv: SessionDiv
) {
try {
sendUpdate("""<div>${ChatSessionFlexmark.renderMarkdown(userMessage)}</div>""", true)
sessionDiv.append("""<div>${ChatSessionFlexmark.renderMarkdown(userMessage)}</div>""", true)
val storyParameters = storyAPI.generateStoryIdeas(userMessage)
storyParameters.items.forEach { storyParameters ->
sendUpdate(
sessionDiv.append(
"""<div>${
sessionUI.hrefLink {
sendUpdate("<hr/><div><em>${storyParameters.title}</em></div>", true)
sessionDiv.append("<hr/><div><em>${storyParameters.title}</em></div>", true)
extracted(
sendUpdate = sendUpdate,
sessionDiv = sessionDiv,
history = listOf(),
storyPage = storyAPI.getFirstStoryPage(storyParameters),
sessionUI = sessionUI,
Expand All @@ -108,7 +109,7 @@ class StoryGenerator(
}

private fun extracted(
sendUpdate: (String, Boolean) -> Unit,
sessionDiv: SessionDiv,
history: List<String>,
storyPage: StoryAPI.StoryPage,
sessionUI: SessionUI,
Expand All @@ -121,20 +122,20 @@ class StoryGenerator(
summary = storyAPI.accumulateSummary(summary, history + storyPage.text)
history = listOf()
}
sendUpdate(("""
sessionDiv.append(("""
<div>${ChatSessionFlexmark.renderMarkdown(storyPage.text)}</div>
<ol>
${
storyPage.choices.joinToString("\n") { choice ->
sendUpdate("", true)
sessionDiv.append("", true)
"<li>${
sessionUI.hrefLink {
sendUpdate(
sessionDiv.append(
"""<div><em>${ChatSessionFlexmark.renderMarkdown(choice.text)}</em></div>""",
true
)
extracted(
sendUpdate = sendUpdate,
sessionDiv = sessionDiv,
history = history + (storyPage.text + "\n\n" + choice.text),
storyPage = storyAPI.nextStoryPage(
storyParameters,
Expand Down

0 comments on commit a4b92a0

Please sign in to comment.