Skip to content

Commit

Permalink
1.0.36
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 21, 2023
1 parent 3208f6e commit bb51c2d
Show file tree
Hide file tree
Showing 32 changed files with 217 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,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
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
@@ -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,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.SimpleActor
Expand All @@ -15,7 +16,7 @@ class SimpleActorInterceptor(
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 @@ -182,7 +182,7 @@ open class DataStorage(
return Session("U-$yyyyMMdd-$uuid")
}

private const val MESSAGE_DIR = "messages"
private val MESSAGE_DIR = ".sys" + File.separator + "messages"

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.simiacryptus.skyenet.platform
import com.simiacryptus.skyenet.platform.DataStorage.Companion.validateSessionId

data class Session(
val sessionId: String
internal val sessionId: String
) {
init {
validateSessionId(this)
}

override fun toString() = sessionId
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ open class UsageManager {
val txLogFileWriter = txLogFileWriter
if (null != txLogFileWriter) {
synchronized(txLogFile) {
txLogFileWriter.write("$session,$user,${model.modelName},${tokens.prompt_tokens},input\n")
txLogFileWriter.write("$session,$user,${model.modelName},${tokens.completion_tokens},output\n")
txLogFileWriter.write("$session,${user?.email},${model.modelName},${tokens.prompt_tokens},input\n")
txLogFileWriter.write("$session,${user?.email},${model.modelName},${tokens.completion_tokens},output\n")
txLogFileWriter.flush()
}
}
Expand Down
26 changes: 21 additions & 5 deletions core/src/main/kotlin/com/simiacryptus/skyenet/platform/User.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package com.simiacryptus.skyenet.platform

data class User(
val email: String,
val id: String? = null, // TODO: Remove default value
val name: String? = null, // TODO: Remove default value
val picture: String? = null, // TODO: Remove default value
)
internal val email: String,
internal val name: String? = null,
internal val id: String? = null,
internal val picture: String? = null,
) {
override fun toString() = email

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as User

return email == other.email
}

override fun hashCode(): Int {
return email.hashCode()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ class JsonFunctionRecorder(baseDir: File) : FunctionInterceptor, Closeable {
)
// Get the caller method name from the stack trace (first caller not in internalClassList)
val caller = Thread.currentThread().stackTrace
.firstOrNull { !internalClassList.contains(Class.forName(it.className)) }
.filter { !internalClassList.contains(Class.forName(it.className)) }
.filter { it.methodName != "intercept" }
.firstOrNull()
val methodName = caller?.methodName ?: "unknown"
val file = File(baseDirectory, "$id-$yyyyMMddHHmmss-$methodName")
if(file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.simiacryptus.skyenet

import com.simiacryptus.openai.OpenAIAPI
import com.simiacryptus.skyenet.servlet.AppInfoServlet
import com.simiacryptus.skyenet.chat.ChatServer
import com.simiacryptus.skyenet.chat.ChatSocket
import com.simiacryptus.skyenet.platform.ApplicationServices.authenticationManager
import com.simiacryptus.skyenet.platform.ApplicationServices.authorizationManager
import com.simiacryptus.skyenet.platform.ApplicationServices.dataStorageFactory
Expand All @@ -14,6 +14,7 @@ import com.simiacryptus.skyenet.platform.AuthorizationManager
import com.simiacryptus.skyenet.platform.DataStorage
import com.simiacryptus.skyenet.platform.Session
import com.simiacryptus.skyenet.platform.User
import com.simiacryptus.skyenet.session.ApplicationInterface
import com.simiacryptus.skyenet.session.ApplicationSocketManager
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
Expand Down Expand Up @@ -50,14 +51,14 @@ abstract class ApplicationBase(
userMessage: String,
socketManager: ApplicationSocketManager,
sessionMessage: SessionMessage,
socket: ChatSocket
api: OpenAIAPI
) = this@ApplicationBase.newSession(
session = session,
user = user,
userMessage = userMessage,
socketManager = socketManager.applicationInterface,
sessionMessage = sessionMessage,
socket = socket
api = api
)
}
}
Expand All @@ -66,9 +67,9 @@ abstract class ApplicationBase(
session: Session,
user: User?,
userMessage: String,
socketManager: ApplicationSocketManager.ApplicationInterface,
socketManager: ApplicationInterface,
sessionMessage: SessionMessage,
socket: ChatSocket
api: OpenAIAPI
)

open val settingsClass: Class<*> get() = Map::class.java
Expand Down
Loading

0 comments on commit bb51c2d

Please sign in to comment.