Skip to content

Commit

Permalink
jo-penai updates
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 22, 2023
1 parent 52019b2 commit abf995b
Show file tree
Hide file tree
Showing 35 changed files with 176 additions and 183 deletions.
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.33")
implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.34")

implementation(group = "org.slf4j", name = "slf4j-api", version = "2.0.9")
implementation(group = "commons-io", name = "commons-io", version = "2.15.0")
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

package com.simiacryptus.skyenet

import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.OpenAIClient.*
import com.simiacryptus.openai.OpenAIClientBase.Companion.toContentList
import com.simiacryptus.openai.models.OpenAITextModel
import com.simiacryptus.util.JsonUtil.toJson
import com.simiacryptus.util.describe.TypeDescriber
import com.simiacryptus.util.describe.YamlDescriber
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.ApiModel.*
import com.simiacryptus.jopenai.ClientUtil.toContentList
import com.simiacryptus.jopenai.models.OpenAITextModel
import com.simiacryptus.jopenai.util.JsonUtil.toJson
import com.simiacryptus.jopenai.describe.TypeDescriber
import com.simiacryptus.jopenai.describe.YamlDescriber
import org.intellij.lang.annotations.Language
import java.lang.reflect.Method
import java.lang.reflect.Modifier
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Ears.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.simiacryptus.skyenet

import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.util.audio.AudioRecorder
import com.simiacryptus.util.audio.LookbackLoudnessWindowBuffer
import com.simiacryptus.util.audio.TranscriptionProcessor
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.proxy.ChatProxy
import com.simiacryptus.jopenai.audio.AudioRecorder
import com.simiacryptus.jopenai.audio.LookbackLoudnessWindowBuffer
import com.simiacryptus.jopenai.audio.TranscriptionProcessor
import org.slf4j.LoggerFactory
import java.util.*
import java.util.concurrent.ConcurrentLinkedDeque
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/kotlin/com/simiacryptus/skyenet/actors/BaseActor.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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
import com.simiacryptus.openai.OpenAIClientBase.Companion.toContentList
import com.simiacryptus.openai.models.OpenAITextModel
import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.models.OpenAIModel
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.ClientUtil.toContentList
import com.simiacryptus.jopenai.models.OpenAITextModel

abstract class BaseActor<T>(
open val prompt: String,
val name: String? = null,
val model: OpenAITextModel = ChatModels.GPT35Turbo,
val temperature: Double = 0.3,
) {
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(
abstract fun answer(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API): T
open fun response(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, model: OpenAIModel = this.model, api: API) = (api as OpenAIClient).chat(
com.simiacryptus.jopenai.ApiModel.ChatRequest(
messages = ArrayList(messages.toList()),
temperature = temperature,
model = this.model.modelName,
),
model = this.model
)
open fun answer(vararg questions: String, api: OpenAIAPI): T = answer(*chatMessages(*questions), api = api)
open fun answer(vararg questions: String, api: API): T = answer(*chatMessages(*questions), api = api)

open fun chatMessages(vararg questions: String) = arrayOf(
OpenAIClient.ChatMessage(
role = OpenAIClient.Role.system,
com.simiacryptus.jopenai.ApiModel.ChatMessage(
role = com.simiacryptus.jopenai.ApiModel.Role.system,
content = prompt.toContentList()
),
) + questions.map {
OpenAIClient.ChatMessage(
role = OpenAIClient.Role.user,
com.simiacryptus.jopenai.ApiModel.ChatMessage(
role = com.simiacryptus.jopenai.ApiModel.Role.user,
content = it.toContentList()
)
}
Expand Down
40 changes: 20 additions & 20 deletions core/src/main/kotlin/com/simiacryptus/skyenet/actors/CodingActor.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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
import com.simiacryptus.openai.models.OpenAITextModel
import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.ClientUtil.toContentList
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.OpenAITextModel
import com.simiacryptus.skyenet.Brain
import com.simiacryptus.skyenet.Brain.Companion.indent
import com.simiacryptus.skyenet.Heart
import com.simiacryptus.skyenet.OutputInterceptor
import com.simiacryptus.util.describe.AbbrevWhitelistYamlDescriber
import com.simiacryptus.util.describe.TypeDescriber
import com.simiacryptus.jopenai.describe.AbbrevWhitelistYamlDescriber
import com.simiacryptus.jopenai.describe.TypeDescriber
import kotlin.reflect.KClass

@Suppress("unused", "MemberVisibilityCanBePrivate")
Expand Down Expand Up @@ -67,31 +67,31 @@ open class CodingActor(

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

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

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): CodeResult =
override fun answer(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API): 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: OpenAIAPI
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
api: API
): CodeResult =
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: OpenAIAPI,
api: API,
codePrefix: String = ""
) = answerWithAutoEval(*injectCodePrefix(chatMessages(*messages), codePrefix), api = api)

open fun answerWithAutoEval(
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIAPI
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
api: API
): Pair<CodeResult, ExecutionResult> {
var result = CodeResultImpl(*messages, api = (api as OpenAIClient))
var lastError: Throwable? = null
Expand All @@ -117,15 +117,15 @@ open class CodingActor(
}

private fun injectCodePrefix(
messages: Array<out OpenAIClient.ChatMessage>,
messages: Array<out com.simiacryptus.jopenai.ApiModel.ChatMessage>,
codePrefix: String
) = (messages.dropLast(1) + if (codePrefix.isBlank()) listOf() else listOf(
OpenAIClient.ChatMessage(OpenAIClient.Role.assistant, codePrefix.toContentList())
com.simiacryptus.jopenai.ApiModel.ChatMessage(com.simiacryptus.jopenai.ApiModel.Role.assistant, codePrefix.toContentList())
) + messages.last()).toTypedArray()

private fun fix(
api: OpenAIClient,
messages: Array<out OpenAIClient.ChatMessage>,
messages: Array<out com.simiacryptus.jopenai.ApiModel.ChatMessage>,
result: CodingActor.CodeResultImpl,
ex: Throwable
): CodingActor.CodeResultImpl {
Expand All @@ -147,7 +147,7 @@ open class CodingActor(
)

private inner class CodeResultImpl(
vararg messages: OpenAIClient.ChatMessage,
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
codePrefix: String = "",
api: OpenAIClient,
) : CodeResult {
Expand Down Expand Up @@ -175,7 +175,7 @@ open class CodingActor(

fun implement(
brain: Brain,
vararg messages: OpenAIClient.ChatMessage,
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
codePrefix: String = "",
): String {
val response = brain.implement(*messages)
Expand All @@ -194,7 +194,7 @@ open class CodingActor(
brain: Brain,
codePrefix: String,
initialCode: String,
messages: Array<out OpenAIClient.ChatMessage>
messages: Array<out com.simiacryptus.jopenai.ApiModel.ChatMessage>
): String? {
var workingCode = initialCode
for (fixAttempt in 0..fixIterations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.OpenAITextModel
import com.simiacryptus.jopenai.proxy.ChatProxy
import java.util.function.Function

open class ParsedActor<T:Any>(
Expand All @@ -21,7 +21,7 @@ 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: OpenAIAPI) : ParsedResponse<T>(resultClass) {
private inner class ParsedResponseImpl(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API) : ParsedResponse<T>(resultClass) {
private val parser: Function<String, T> = ChatProxy(
clazz = parserClass,
api = (api as OpenAIClient),
Expand All @@ -34,7 +34,7 @@ open class ParsedActor<T:Any>(
override fun getObj(clazz: Class<T>): T = _obj
}

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): ParsedResponse<T> {
override fun answer(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API): ParsedResponse<T> {
return ParsedResponseImpl(*messages, api = api)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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
import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.OpenAITextModel

open class SimpleActor(
prompt: String,
Expand All @@ -17,7 +16,7 @@ open class SimpleActor(
temperature = temperature,
) {

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

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): String = response(*messages, api = api).choices.first().message?.content ?: throw RuntimeException("No response")
override fun answer(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API): String = response(*messages, api = api).choices.first().message?.content ?: throw RuntimeException("No response")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.simiacryptus.skyenet.actors.opt

import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.OpenAITextModel
import com.simiacryptus.skyenet.actors.opt.ActorOptimization.GeneticApi.Prompt
import com.simiacryptus.openai.proxy.ChatProxy
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.describe.Description
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.OpenAITextModel
import com.simiacryptus.jopenai.proxy.ChatProxy
import com.simiacryptus.skyenet.actors.BaseActor
import com.simiacryptus.util.describe.Description
import com.simiacryptus.skyenet.actors.opt.ActorOptimization.GeneticApi.Prompt
import org.slf4j.LoggerFactory
import kotlin.math.ceil
import kotlin.math.ln
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.simiacryptus.skyenet.actors.opt

import com.simiacryptus.openai.models.ChatModels
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.models.EmbeddingModels
import com.simiacryptus.openai.opt.DistanceType
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.EmbeddingModels
import com.simiacryptus.jopenai.opt.DistanceType
import org.slf4j.LoggerFactory

abstract class Expectation {
Expand All @@ -30,7 +29,7 @@ abstract class Expectation {
}

private fun createEmbedding(api: OpenAIClient, str: String) = api.createEmbedding(
OpenAIClient.EmbeddingRequest(
com.simiacryptus.jopenai.ApiModel.EmbeddingRequest(
model = EmbeddingModels.AdaEmbedding.modelName, input = str
)
).data.first().embedding!!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.jopenai.API
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.OpenAIModel
import com.simiacryptus.skyenet.actors.CodeResult
import com.simiacryptus.skyenet.actors.CodingActor
import com.simiacryptus.skyenet.util.FunctionWrapper
Expand All @@ -21,15 +21,15 @@ class CodingActorInterceptor(
temperature = inner.temperature,
autoEvaluate = inner.autoEvaluate,
) {
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIAPI): CodeResult {
override fun answer(vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage, api: API): CodeResult {
return functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
messages: Array<com.simiacryptus.jopenai.ApiModel.ChatMessage> ->
CodingResultInterceptor(*messages, api = (api as OpenAIClient), inner = inner.answer(*messages, api = api))
}
}

private inner class CodingResultInterceptor(
vararg messages: OpenAIClient.ChatMessage,
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
private val inner: CodeResult,
api: OpenAIClient,
) : CodeResult {
Expand All @@ -40,11 +40,11 @@ class CodingActorInterceptor(
}

override fun response(
vararg messages: OpenAIClient.ChatMessage,
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
model: OpenAIModel,
api: OpenAIAPI
api: API
) = functionInterceptor.wrap(messages.toList().toTypedArray(), model) {
messages: Array<OpenAIClient.ChatMessage>,
messages: Array<com.simiacryptus.jopenai.ApiModel.ChatMessage>,
model: OpenAIModel ->
inner.response(*messages, model = model, api = api)
}
Expand All @@ -53,23 +53,23 @@ class CodingActorInterceptor(
inner.chatMessages(*it)
}

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

override fun answerWithPrefix(
codePrefix: String,
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIAPI
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
api: API
) = functionInterceptor.wrap(messages.toList().toTypedArray(), codePrefix) {
messages: Array<OpenAIClient.ChatMessage>,
messages: Array<com.simiacryptus.jopenai.ApiModel.ChatMessage>,
codePrefix: String ->
inner.answerWithPrefix(codePrefix, *messages, api = api)
}

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

override fun answerWithAutoEval(
vararg messages: OpenAIClient.ChatMessage,
api: OpenAIAPI
vararg messages: com.simiacryptus.jopenai.ApiModel.ChatMessage,
api: API
) = functionInterceptor.wrap(messages.toList().toTypedArray()) {
inner.answerWithAutoEval(*messages, api = api)
}
Expand Down
Loading

0 comments on commit abf995b

Please sign in to comment.