Skip to content

Commit

Permalink
1.0.36 (#40)
Browse files Browse the repository at this point in the history
* 1.0.36

* wip

* wip

* 1.0.36
  • Loading branch information
acharneski authored Nov 21, 2023
1 parent da4259c commit fd0c07b
Show file tree
Hide file tree
Showing 55 changed files with 726 additions and 503 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.35</version>
<version>1.0.36</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.35'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.36'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.35")
implementation("com.simiacryptus:skyenet:1.0.36")
```

### 🌟 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 @@ -31,7 +31,7 @@ val logback_version = "1.4.11"

dependencies {

implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.32")
implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.33")

implementation(group = "org.slf4j", name = "slf4j-api", version = "2.0.9")
implementation(group = "commons-io", name = "commons-io", version = "2.15.0")
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ open class Brain(
companion object {
private val log = org.slf4j.LoggerFactory.getLogger(Brain::class.java)
fun String.indent(indent: String = " ") = this.replace("\n", "\n$indent")
fun joinYamlList(typeDescriptions: List<String>) = typeDescriptions.joinToString("\n") {
private fun joinYamlList(typeDescriptions: List<String>) = typeDescriptions.joinToString("\n") {
"- " + it.indent()
}

fun Method.superMethod(): Method? {
private fun Method.superMethod(): Method? {
val superMethod = declaringClass.superclasses.flatMap { it.methods.toList() }
.find { it.name == name && it.parameters.size == parameters.size }
return superMethod?.superMethod() ?: superMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package com.simiacryptus.skyenet.actors

import com.simiacryptus.skyenet.actors.record.*
import com.simiacryptus.skyenet.platform.DataStorage
import com.simiacryptus.skyenet.platform.Session
import com.simiacryptus.skyenet.platform.User
import com.simiacryptus.skyenet.util.FunctionWrapper
import com.simiacryptus.skyenet.util.JsonFunctionRecorder
import java.io.File

open class ActorSystem<T:Enum<*>>(
private val actors: Map<T, BaseActor<*>>,
private val dataStorage: DataStorage,
val userId: String?,
val sessionId: String
dataStorage: DataStorage,
val userId: User?,
val sessionId: Session
) {
val sessionDir = dataStorage.getSessionDir(userId, sessionId)
private val sessionDir = dataStorage.getSessionDir(userId, sessionId)
fun getActor(actor: T): BaseActor<*> {
val wrapper = getWrapper(actor.name)
return when (val baseActor = actors[actor]) {
Expand All @@ -26,6 +28,6 @@ open class ActorSystem<T:Enum<*>>(

private val wrapperMap = mutableMapOf<String, FunctionWrapper>()
private fun getWrapper(name: String) = wrapperMap.computeIfAbsent(name) {
FunctionWrapper(JsonFunctionRecorder(File(sessionDir, "actors/$name")))
FunctionWrapper(JsonFunctionRecorder(File(sessionDir, ".sys/actors/$name")))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.OpenAIClient
Expand All @@ -12,16 +13,16 @@ abstract class BaseActor<T>(
val model: OpenAITextModel = ChatModels.GPT35Turbo,
val temperature: Double = 0.3,
) {
open fun response(vararg messages: OpenAIClient.ChatMessage, model: OpenAIModel = this.model, api: OpenAIClient) = api.chat(
abstract fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): T
open fun response(vararg messages: OpenAIClient.ChatMessage, model: OpenAIModel = this.model, api: OpenAIAPI) = (api as OpenAIClient).chat(
OpenAIClient.ChatRequest(
messages = ArrayList(messages.toList()),
temperature = temperature,
model = this.model.modelName,
),
model = this.model
)
abstract fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): T
open fun answer(vararg questions: String, api: OpenAIClient): T = answer(*chatMessages(*questions), api = api)
open fun answer(vararg questions: String, api: OpenAIAPI): T = answer(*chatMessages(*questions), api = api)

open fun chatMessages(vararg questions: String) = arrayOf(
OpenAIClient.ChatMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.simiacryptus.skyenet.actors

import com.fasterxml.jackson.annotation.JsonIgnore
import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.OpenAIClientBase.Companion.toContentList
import com.simiacryptus.openai.models.ChatModels
Expand Down Expand Up @@ -66,33 +67,33 @@ open class CodingActor(

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

override fun answer(vararg questions: String, api: OpenAIClient): CodeResult =
override fun answer(vararg questions: String, api: OpenAIAPI): CodeResult =
if (!autoEvaluate) answer(*chatMessages(*questions), api = api)
else answerWithAutoEval(*chatMessages(*questions), api = api).first

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): CodeResult =
if (!autoEvaluate) CodeResultImpl(*messages, api = api)
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): CodeResult =
if (!autoEvaluate) CodeResultImpl(*messages, api = (api as OpenAIClient))
else answerWithAutoEval(*messages, api = api).first

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

open fun answerWithAutoEval(
vararg messages: String,
api: OpenAIClient,
api: OpenAIAPI,
codePrefix: String = ""
) = answerWithAutoEval(*injectCodePrefix(chatMessages(*messages), codePrefix), api = api)

open fun answerWithAutoEval(
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIClient
api: OpenAIAPI
): Pair<CodeResult, ExecutionResult> {
var result = CodeResultImpl(*messages, api = api)
var result = CodeResultImpl(*messages, api = (api as OpenAIClient))
var lastError: Throwable? = null
for (i in 0..fixIterations) try {
return result to result.run()
Expand Down Expand Up @@ -159,7 +160,7 @@ open class CodingActor(
var codedInstruction = implement(
brain(api, model), *messages, codePrefix = codePrefix
)
if (_status != CodeResult.Status.Success) {
if (_status != CodeResult.Status.Success && fallbackModel != model) {
codedInstruction = implement(
brain(api, fallbackModel), *messages, codePrefix = codePrefix
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.models.OpenAITextModel
Expand All @@ -20,10 +21,10 @@ open class ParsedActor<T:Any>(
) {
val resultClass: Class<T> by lazy { parserClass.getMethod("apply", String::class.java).returnType as Class<T> }

private inner class ParsedResponseImpl(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient) : ParsedResponse<T>(resultClass) {
private inner class ParsedResponseImpl(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI) : ParsedResponse<T>(resultClass) {
private val parser: Function<String, T> = ChatProxy(
clazz = parserClass,
api = api,
api = (api as OpenAIClient),
model = ChatModels.GPT35Turbo,
temperature = temperature,
).create()
Expand All @@ -33,7 +34,7 @@ open class ParsedActor<T:Any>(
override fun getObj(clazz: Class<T>): T = _obj
}

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): ParsedResponse<T> {
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): ParsedResponse<T> {
return ParsedResponseImpl(*messages, api = api)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.models.OpenAITextModel
Expand All @@ -16,7 +17,7 @@ open class SimpleActor(
temperature = temperature,
) {

override fun answer(vararg questions: String, api: OpenAIClient): String = answer(*chatMessages(*questions), api = api)
override fun answer(vararg questions: String, api: OpenAIAPI): String = answer(*chatMessages(*questions), api = api)

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): String = response(*messages, api = api).choices.first().message?.content ?: throw RuntimeException("No response")
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): String = response(*messages, api = api).choices.first().message?.content ?: throw RuntimeException("No response")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class Expectation {
private val log = LoggerFactory.getLogger(Expectation::class.java)
}

open class VectorMatch(val example: String, private val metric: DistanceType = DistanceType.Cosine) : Expectation() {
open class VectorMatch(private val example: String, private val metric: DistanceType = DistanceType.Cosine) : Expectation() {
override fun matches(api: OpenAIClient, response: String): Boolean {
return true
}
Expand All @@ -37,7 +37,7 @@ abstract class Expectation {
}

open class ContainsMatch(
val pattern: Regex,
private val pattern: Regex,
val critical: Boolean = true
) : Expectation() {
override fun matches(api: OpenAIClient, response: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors.record

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.skyenet.actors.CodeResult
Expand All @@ -20,10 +21,10 @@ class CodingActorInterceptor(
temperature = inner.temperature,
autoEvaluate = inner.autoEvaluate,
) {
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): CodeResult {
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): CodeResult {
return functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
CodingResultInterceptor(*messages, api = api, inner = inner.answer(*messages, api = api))
CodingResultInterceptor(*messages, api = (api as OpenAIClient), inner = inner.answer(*messages, api = api))
}
}

Expand All @@ -41,7 +42,7 @@ class CodingActorInterceptor(
override fun response(
vararg messages: OpenAIClient.ChatMessage,
model: OpenAIModel,
api: OpenAIClient
api: OpenAIAPI
) = functionInterceptor.wrap(messages.toList().toTypedArray(), model) {
messages: Array<OpenAIClient.ChatMessage>,
model: OpenAIModel ->
Expand All @@ -52,14 +53,14 @@ class CodingActorInterceptor(
inner.chatMessages(*it)
}

override fun answer(vararg questions: String, api: OpenAIClient) = functionInterceptor.wrap(questions) {
override fun answer(vararg questions: String, api: OpenAIAPI) = functionInterceptor.wrap(questions) {
inner.answer(*it, api = api)
}

override fun answerWithPrefix(
codePrefix: String,
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIClient
api: OpenAIAPI
) = functionInterceptor.wrap(messages.toList().toTypedArray(), codePrefix) {
messages: Array<OpenAIClient.ChatMessage>,
codePrefix: String ->
Expand All @@ -68,7 +69,7 @@ class CodingActorInterceptor(

override fun answerWithAutoEval(
vararg messages: String,
api: OpenAIClient,
api: OpenAIAPI,
codePrefix: String
) = functionInterceptor.wrap(messages.toList().toTypedArray(), codePrefix) {
messages: Array<String>,
Expand All @@ -78,7 +79,7 @@ class CodingActorInterceptor(

override fun answerWithAutoEval(
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIClient
api: OpenAIAPI
) = functionInterceptor.wrap(messages.toList().toTypedArray()) {
inner.answerWithAutoEval(*messages, api = api)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors.record

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.skyenet.actors.ParsedActor
Expand All @@ -16,13 +17,13 @@ class ParsedActorInterceptor<T:Any>(
model = inner.model,
temperature = inner.temperature,
) {
private inner class ParsedResponseInterceptor(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient, private val inner: ParsedResponse<T>) :
private inner class ParsedResponseInterceptor(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI, private val inner: ParsedResponse<T>) :
ParsedResponse<T>(this@ParsedActorInterceptor.inner.resultClass) {
override fun getText() = functionInterceptor.wrap { inner.getText() }
override fun getObj(clazz: Class<T>) = functionInterceptor.intercept(clazz) { inner.getObj(clazz) } // <-- Cannot use 'T' as reified type parameter. Use a class instead.
}

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): ParsedResponse<T> {
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): ParsedResponse<T> {
return functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
ParsedResponseInterceptor(*messages, api = api, inner = inner.answer(*messages, api = api))
Expand All @@ -32,14 +33,14 @@ class ParsedActorInterceptor<T:Any>(
override fun response(
vararg messages: OpenAIClient.ChatMessage,
model: OpenAIModel,
api: OpenAIClient
api: OpenAIAPI
) = functionInterceptor.wrap(messages.toList().toTypedArray(), model) {
messages: Array<OpenAIClient.ChatMessage>,
model: OpenAIModel ->
inner.response(*messages, model = model, api = api)
}

override fun answer(vararg questions: String, api: OpenAIClient) = functionInterceptor.wrap(questions) {
override fun answer(vararg questions: String, api: OpenAIAPI) = functionInterceptor.wrap(questions) {
inner.answer(*it, api = api)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.simiacryptus.skyenet.actors.record

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.skyenet.actors.SimpleActor
import com.simiacryptus.skyenet.util.FunctionWrapper

class SimpleActorInterceptor(
val inner: SimpleActor,
val functionInterceptor: FunctionWrapper,
private val functionInterceptor: FunctionWrapper,
) : SimpleActor(
prompt = inner.prompt,
name = inner.name,
model = inner.model,
temperature = inner.temperature,
) {

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient) =
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI) =
functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
inner.answer(*messages, api = api)
Expand All @@ -24,7 +25,7 @@ class SimpleActorInterceptor(
override fun response(
vararg messages: OpenAIClient.ChatMessage,
model: OpenAIModel,
api: OpenAIClient
api: OpenAIAPI
) = functionInterceptor.wrap(messages.toList().toTypedArray(), model) {
messages: Array<OpenAIClient.ChatMessage>,
model: OpenAIModel ->
Expand All @@ -35,7 +36,7 @@ class SimpleActorInterceptor(
inner.chatMessages(*it)
}

override fun answer(vararg questions: String, api: OpenAIClient) = functionInterceptor.wrap(questions) {
override fun answer(vararg questions: String, api: OpenAIAPI) = functionInterceptor.wrap(questions) {
inner.answer(*it, api = api)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.simiacryptus.skyenet.actors.ParsedResponse
import java.util.function.Function

abstract class ParsedActorTestBase<T:Any>(
val parserClass: Class<out Function<String, T>>,
private val parserClass: Class<out Function<String, T>>,
) : ActorTestBase<ParsedResponse<T>>() {

override fun actorFactory(prompt: String) = ParsedActor(
Expand Down
Loading

0 comments on commit fd0c07b

Please sign in to comment.