Skip to content

Commit

Permalink
1.0.72 (#77)
Browse files Browse the repository at this point in the history
* 1.0.72

* 1.0.72

* appScript

* Settings

* js refactors

* wip
  • Loading branch information
acharneski authored May 21, 2024
1 parent 7e828b8 commit e062167
Show file tree
Hide file tree
Showing 27 changed files with 1,068 additions and 562 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.core.platform.User
import com.simiacryptus.skyenet.core.platform.file.DataStorage.Companion.SYS_DIR
import com.simiacryptus.skyenet.core.util.FunctionWrapper
import com.simiacryptus.skyenet.core.util.JsonFunctionRecorder
import java.io.File
Expand Down Expand Up @@ -68,7 +67,7 @@ open class ActorSystem<T : Enum<*>>(
wrapperMap.getOrPut(name) {
FunctionWrapper(JsonFunctionRecorder(
File(
SYS_DIR,
ApplicationServices.dataStorageRoot,
"${if (session.isGlobal()) "global" else user}/$session/actors/$name"
).apply { mkdirs() }))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ open class CodingActor(
|Each response should have EXACTLY ONE code block. Do not use inline blocks.
|$formatInstructions
|
|Defined symbols include {${symbols.keys.joinToString(", ")}} described below:
|Defined symbols include ${symbols.keys.joinToString(", ")} described below:
|
|```${this.describer.markupLanguage}
|${this.apiDescription}
|```
|
|DO NOT RE-DEFINE THESE SYMBOLS: ${symbols.keys.joinToString(", ")}
|They are already defined for you.
|
|${details ?: ""}
|""".trimMargin().trim()
} else """
Expand Down Expand Up @@ -475,12 +478,12 @@ open class CodingActor(

fun errorMessage(ex: ScriptException, code: String) = try {
"""
|```text
|${ex.message ?: ""} at line ${ex.lineNumber} column ${ex.columnNumber}
| ${if (ex.lineNumber > 0) code.split("\n")[ex.lineNumber - 1] else ""}
| ${if (ex.columnNumber > 0) " ".repeat(ex.columnNumber - 1) + "^" else ""}
|```
""".trimMargin().trim()
|```text
|${ex.message ?: ""} at line ${ex.lineNumber} column ${ex.columnNumber}
| ${if (ex.lineNumber > 0) code.split("\n")[ex.lineNumber - 1] else ""}
| ${if (ex.columnNumber > 0) " ".repeat(ex.columnNumber - 1) + "^" else ""}
|```
""".trimMargin().trim()
} catch (_: Exception) {
ex.message ?: ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object ApplicationServices {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var usageManager: UsageInterface = UsageManager(File(dataStorageRoot, ".skyenet/usage"))
var usageManager: UsageInterface = UsageManager(File(dataStorageRoot, "usage"))
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.APIProvider
import com.simiacryptus.jopenai.models.OpenAIModel
import com.simiacryptus.jopenai.util.ClientUtil
import com.simiacryptus.skyenet.core.platform.ApplicationServices.dataStorageFactory
import com.simiacryptus.skyenet.core.platform.ApplicationServices.dataStorageRoot
import com.simiacryptus.skyenet.core.platform.ApplicationServices.userSettingsManager
import com.simiacryptus.skyenet.core.platform.AuthorizationInterface.OperationType
import com.simiacryptus.skyenet.core.platform.file.DataStorage.Companion.SYS_DIR
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient
import org.apache.hc.core5.http.HttpRequest
import org.slf4j.LoggerFactory
import org.slf4j.event.Level
import java.io.File
import java.util.concurrent.*
Expand All @@ -30,6 +33,7 @@ open class ClientManager {
user: User?,
dataStorage: StorageInterface?,
): OpenAIClient {
log.debug("Fetching client for session: {}, user: {}", session, user)
val key = SessionKey(session, user)
return if (null == dataStorage) clientCache[key] ?: throw IllegalStateException("No data storage")
else clientCache.getOrPut(key) { createClient(session, user, dataStorage)!! }
Expand All @@ -42,15 +46,17 @@ open class ClientManager {
SynchronousQueue(),
RecordingThreadFactory(session, user)
)

/*createScheduledPool*/
protected open fun createScheduledPool(session: Session, user: User?, dataStorage: StorageInterface?) =
MoreExecutors.listeningDecorator(ScheduledThreadPoolExecutor(1,))
MoreExecutors.listeningDecorator(ScheduledThreadPoolExecutor(1))

fun getPool(
session: Session,
user: User?,
dataStorage: StorageInterface?,
): ThreadPoolExecutor {
log.debug("Fetching thread pool for session: {}, user: {}", session, user)
val key = SessionKey(session, user)
return poolCache.getOrPut(key) {
createPool(session, user, dataStorage)
Expand All @@ -62,19 +68,21 @@ open class ClientManager {
user: User?,
dataStorage: StorageInterface?,
): ListeningScheduledExecutorService {
log.debug("Fetching scheduled pool for session: {}, user: {}", session, user)
val key = SessionKey(session, user)
return scheduledPoolCache.getOrPut(key) {
createScheduledPool(session, user, dataStorage)
}
}

inner class RecordingThreadFactory(
session: Session,
user: User?
val session: Session,
val user: User?
) : ThreadFactory {
private val inner = ThreadFactoryBuilder().setNameFormat("Session $session; User $user; #%d").build()
val threads = mutableSetOf<Thread>()
override fun newThread(r: Runnable): Thread {
log.debug("Creating new thread for session: {}, user: {}", session, user)
inner.newThread(r).also {
threads.add(it)
return it
Expand All @@ -87,10 +95,13 @@ open class ClientManager {
user: User?,
dataStorage: StorageInterface?,
): OpenAIClient? {
log.debug("Creating client for session: {}, user: {}", session, user)
if (user != null) {
val userSettings = ApplicationServices.userSettingsManager.getUserSettings(user)
val logfile = SYS_DIR?.resolve("${if (session.isGlobal()) "global" else user}/$session/openai.log")?.apply { parentFile?.mkdirs() }
val userSettings = userSettingsManager.getUserSettings(user)

val logfile = dataStorageFactory(dataStorageRoot).getSessionDir(user, session).resolve("openai.log").apply { mkdirs() }.resolve("openai.log")
logfile?.parentFile?.mkdirs()
log.debug("Logfile: {}", logfile)
val userApi =
if (userSettings.apiKeys.isNotEmpty())
MonitoredClient(
Expand All @@ -107,7 +118,8 @@ open class ClientManager {
null, user, OperationType.GlobalKey
)
if (!canUseGlobalKey) throw RuntimeException("No API key")
val logfile = SYS_DIR?.resolve("${if (session.isGlobal()) "global" else user}/$session/openai.log")?.apply { parentFile?.mkdirs() }
val logfile = dataStorageRoot?.resolve("${if (session.isGlobal()) "global" else user}/$session/openai.log")
?.apply { parentFile?.mkdirs() }
logfile?.parentFile?.mkdirs()
return (if (ClientUtil.keyMap.isNotEmpty()) {
MonitoredClient(
Expand Down Expand Up @@ -144,16 +156,26 @@ open class ClientManager {
) {
var budget = 2.00
override fun authorize(request: HttpRequest, apiProvider: APIProvider) {
log.debug("Authorizing request for session: {}, user: {}, apiProvider: {}", session, user, apiProvider)
require(budget > 0.0) { "Budget Exceeded" }
super.authorize(request, ClientUtil.defaultApiProvider)
}

override fun onUsage(model: OpenAIModel?, tokens: ApiModel.Usage) {
log.debug(
"Usage recorded for session: {}, user: {}, model: {}, tokens: {}",
session,
user,
model,
tokens
)
ApplicationServices.usageManager.incrementUsage(session, user, model!!, tokens)
budget -= tokens.cost ?: 0.0
super.onUsage(model, tokens)
}
}

companion object
companion object {
private val log = LoggerFactory.getLogger(ClientManager::class.java)
}
}
Loading

0 comments on commit e062167

Please sign in to comment.